Skip to content
Open
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
22 changes: 19 additions & 3 deletions packages/ngtools/webpack/src/transformers/elide_imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ export function elideImports(

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

Expand Down Expand Up @@ -210,9 +213,14 @@ export function elideImports(
/**
* Determines whether an import alias resolves to a runtime value. Aliases that only resolve to
* types, or that pass through a type-only import or export declaration anywhere along the alias
* chain, are always elided by TypeScript.
* chain, are always elided by TypeScript. The same applies to const enums, whose references are
* inlined, unless `preserveConstEnums`, `isolatedModules` or `verbatimModuleSyntax` keeps them.
*/
function isValueAlias(symbol: ts.Symbol, typeChecker: ts.TypeChecker): boolean {
function isValueAlias(
symbol: ts.Symbol,
typeChecker: ts.TypeChecker,
compilerOptions: ts.CompilerOptions,
): boolean {
const seen = new Set<ts.Symbol>();
let current = symbol;

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

if ((current.flags & ts.SymbolFlags.ConstEnum) !== 0) {
return !!(
compilerOptions.preserveConstEnums ||
compilerOptions.isolatedModules ||
compilerOptions.verbatimModuleSyntax
);
}
Comment on lines +249 to +255

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

If a const enum is merged with another value-carrying declaration (such as a namespace), the symbol will have both ts.SymbolFlags.ConstEnum and other value flags (e.g., ts.SymbolFlags.ValueModule).\n\nIn the current implementation, if ts.SymbolFlags.ConstEnum is present, the function immediately returns based on the compiler options, ignoring any other merged value flags. This will cause the import to be elided even if the merged value component (like the namespace) is used at runtime, leading to a runtime ReferenceError.\n\nTo prevent this, we should only apply the const enum elision logic if the symbol does not contain any other value flags.

  if (\n    (current.flags & ts.SymbolFlags.ConstEnum) !== 0 &&\n    (current.flags & (ts.SymbolFlags.Value & ~ts.SymbolFlags.ConstEnum)) === 0\n  ) {\n    return !!(\n      compilerOptions.preserveConstEnums ||\n      compilerOptions.isolatedModules ||\n      compilerOptions.verbatimModuleSyntax\n    );\n  }


return (current.flags & ts.SymbolFlags.Value) !== 0;
}
26 changes: 26 additions & 0 deletions packages/ngtools/webpack/src/transformers/elide_imports_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,32 @@ describe('elide_imports', () => {

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

it('should not retain inlined const enum imports when a declaration is rewritten to remove an unused value import', () => {
const input = tags.stripIndent`
import { MixedEnum, MixedService } from './mixed';

export const value = MixedEnum.A;
export let service: MixedService | undefined;

${dummyNode}
`;

const output = tags.stripIndent`
export const value = 1 /* MixedEnum.A */;
export let service;
`;

const { program, compilerHost } = createTypescriptContext(
input,
additionalFiles,
true,
extraCompilerOptions,
);
const result = transformTypescript(undefined, [transformer(program)], program, compilerHost);

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

it('keeps jsxFactory imports when configured', () => {
Expand Down