Skip to content

fix(@ngtools/webpack): account for shorthand properties when import eliding #9699

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 21, 2018
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
9 changes: 6 additions & 3 deletions packages/@ngtools/webpack/src/transformers/elide_imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function elideImports(
const typeChecker = getTypeChecker();

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

ts.forEachChild(node, visit);
Expand All @@ -54,7 +57,7 @@ export function elideImports(
}

const isUnused = (node: ts.Identifier) => {
if (exportSpecifiers.has(node.text)) {
if (specialCaseNames.has(node.text)) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ describe('@ngtools/webpack transformers', () => {
it('should not remove imports from types that are still used', () => {
const input = stripIndent`
import { Component, ChangeDetectionStrategy, EventEmitter } from '@angular/core';
import { abc } from 'xyz';

@Component({
selector: 'app-root',
Expand All @@ -210,17 +211,20 @@ describe('@ngtools/webpack transformers', () => {
export class AppComponent {
notify: EventEmitter<string> = new EventEmitter<string>();
title = 'app';
example = { abc };
}

export { ChangeDetectionStrategy };
`;
const output = stripIndent`
import { ChangeDetectionStrategy, EventEmitter } from '@angular/core';
import { abc } from 'xyz';

export class AppComponent {
constructor() {
this.notify = new EventEmitter();
this.title = 'app';
this.example = { abc };
}
}

Expand Down