Skip to content

Commit

Permalink
fix(migrations): duplicated comments on migrated classes (#48966)
Browse files Browse the repository at this point in the history
Fixes that the migration was duplicating the comments on class nodes that were being converted to standalone.

Fixes #48943.

PR Close #48966
  • Loading branch information
crisbeto authored and atscott committed Feb 6, 2023
1 parent 2de6dae commit 759db12
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,11 @@ function addPropertyToAngularDecorator(
return node;
}

return ts.factory.updateDecorator(
node,
ts.factory.createCallExpression(node.expression.expression, node.expression.typeArguments, [
ts.factory.createObjectLiteralExpression(literalProperties, literalProperties.length > 1)
]));
// Use `createDecorator` instead of `updateDecorator`, because
// the latter ends up duplicating the node's leading comment.
return ts.factory.createDecorator(ts.factory.createCallExpression(
node.expression.expression, node.expression.typeArguments,
[ts.factory.createObjectLiteralExpression(literalProperties, literalProperties.length > 1)]));
}

/** Checks if a node is a `PropertyAssignment` with a name. */
Expand Down
28 changes: 28 additions & 0 deletions packages/core/schematics/test/standalone_migration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,34 @@ describe('standalone migration', () => {
`));
});

it('should not duplicate doc strings', async () => {
writeFile('module.ts', `
import {NgModule, Directive} from '@angular/core';
/** Directive used for testing. */
@Directive({selector: '[dir]'})
export class MyDir {}
/** Module used for testing. */
@NgModule({declarations: [MyDir]})
export class Mod {}
`);

await runMigration('convert-to-standalone');

expect(stripWhitespace(tree.readContent('module.ts'))).toBe(stripWhitespace(`
import {NgModule, Directive} from '@angular/core';
/** Directive used for testing. */
@Directive({selector: '[dir]', standalone: true})
export class MyDir {}
/** Module used for testing. */
@NgModule({imports: [MyDir]})
export class Mod {}
`));
});

it('should remove a module that only has imports and exports', async () => {
writeFile('app.module.ts', `
import {NgModule} from '@angular/core';
Expand Down

0 comments on commit 759db12

Please sign in to comment.