Skip to content

Commit 81de9ea

Browse files
committed
fix(@ngtools/webpack): elide inlined const enum imports from rewritten import declarations
TypeScript inlines const enum usages and elides their imports. When an unused runtime value specifier was removed from the same import declaration, the declaration was rewritten, TypeScript skipped its own elision, and the const enum import was kept even though the enum has no runtime export.
1 parent 8535d7a commit 81de9ea

2 files changed

Lines changed: 113 additions & 3 deletions

File tree

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,10 @@ export function elideImports(
133133

134134
// Note: with `verbatimModuleSyntax`, TypeScript reports an error for imports of non-value aliases
135135
// that do not use a `type` modifier (TS1484/TS1485), so no special handling is needed for it.
136-
if ((symbol.flags & ts.SymbolFlags.Alias) !== 0 && !isValueAlias(symbol, typeChecker)) {
136+
if (
137+
(symbol.flags & ts.SymbolFlags.Alias) !== 0 &&
138+
!isValueAlias(symbol, typeChecker, compilerOptions)
139+
) {
137140
return 'type';
138141
}
139142

@@ -210,9 +213,14 @@ export function elideImports(
210213
/**
211214
* Determines whether an import alias resolves to a runtime value. Aliases that only resolve to
212215
* types, or that pass through a type-only import or export declaration anywhere along the alias
213-
* chain, are always elided by TypeScript.
216+
* chain, are always elided by TypeScript. The same applies to const enums, as their usages are
217+
* inlined, unless const enums are preserved.
214218
*/
215-
function isValueAlias(symbol: ts.Symbol, typeChecker: ts.TypeChecker): boolean {
219+
function isValueAlias(
220+
symbol: ts.Symbol,
221+
typeChecker: ts.TypeChecker,
222+
compilerOptions: ts.CompilerOptions,
223+
): boolean {
216224
const seen = new Set<ts.Symbol>();
217225
let current = symbol;
218226

@@ -238,5 +246,13 @@ function isValueAlias(symbol: ts.Symbol, typeChecker: ts.TypeChecker): boolean {
238246
current = next;
239247
}
240248

249+
if ((current.flags & ts.SymbolFlags.ConstEnum) !== 0) {
250+
return !!(
251+
compilerOptions.preserveConstEnums ||
252+
compilerOptions.isolatedModules ||
253+
compilerOptions.verbatimModuleSyntax
254+
);
255+
}
256+
241257
return (current.flags & ts.SymbolFlags.Value) !== 0;
242258
}

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

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,76 @@ describe('elide_imports', () => {
545545

546546
expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
547547
});
548+
549+
it('should not retain inlined const enum imports when a value specifier is removed from a declaration', () => {
550+
const input = tags.stripIndent`
551+
import { MixedEnum, MixedService } from './mixed';
552+
553+
export const value = MixedEnum.A;
554+
555+
const remove = MixedService;
556+
`;
557+
558+
const output = tags.stripIndent`
559+
export const value = 1 /* MixedEnum.A */;
560+
`;
561+
562+
const { program, compilerHost } = createTypescriptContext(
563+
input,
564+
additionalFiles,
565+
true,
566+
extraCompilerOptions,
567+
);
568+
const result = transformTypescript(undefined, [transformer(program)], program, compilerHost);
569+
570+
expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
571+
});
572+
573+
it('should retain const enum imports when a value specifier is removed and isolatedModules is enabled', () => {
574+
const input = tags.stripIndent`
575+
import { MixedEnum, MixedService } from './mixed';
576+
577+
export const value = MixedEnum.A;
578+
579+
const remove = MixedService;
580+
`;
581+
582+
const output = tags.stripIndent`
583+
import { MixedEnum } from './mixed';
584+
export const value = MixedEnum.A;
585+
`;
586+
587+
const { program, compilerHost } = createTypescriptContext(input, additionalFiles, true, {
588+
...extraCompilerOptions,
589+
isolatedModules: true,
590+
});
591+
const result = transformTypescript(undefined, [transformer(program)], program, compilerHost);
592+
593+
expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
594+
});
595+
596+
it('should retain const enum imports when a value specifier is removed and preserveConstEnums is enabled', () => {
597+
const input = tags.stripIndent`
598+
import { MixedEnum, MixedService } from './mixed';
599+
600+
export const value = MixedEnum.A;
601+
602+
const remove = MixedService;
603+
`;
604+
605+
const output = tags.stripIndent`
606+
import { MixedEnum } from './mixed';
607+
export const value = 1 /* MixedEnum.A */;
608+
`;
609+
610+
const { program, compilerHost } = createTypescriptContext(input, additionalFiles, true, {
611+
...extraCompilerOptions,
612+
preserveConstEnums: true,
613+
});
614+
const result = transformTypescript(undefined, [transformer(program)], program, compilerHost);
615+
616+
expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
617+
});
548618
});
549619

550620
it('keeps jsxFactory imports when configured', () => {
@@ -1072,6 +1142,30 @@ describe('elide_imports', () => {
10721142
expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
10731143
});
10741144

1145+
it('should not retain inlined const enum imports when a value specifier is removed from a declaration', () => {
1146+
const input = tags.stripIndent`
1147+
import { MixedEnum, MixedService } from './mixed';
1148+
1149+
export const value = MixedEnum.A;
1150+
1151+
const remove = MixedService;
1152+
`;
1153+
1154+
const output = tags.stripIndent`
1155+
export const value = 1 /* MixedEnum.A */;
1156+
`;
1157+
1158+
const { program, compilerHost } = createTypescriptContext(
1159+
input,
1160+
additionalFiles,
1161+
true,
1162+
extraCompilerOptions,
1163+
);
1164+
const result = transformTypescript(undefined, [transformer(program)], program, compilerHost);
1165+
1166+
expect(tags.oneLine`${result}`).toEqual(tags.oneLine`${output}`);
1167+
});
1168+
10751169
it('should elide imports that resolve through type-only re-exports', () => {
10761170
const input = tags.stripIndent`
10771171
import { Decorator } from './decorator';

0 commit comments

Comments
 (0)