Skip to content

Commit 759db12

Browse files
crisbetoatscott
authored andcommitted
fix(migrations): duplicated comments on migrated classes (#48966)
Fixes that the migration was duplicating the comments on class nodes that were being converted to standalone. Fixes #48943. PR Close #48966
1 parent 2de6dae commit 759db12

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

Diff for: packages/core/schematics/ng-generate/standalone-migration/to-standalone.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,11 @@ function addPropertyToAngularDecorator(
286286
return node;
287287
}
288288

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

296296
/** Checks if a node is a `PropertyAssignment` with a name. */

Diff for: packages/core/schematics/test/standalone_migration_spec.ts

+28
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,34 @@ describe('standalone migration', () => {
12941294
`));
12951295
});
12961296

1297+
it('should not duplicate doc strings', async () => {
1298+
writeFile('module.ts', `
1299+
import {NgModule, Directive} from '@angular/core';
1300+
1301+
/** Directive used for testing. */
1302+
@Directive({selector: '[dir]'})
1303+
export class MyDir {}
1304+
1305+
/** Module used for testing. */
1306+
@NgModule({declarations: [MyDir]})
1307+
export class Mod {}
1308+
`);
1309+
1310+
await runMigration('convert-to-standalone');
1311+
1312+
expect(stripWhitespace(tree.readContent('module.ts'))).toBe(stripWhitespace(`
1313+
import {NgModule, Directive} from '@angular/core';
1314+
1315+
/** Directive used for testing. */
1316+
@Directive({selector: '[dir]', standalone: true})
1317+
export class MyDir {}
1318+
1319+
/** Module used for testing. */
1320+
@NgModule({imports: [MyDir]})
1321+
export class Mod {}
1322+
`));
1323+
});
1324+
12971325
it('should remove a module that only has imports and exports', async () => {
12981326
writeFile('app.module.ts', `
12991327
import {NgModule} from '@angular/core';

0 commit comments

Comments
 (0)