Skip to content

Commit

Permalink
fix(@ngtools/webpack): fix elide removing whole imports on single match
Browse files Browse the repository at this point in the history
If an import imports multiple symbols, the previous condition meant that the whole
import statement was removed, instead of only the symbol.

Fixes #8518.
  • Loading branch information
hansl authored and Brocco committed Nov 17, 2017
1 parent c676fe1 commit 62f3454
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export function elideImports(
})
.forEach((symbol) => {
// Remove the whole declaration if it's a single import.
const nodeToRemove = symbol.singleImport ? symbol.importSpec : symbol.importDecl;
const nodeToRemove = symbol.singleImport ? symbol.importDecl : symbol.importSpec;
ops.push(new RemoveNodeOperation(sourceFile, nodeToRemove));
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,41 @@ describe('@ngtools/webpack transformers', () => {

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

it('should not remove imports from types that are still used', () => {
const input = stripIndent`
import { Component, EventEmitter } from '@angular/core';
@Component({
selector: 'app-root',
changeDetection: ChangeDetectionStrategy.OnPush,
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
notify: EventEmitter<string> = new EventEmitter<string>();
title = 'app';
}
`;
const output = stripIndent`
import { EventEmitter } from '@angular/core';
export class AppComponent {
constructor() {
this.notify = new EventEmitter();
this.title = 'app';
}
}
`;

const { program, compilerHost } = createTypescriptContext(input);
const transformer = removeDecorators(
() => true,
() => program.getTypeChecker(),
);
const result = transformTypescript(undefined, [transformer], program, compilerHost);

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

0 comments on commit 62f3454

Please sign in to comment.