Skip to content

Commit a20b314

Browse files
clydinhansl
authored andcommitted
fix(@ngtools/webpack): account for shorthand properties when import eliding
1 parent 27106b6 commit a20b314

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

packages/@ngtools/webpack/src/transformers/elide_imports.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function elideImports(
2222
const typeChecker = getTypeChecker();
2323

2424
// Collect all imports and used identifiers
25-
const exportSpecifiers = new Set<string>();
25+
const specialCaseNames = new Set<string>();
2626
const usedSymbols = new Set<ts.Symbol>();
2727
const imports = new Array<ts.ImportDeclaration>();
2828
ts.forEachChild(sourceFile, function visit(node) {
@@ -42,8 +42,11 @@ export function elideImports(
4242
} else if (ts.isExportSpecifier(node)) {
4343
// Export specifiers return the non-local symbol from the above
4444
// so check the name string instead
45-
exportSpecifiers.add((node.propertyName || node.name).text);
45+
specialCaseNames.add((node.propertyName || node.name).text);
4646
return;
47+
} else if (ts.isShorthandPropertyAssignment(node)) {
48+
// Shorthand property assignments return the object property's symbol not the import's
49+
specialCaseNames.add(node.name.text);
4750
}
4851

4952
ts.forEachChild(node, visit);
@@ -54,7 +57,7 @@ export function elideImports(
5457
}
5558

5659
const isUnused = (node: ts.Identifier) => {
57-
if (exportSpecifiers.has(node.text)) {
60+
if (specialCaseNames.has(node.text)) {
5861
return false;
5962
}
6063

packages/@ngtools/webpack/src/transformers/remove_decorators.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ describe('@ngtools/webpack transformers', () => {
200200
it('should not remove imports from types that are still used', () => {
201201
const input = stripIndent`
202202
import { Component, ChangeDetectionStrategy, EventEmitter } from '@angular/core';
203+
import { abc } from 'xyz';
203204
204205
@Component({
205206
selector: 'app-root',
@@ -210,17 +211,20 @@ describe('@ngtools/webpack transformers', () => {
210211
export class AppComponent {
211212
notify: EventEmitter<string> = new EventEmitter<string>();
212213
title = 'app';
214+
example = { abc };
213215
}
214216
215217
export { ChangeDetectionStrategy };
216218
`;
217219
const output = stripIndent`
218220
import { ChangeDetectionStrategy, EventEmitter } from '@angular/core';
221+
import { abc } from 'xyz';
219222
220223
export class AppComponent {
221224
constructor() {
222225
this.notify = new EventEmitter();
223226
this.title = 'app';
227+
this.example = { abc };
224228
}
225229
}
226230

0 commit comments

Comments
 (0)