From 759db12e0b618fcb51f4cb141adeb49bfa495a60 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Mon, 6 Feb 2023 09:01:29 +0100 Subject: [PATCH] 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 --- .../standalone-migration/to-standalone.ts | 10 +++---- .../test/standalone_migration_spec.ts | 28 +++++++++++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/packages/core/schematics/ng-generate/standalone-migration/to-standalone.ts b/packages/core/schematics/ng-generate/standalone-migration/to-standalone.ts index 1de2f50c966..f5ee3c2073c 100644 --- a/packages/core/schematics/ng-generate/standalone-migration/to-standalone.ts +++ b/packages/core/schematics/ng-generate/standalone-migration/to-standalone.ts @@ -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. */ diff --git a/packages/core/schematics/test/standalone_migration_spec.ts b/packages/core/schematics/test/standalone_migration_spec.ts index d4408b9dba0..881380af2e1 100644 --- a/packages/core/schematics/test/standalone_migration_spec.ts +++ b/packages/core/schematics/test/standalone_migration_spec.ts @@ -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';