diff --git a/migration/fixtures/single-import.component.ts b/migration/fixtures/single-import.component.ts index a6e7fd7..0d37e2d 100644 --- a/migration/fixtures/single-import.component.ts +++ b/migration/fixtures/single-import.component.ts @@ -2,6 +2,7 @@ import { of } from 'rxjs'; import { untilDestroyed } from 'ngx-take-until-destroy'; import { OnDestroy } from '@angular/core'; +// test comment @Component({ template: '' }) export class SingleImportComponent extends BaseComponent implements OnDestroy { create() { diff --git a/migration/run.spec.ts b/migration/run.spec.ts index 0d79d35..808708c 100644 --- a/migration/run.spec.ts +++ b/migration/run.spec.ts @@ -65,6 +65,11 @@ describe('Migration script', () => { expect(result).not.toContain('ngOnDestroy()'); }); + it('should place @UntilDestroy decorator to a new line after the comment', () => { + const result = transformCode(code, 'single-import.component.ts', true); + expect(result).toContain(`// test comment\n@UntilDestroy()`); + }); + it('should remove class implements', () => { const result = transformCode(code, 'single-import.component.ts', true); expect(result).toContain(`export class SingleImportComponent extends BaseComponent {`); diff --git a/migration/run.ts b/migration/run.ts index 6f482bb..d4cdc5b 100644 --- a/migration/run.ts +++ b/migration/run.ts @@ -64,12 +64,7 @@ function replaceOldImport(sourceFile: SourceFile) { } function addUntilDestroyDecorator(classDeclaration: ClassDeclaration) { - const decorators = [ - { name: 'UntilDestroy', arguments: [] }, - ...(classDeclaration.getStructure().decorators || []) - ]; - classDeclaration.getDecorators().forEach(d => d.remove()); - classDeclaration.addDecorators(decorators); + classDeclaration.insertDecorator(0, { name: 'UntilDestroy', arguments: [] }); } function removeOnDestroyImplements(classDeclaration: ClassDeclaration) {