Files
Charles Lyding ee18d57563 refactor(compiler-cli): support external modules, virtual IDs, and type-only import kinds in preprocessor
Bundler-backed AnalysisHost implementations resolve dependencies into both in-bundle modules and
bundle-external dependencies, distinguish runtime imports from type-only imports, and assign opaque
virtual module IDs that are not filesystem paths. Previously, the query engine assumed every
extracted specifier had uniform type-only semantics, treated resolved modules as in-project paths
unless they ended in .d.ts, and computed relative paths across emitted imports and debug metadata.

Classify extracted static imports, re-exports, dynamic import() expressions, and TSImportType
queries with exact ImportKind and type_only flags and forward both on ResolveRequest. Track
authoritative external module flags in FileIdInterner and QueryEngine so external modules pass
external = true to AnalysisHost::load, stay out of ProgramFiles, coordinator roots, and core
compilation, and preserve or rebase their original OwningReference specifiers instead of emitting
paths to resolved external files.

Propagate IdKind onto AnalysisResult, FileUpdate, FileInvalidation, and WASM ModuleRef entry
points. When either side of a reference or a compiled file carries IdKind::Virtual, emit the module
ID verbatim across cross-file import emission, class debug info, and HMR identifiers without path
diffing or rootDir stripping.
2026-11-19 16:40:57 -08:00

