Skip to content

Commit f6d9ba8

Browse files
committed
fix(@ngtools/webpack): fix elide removing whole imports on single match
If an import imports multiple symbols, the previous condition meant that the whole import statement was removed, instead of only the symbol. Fixes #8518.
1 parent 811cbfe commit f6d9ba8

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export function elideImports(
113113
})
114114
.forEach((symbol) => {
115115
// Remove the whole declaration if it's a single import.
116-
const nodeToRemove = symbol.singleImport ? symbol.importSpec : symbol.importDecl;
116+
const nodeToRemove = symbol.singleImport ? symbol.importDecl : symbol.importSpec;
117117
ops.push(new RemoveNodeOperation(sourceFile, nodeToRemove));
118118
});
119119

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,41 @@ describe('@ngtools/webpack transformers', () => {
108108

109109
expect(oneLine`${result}`).toEqual(oneLine`${output}`);
110110
});
111+
112+
it('should not remove imports from types that are still used', () => {
113+
const input = stripIndent`
114+
import { Component, EventEmitter } from '@angular/core';
115+
116+
@Component({
117+
selector: 'app-root',
118+
changeDetection: ChangeDetectionStrategy.OnPush,
119+
templateUrl: './app.component.html',
120+
styleUrls: ['./app.component.css']
121+
})
122+
export class AppComponent {
123+
notify: EventEmitter<string> = new EventEmitter<string>();
124+
title = 'app';
125+
}
126+
`;
127+
const output = stripIndent`
128+
import { EventEmitter } from '@angular/core';
129+
130+
export class AppComponent {
131+
constructor() {
132+
this.notify = new EventEmitter();
133+
this.title = 'app';
134+
}
135+
}
136+
`;
137+
138+
const { program, compilerHost } = createTypescriptContext(input);
139+
const transformer = removeDecorators(
140+
() => true,
141+
() => program.getTypeChecker(),
142+
);
143+
const result = transformTypescript(undefined, [transformer], program, compilerHost);
144+
145+
expect(oneLine`${result}`).toEqual(oneLine`${output}`);
146+
});
111147
});
112148
});

0 commit comments

Comments
 (0)