From bd4078d5b6c6f427bc2191a768abed024bb29bed Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Tue, 25 Feb 2020 14:41:55 +0100 Subject: [PATCH] fix(@ngtools/webpack): don't elide decorated method parameter type reference Fixes #17080 (cherry picked from commit 678180e4768cca10b322eac6c79ae8dd90c093c9) --- .../webpack/src/transformers/elide_imports.ts | 6 +-- .../src/transformers/elide_imports_spec.ts | 44 ++++++++++--------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/packages/ngtools/webpack/src/transformers/elide_imports.ts b/packages/ngtools/webpack/src/transformers/elide_imports.ts index 9107926876c9..d243535319d5 100644 --- a/packages/ngtools/webpack/src/transformers/elide_imports.ts +++ b/packages/ngtools/webpack/src/transformers/elide_imports.ts @@ -78,11 +78,9 @@ export function elideImports( (ts.isConstructorDeclaration(parent.parent) && !!parent.parent.parent.decorators?.length)); break; } + if (isTypeReferenceForDecoratoredNode) { - symbol = typeChecker.getSymbolAtLocation(node); - } else { - // If type reference is not for Decorator skip and marked as unused. - return; + symbol = typeChecker.getSymbolAtLocation(node.typeName); } } else { switch (node.kind) { diff --git a/packages/ngtools/webpack/src/transformers/elide_imports_spec.ts b/packages/ngtools/webpack/src/transformers/elide_imports_spec.ts index d037e77d3f6e..6b3a5885433c 100644 --- a/packages/ngtools/webpack/src/transformers/elide_imports_spec.ts +++ b/packages/ngtools/webpack/src/transformers/elide_imports_spec.ts @@ -281,6 +281,27 @@ describe('@ngtools/webpack transformers', () => { expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`); }); + it(`should remove import for 'ExpressionWithTypeArguments' implements token`, () => { + const input = tags.stripIndent` + import { Bar, Buz, Unused } from './bar'; + + export class Foo extends Bar implements Buz { } + + ${dummyNode} + `; + + const output = tags.stripIndent` + import { Bar } from './bar'; + + export class Foo extends Bar { } + `; + + const { program, compilerHost } = createTypescriptContext(input); + const result = transformTypescript(undefined, [transformer(program)], program, compilerHost); + + expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`); + }); + describe('should elide imports decorator type references when emitDecoratorMetadata is false', () => { const extraCompilerOptions: ts.CompilerOptions = { emitDecoratorMetadata: false, @@ -317,27 +338,6 @@ describe('@ngtools/webpack transformers', () => { }); }); - it(`should remove import for 'ExpressionWithTypeArguments' implements token`, () => { - const input = tags.stripIndent` - import { Bar, Buz, Unused } from './bar'; - - export class Foo extends Bar implements Buz { } - - ${dummyNode} - `; - - const output = tags.stripIndent` - import { Bar } from './bar'; - - export class Foo extends Bar { } - `; - - const { program, compilerHost } = createTypescriptContext(input); - const result = transformTypescript(undefined, [transformer(program)], program, compilerHost); - - expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`); - }); - describe('should not elide imports decorator type references when emitDecoratorMetadata is true', () => { const extraCompilerOptions: ts.CompilerOptions = { emitDecoratorMetadata: true, @@ -515,6 +515,8 @@ describe('@ngtools/webpack transformers', () => { import { __decorate, __metadata } from "tslib"; import { Decorator } from './decorator'; + import { Service } from './service'; + export class Foo { name(f) { } } __decorate([ Decorator(), __metadata("design:type", Function), __metadata("design:paramtypes", [Service]),