Skip to content
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

fix(@ngtools/webpack): elide unused type references #24318

Merged
merged 1 commit into from Nov 28, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 26 additions & 23 deletions packages/ngtools/webpack/src/transformers/elide_imports.ts
Expand Up @@ -54,31 +54,34 @@ export function elideImports(
return;
}

if (!ts.isTypeReferenceNode(node)) {
let symbol: ts.Symbol | undefined;
switch (node.kind) {
case ts.SyntaxKind.Identifier:
const parent = node.parent;
if (parent && ts.isShorthandPropertyAssignment(parent)) {
const shorthandSymbol = typeChecker.getShorthandAssignmentValueSymbol(parent);
if (shorthandSymbol) {
symbol = shorthandSymbol;
}
} else {
symbol = typeChecker.getSymbolAtLocation(node);
// Type reference imports do not need to be emitted when emitDecoratorMetadata is disabled.
if (ts.isTypeReferenceNode(node) && !compilerOptions.emitDecoratorMetadata) {
return;
}

let symbol: ts.Symbol | undefined;
switch (node.kind) {
case ts.SyntaxKind.Identifier:
const parent = node.parent;
if (parent && ts.isShorthandPropertyAssignment(parent)) {
const shorthandSymbol = typeChecker.getShorthandAssignmentValueSymbol(parent);
if (shorthandSymbol) {
symbol = shorthandSymbol;
}
break;
case ts.SyntaxKind.ExportSpecifier:
symbol = typeChecker.getExportSpecifierLocalTargetSymbol(node as ts.ExportSpecifier);
break;
case ts.SyntaxKind.ShorthandPropertyAssignment:
symbol = typeChecker.getShorthandAssignmentValueSymbol(node);
break;
}
} else {
symbol = typeChecker.getSymbolAtLocation(node);
}
break;
case ts.SyntaxKind.ExportSpecifier:
symbol = typeChecker.getExportSpecifierLocalTargetSymbol(node as ts.ExportSpecifier);
break;
case ts.SyntaxKind.ShorthandPropertyAssignment:
symbol = typeChecker.getShorthandAssignmentValueSymbol(node);
break;
}

if (symbol) {
usedSymbols.add(symbol);
}
if (symbol) {
usedSymbols.add(symbol);
}

ts.forEachChild(node, visit);
Expand Down
40 changes: 40 additions & 0 deletions packages/ngtools/webpack/src/transformers/elide_imports_spec.ts
Expand Up @@ -66,6 +66,7 @@ describe('@ngtools/webpack transformers', () => {
`,
'service.ts': `
export class Service { }
export class Service2 { }
`,
'type.ts': `
export interface OnChanges {
Expand Down Expand Up @@ -385,6 +386,45 @@ describe('@ngtools/webpack transformers', () => {

expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
});

it('should remove ctor parameter type reference and unused named import from same declaration', () => {
const input = tags.stripIndent`
import { Decorator } from './decorator';
import { Service, Service2 as ServiceUnused } from './service';

@Decorator()
export class Foo {
constructor(param: Service) {
}
}

${dummyNode}
`;

const output = tags.stripIndent`
import { __decorate } from "tslib";
import { Decorator } from './decorator';

let Foo = class Foo { constructor(param) { } };
Foo = __decorate([ Decorator() ], Foo);
export { Foo };
`;

const { program, compilerHost } = createTypescriptContext(
input,
additionalFiles,
true,
extraCompilerOptions,
);
const result = transformTypescript(
undefined,
[transformer(program)],
program,
compilerHost,
);

expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
});
});

it('keeps jsxFactory imports when configured', () => {
Expand Down