Skip to content

fix(@ngtools/webpack): don't elide decorated method parameter type reference #17083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions packages/ngtools/webpack/src/transformers/elide_imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
44 changes: 23 additions & 21 deletions packages/ngtools/webpack/src/transformers/elide_imports_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -515,6 +515,8 @@ describe('@ngtools/webpack transformers', () => {
import { __decorate, __metadata } from "tslib";

import { Decorator } from './decorator';
import { Service } from './service';
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops 😱


export class Foo { name(f) { } }

__decorate([ Decorator(), __metadata("design:type", Function), __metadata("design:paramtypes", [Service]),
Expand Down