Skip to content

Commit 42edc49

Browse files
alan-agius4Keen Yee Liau
authored and
Keen Yee Liau
committed
fix(@ngtools/webpack): elide imports for implements keywords
Running the `remove-ivy-jit-support-calls` and `remove_decorators` transformers causes the following TS bug microsoft/TypeScript#17552 which is why the `elide-imports` transformer exists in the first place. However, when having a syntax like the below; ```ts import { AccountComponentChild } from '../@types'; export class SignUpComponent implements AccountComponentChild{} ``` The `implements` parts of the class is called a `HeritageClause` with child statements of `ExpressionWithTypeArguments` also the same is for `abstract`. With this change we check the token of the `HeritageClause` and if it's an `ImplementsKeyword` we elide the import. Closes #16907
1 parent 26f2fe1 commit 42edc49

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ export function elideImports(
3939
return;
4040
}
4141

42+
// Consider types for 'implements' as unused.
43+
// A HeritageClause token can also be an 'AbstractKeyword'
44+
// which in that case we should not elide the import.
45+
if (ts.isHeritageClause(node) && node.token === ts.SyntaxKind.ImplementsKeyword) {
46+
return;
47+
}
48+
4249
// Record import and skip
4350
if (ts.isImportDeclaration(node)) {
4451
imports.push(node);

packages/ngtools/webpack/src/transformers/elide_imports_spec.ts

+21
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,27 @@ describe('@ngtools/webpack transformers', () => {
317317
});
318318
});
319319

320+
it(`should remove import for 'ExpressionWithTypeArguments' implements token`, () => {
321+
const input = tags.stripIndent`
322+
import { Bar, Buz, Unused } from './bar';
323+
324+
export class Foo extends Bar implements Buz { }
325+
326+
${dummyNode}
327+
`;
328+
329+
const output = tags.stripIndent`
330+
import { Bar } from './bar';
331+
332+
export class Foo extends Bar { }
333+
`;
334+
335+
const { program, compilerHost } = createTypescriptContext(input);
336+
const result = transformTypescript(undefined, [transformer(program)], program, compilerHost);
337+
338+
expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
339+
});
340+
320341
describe('should not elide imports decorator type references when emitDecoratorMetadata is true', () => {
321342
const extraCompilerOptions: ts.CompilerOptions = {
322343
emitDecoratorMetadata: true,

0 commit comments

Comments
 (0)