417 lines
16 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
import {outputAst as o} from '@angular/compiler';
import {ExpressionPrinter} from '../src/output_ast_printer.js';
import {TcbImportManager} from '../src/tcb.js';
import {stripIife} from '../src/compiler-utils.js';
describe('ExpressionPrinter.printType', () => {
const printer = new ExpressionPrinter('test.ts');
beforeEach(() => {
printer.reset();
});
describe('Builtin Types', () => {
it('should print boolean type', () => {
expect(printer.printType(o.BOOL_TYPE)).toBe('boolean');
});
it('should print dynamic type', () => {
expect(printer.printType(o.DYNAMIC_TYPE)).toBe('any');
});
it('should print number type', () => {
expect(printer.printType(o.INT_TYPE)).toBe('number');
expect(printer.printType(o.NUMBER_TYPE)).toBe('number');
});
it('should print string type', () => {
expect(printer.printType(o.STRING_TYPE)).toBe('string');
});
it('should print none type as never', () => {
expect(printer.printType(o.NONE_TYPE)).toBe('never');
});
it('should print function type', () => {
expect(printer.printType(o.FUNCTION_TYPE)).toBe('Function');
});
it('should print inferred type as empty', () => {
expect(printer.printType(o.INFERRED_TYPE)).toBe('');
});
});
describe('Expression Types', () => {
it('should print a simple class expression type', () => {
const type = new o.ExpressionType(o.variable('MyClass'));
expect(printer.printType(type)).toBe('MyClass');
});
it('should print an expression type with generic arguments', () => {
const myClassRef = o.importExpr({name: 'MyClass', moduleName: './my-module'});
const type = new o.ExpressionType(myClassRef, undefined, [o.STRING_TYPE, o.INT_TYPE]);
expect(printer.printType(type)).toBe('i1.MyClass<string, number>');
});
it('should print expression type with nested generic arguments', () => {
const myClassRef = o.importExpr({name: 'MyClass', moduleName: './my-module'});
const anotherClassRef = o.importExpr({name: 'AnotherClass', moduleName: './another-module'});
const type = new o.ExpressionType(myClassRef, undefined, [
new o.ExpressionType(anotherClassRef, undefined, [o.BOOL_TYPE]),
]);
expect(printer.printType(type)).toBe('i1.MyClass<i2.AnotherClass<boolean>>');
});
});
describe('Array Types', () => {
it('should print an array of builtin type', () => {
const type = new o.ArrayType(o.STRING_TYPE);
expect(printer.printType(type)).toBe('string[]');
});
it('should print an array of expression type', () => {
const type = new o.ArrayType(new o.ExpressionType(o.variable('MyClass')));
expect(printer.printType(type)).toBe('MyClass[]');
});
it('should print nested array types', () => {
const type = new o.ArrayType(new o.ArrayType(o.INT_TYPE));
expect(printer.printType(type)).toBe('number[][]');
});
});
describe('Map Types', () => {
it('should print a simple map type', () => {
const type = new o.MapType(o.STRING_TYPE);
expect(printer.printType(type)).toBe('{ [key: string]: string; }');
});
it('should print a map type with null valueType as unknown', () => {
const type = new o.MapType(null);
expect(printer.printType(type)).toBe('{ [key: string]: unknown; }');
});
it('should print a map type with expression type value', () => {
const type = new o.MapType(new o.ExpressionType(o.variable('MyClass')));
expect(printer.printType(type)).toBe('{ [key: string]: MyClass; }');
});
it('should print nested map types', () => {
const type = new o.MapType(new o.MapType(o.BOOL_TYPE));
expect(printer.printType(type)).toBe('{ [key: string]: { [key: string]: boolean; }; }');
});
});
describe('Complex Nested Structures', () => {
it('should print map of arrays of generic expressions', () => {
const myClassRef = o.importExpr({name: 'MyClass', moduleName: './my-module'});
const type = new o.MapType(
new o.ArrayType(new o.ExpressionType(myClassRef, undefined, [o.STRING_TYPE])),
);
expect(printer.printType(type)).toBe('{ [key: string]: i1.MyClass<string>[]; }');
});
it('should print generic types holding maps and arrays', () => {
const myClassRef = o.importExpr({name: 'MyClass', moduleName: './my-module'});
const type = new o.ExpressionType(myClassRef, undefined, [
new o.MapType(o.INT_TYPE),
new o.ArrayType(o.BOOL_TYPE),
]);
expect(printer.printType(type)).toBe('i1.MyClass<{ [key: string]: number; }, boolean[]>');
});
});
});
describe('stripIife', () => {
it('should unwrap arrow function with statement block body', () => {
const stmt = o.variable('x').set(o.literal(1)).toStmt();
const arrowFn = new o.ArrowFunctionExpr([], [stmt]);
const iife = new o.InvokeFunctionExpr(arrowFn, []);
const stmts = stripIife(iife);
expect(stmts).toEqual([stmt]);
});
it('should unwrap arrow function with concise expression body', () => {
const expr = o.variable('x').plus(o.literal(1));
const arrowFn = new o.ArrowFunctionExpr([], expr);
const iife = new o.InvokeFunctionExpr(arrowFn, []);
const stmts = stripIife(iife);
expect(stmts.length).toBe(1);
expect(stmts[0] instanceof o.ExpressionStatement).toBeTrue();
expect((stmts[0] as o.ExpressionStatement).expr).toBe(expr);
});
it('should unwrap regular function expression with statements', () => {
const stmt = o.variable('x').set(o.literal(2)).toStmt();
const fn = new o.FunctionExpr([], [stmt]);
const iife = new o.InvokeFunctionExpr(fn, []);
const stmts = stripIife(iife);
expect(stmts).toEqual([stmt]);
});
it('should return non-IIFE expressions wrapped as a statement', () => {
const expr = o.variable('x');
const stmts = stripIife(expr);
expect(stmts.length).toBe(1);
expect(stmts[0] instanceof o.ExpressionStatement).toBeTrue();
expect((stmts[0] as o.ExpressionStatement).expr).toBe(expr);
});
});
describe('ExpressionPrinter.print - LocalizedString', () => {
const printer = new ExpressionPrinter('test.ts');
beforeEach(() => {
printer.reset();
});
it('should print a simple LocalizedString without placeholders or metadata', () => {
const localized = new o.LocalizedString({}, [new o.LiteralPiece('Hello World', null!)], [], []);
expect(printer.print(localized)).toBe('$localize `Hello World`');
});
it('should print LocalizedString with meaning and description metadata', () => {
const localized = new o.LocalizedString(
{meaning: 'custom meaning', description: 'custom description'},
[new o.LiteralPiece('Hello World', null!)],
[],
[],
);
expect(printer.print(localized)).toBe(
'$localize `:custom meaning|custom description:Hello World`',
);
});
it('should print LocalizedString with custom ID', () => {
const localized = new o.LocalizedString(
{customId: 'customId123'},
[new o.LiteralPiece('Hello World', null!)],
[],
[],
);
expect(printer.print(localized)).toBe('$localize `:@@customId123:Hello World`');
});
it('should print LocalizedString with expressions and placeholder names', () => {
const localized = new o.LocalizedString(
{meaning: 'greeting', description: 'greeting text'},
[new o.LiteralPiece('Hello ', null!), new o.LiteralPiece('!', null!)],
[new o.PlaceholderPiece('NAME', null!)],
[o.variable('name')],
);
expect(printer.print(localized)).toBe(
'$localize `:greeting|greeting text:Hello ${name}:NAME:!`',
);
});
it('should print LocalizedString with multiple expressions', () => {
const localized = new o.LocalizedString(
{},
[
new o.LiteralPiece('Count: ', null!),
new o.LiteralPiece(' of ', null!),
new o.LiteralPiece(' items', null!),
],
[new o.PlaceholderPiece('CURRENT', null!), new o.PlaceholderPiece('TOTAL', null!)],
[o.variable('curr'), o.variable('total')],
);
expect(printer.print(localized)).toBe(
'$localize `Count: ${curr}:CURRENT: of ${total}:TOTAL: items`',
);
});
});
describe('binary operator @ts-ignore', () => {
function emitStmt(expr: o.Expression, printTypes = true): string {
const printer = new ExpressionPrinter(printTypes ? 'test.ts' : 'test.js');
return printer.printStatement(expr.toStmt(), 0).trim();
}
it('should add @ts-ignore when a numeric operator has a NotExpr operand', () => {
const expr = o.not(o.variable('a')).bigger(o.literal(1));
expect(emitStmt(expr)).toEqual('// @ts-ignore\n(!a > 1);');
});
it('should not add @ts-ignore for plain numeric comparisons', () => {
const expr = o.variable('a').bigger(o.literal(1));
expect(emitStmt(expr)).toEqual('(a > 1);');
});
it('should add @ts-ignore when nullish coalescing has a non-nullish left-hand-side', () => {
const concat = o.literal('prefix-').plus(o.variable('a'));
const expr = new o.ParenthesizedExpr(concat).nullishCoalesce(o.literal(''));
expect(emitStmt(expr)).toEqual(`// @ts-ignore\n(('prefix-' + a) ?? '');`);
});
it('should not add @ts-ignore when nullish coalescing has a nullable left-hand-side', () => {
expect(emitStmt(o.variable('a').nullishCoalesce(o.literal('')))).toEqual(`(a ?? '');`);
expect(emitStmt(o.variable('a').and(o.variable('b')).nullishCoalesce(o.literal('')))).toEqual(
`((a && b) ?? '');`,
);
});
it('should add @ts-ignore when comparing a NotExpr with a non-boolean literal', () => {
const expr = o.not(o.variable('a')).identical(o.literal('oh no'));
expect(emitStmt(expr)).toEqual(`// @ts-ignore\n(!a === 'oh no');`);
});
it('should not add @ts-ignore when comparing a NotExpr with a boolean literal', () => {
const expr = o.not(o.variable('a')).identical(o.literal(false));
expect(emitStmt(expr)).toEqual('(!a === false);');
});
it('should not add @ts-ignore when printTypes is false', () => {
const expr = o.not(o.variable('a')).bigger(o.literal(1));
expect(emitStmt(expr, /* printTypes */ false)).toEqual('(!a > 1);');
});
it('should add at most one @ts-ignore per line when nested expressions match', () => {
const lhs = o.not(o.variable('a')).bigger(o.literal(1));
const rhs = o.not(o.variable('b')).bigger(o.literal(2));
expect(emitStmt(lhs.plus(rhs))).toEqual('// @ts-ignore\n((!a > 1) + (!b > 2));');
});
it('should add @ts-ignore when comparing typeof with an invalid typeof literal', () => {
const invalid = o.typeofExpr(o.variable('a')).identical(o.literal('foobar'));
expect(emitStmt(invalid)).toEqual(`// @ts-ignore\n(typeof a === 'foobar');`);
const valid = o.typeofExpr(o.variable('a')).identical(o.literal('object'));
expect(emitStmt(valid)).toEqual(`(typeof a === 'object');`);
});
it('should add @ts-ignore for invalid in binary expressions with literals', () => {
const inExpr = new o.BinaryOperatorExpr(
o.BinaryOperator.In,
o.literal('foo'),
o.literal('foobar'),
);
expect(emitStmt(inExpr)).toEqual(`// @ts-ignore\n('foo' in 'foobar');`);
});
it('should add @ts-ignore for invalid instanceof binary expressions with literals', () => {
const instanceOfExpr = new o.BinaryOperatorExpr(
o.BinaryOperator.InstanceOf,
o.literal('foo'),
o.variable('Bar'),
);
expect(emitStmt(instanceOfExpr)).toEqual(`// @ts-ignore\n('foo' instanceof Bar);`);
});
});
describe('ExpressionPrinter @angular/core import rewriting', () => {
const defineInjectable = o.importExpr({name: 'ɵɵdefineInjectable', moduleName: '@angular/core'});
const setClassMetadata = o.importExpr({name: 'ɵsetClassMetadata', moduleName: '@angular/core'});
it('should import @angular/core as is outside of core', () => {
const printer = new ExpressionPrinter('app/service.ts');
expect(printer.print(defineInjectable)).toBe('i0.ɵɵdefineInjectable');
expect(printer.print(setClassMetadata)).toBe('i0.ɵsetClassMetadata');
expect(printer.getImportStatements()).toBe(
"// @ts-ignore\nimport * as i0 from '@angular/core';",
);
});
it('should redirect the import to r3_symbols.ts and keep the namespace alias', () => {
const printer = new ExpressionPrinter('core/src/di/injectable.ts', '../r3_symbols');
expect(printer.print(defineInjectable)).toBe('i0.ɵɵdefineInjectable');
expect(printer.getImportStatements()).toBe(
"// @ts-ignore\nimport * as i0 from '../r3_symbols';",
);
});
it('should rename symbols to their r3_symbols.ts export name', () => {
const printer = new ExpressionPrinter('core/src/di/injectable.ts', '../r3_symbols');
expect(printer.print(setClassMetadata)).toBe('i0.setClassMetadata');
const ngModuleFactory = o.importExpr({name: 'ɵNgModuleFactory', moduleName: '@angular/core'});
expect(printer.print(ngModuleFactory)).toBe('i0.NgModuleFactory');
});
it('should rewrite symbols in type position as well', () => {
const printer = new ExpressionPrinter('core/src/di/injectable.ts', '../r3_symbols');
const declaration = o.importExpr({
name: 'ɵɵInjectableDeclaration',
moduleName: '@angular/core',
});
const type = new o.ExpressionType(declaration, undefined, [
new o.ExpressionType(o.variable('MyService')),
]);
expect(printer.printType(type)).toBe('i0.ɵɵInjectableDeclaration<MyService>');
expect(printer.getImportStatements()).toBe(
"// @ts-ignore\nimport * as i0 from '../r3_symbols';",
);
});
it('should reject symbols that core is not allowed to import', () => {
const printer = new ExpressionPrinter('core/src/di/injectable.ts', '../r3_symbols');
const defineComponent = o.importExpr({name: 'ɵɵdefineComponent', moduleName: '@angular/core'});
expect(() => printer.print(defineComponent)).toThrowError(
'Importing unexpected symbol ɵɵdefineComponent while compiling @angular/core',
);
});
it('should leave imports of other modules alone', () => {
const printer = new ExpressionPrinter('core/src/di/injectable.ts', '../r3_symbols');
const injector = o.importExpr({name: 'Injector', moduleName: './injector'});
expect(printer.print(injector)).toBe('i1.Injector');
expect(printer.getImportStatements()).toBe("// @ts-ignore\nimport * as i1 from './injector';");
});
it('should avoid conflicting names in namespaceAliasConflicts without cross-prefix overlap', () => {
const printer = new ExpressionPrinter('app/service.ts', null, new Set(['i0', 'i0_1', 'i1']));
const modA = o.importExpr({name: 'A', moduleName: './a'});
const modB = o.importExpr({name: 'B', moduleName: './b'});
expect(printer.print(defineInjectable)).toBe('i0_2.ɵɵdefineInjectable');
expect(printer.print(modA)).toBe('i1_1.A');
expect(printer.print(modB)).toBe('i2.B');
expect(printer.getImportStatements()).toBe(
[
"// @ts-ignore\nimport * as i0_2 from '@angular/core';",
"// @ts-ignore\nimport * as i1_1 from './a';",
"// @ts-ignore\nimport * as i2 from './b';",
].join('\n'),
);
});
});
describe('TcbImportManager', () => {
it('avoids namespaceAliasConflicts when allocating aliases', () => {
const imports = new TcbImportManager(new Set(['i0', 'i0_1', 'i1']));
expect(imports.addImport('./a')).toBe('i0_2');
expect(imports.addImport('./b')).toBe('i1_1');
expect(imports.addImport('./c')).toBe('i2');
expect(imports.addImport('./a')).toBe('i0_2');
expect(imports.generateImportStatements()).toBe(
"import * as i0_2 from './a';\nimport * as i1_1 from './b';\nimport * as i2 from './c';\n",
);
});
it('escapes specifiers that contain a backslash or a quote', () => {
const imports = new TcbImportManager();
imports.addImport("/@id/it's\\virtual.ts");
expect(imports.generateImportStatements()).toBe(
"import * as i0 from '/@id/it\\'s\\\\virtual.ts';\n",
);
});
});
describe('ExpressionPrinter.getImportStatements', () => {
it('escapes specifiers that contain a backslash or a quote', () => {
const printer = new ExpressionPrinter('test.ts');
printer.reset();
const namespace = printer.getOrCreateNamespace("/@id/it's\\virtual.ts");
printer.addSideEffectImport('/@id/side\\effect.ts');
expect(printer.getImportStatements()).toBe(
`// @ts-ignore\nimport * as ${namespace} from '/@id/it\\'s\\\\virtual.ts';\n` +
"// @ts-ignore\nimport '/@id/side\\\\effect.ts';",
);
});
});