mirror of
https://github.com/angular/angular.git
synced 2026-11-24 15:37:51 -05:00
refactor(compiler-cli): don't use FakeEnvironment for tcb tests (#41268)
For the tests in //packages/compiler-cli/src/ngtsc/typecheck, this commits uses a `TypeCheckFile` for the environment, rather than a `FakeEnvironment`. Using a real environment gives us more flexibility with testing. PR Close #41268
This commit is contained in:
@@ -385,7 +385,8 @@ export class TypeCheckContextImpl implements TypeCheckContext {
|
||||
path: pendingShimData.file.fileName,
|
||||
templates: pendingShimData.templates,
|
||||
});
|
||||
updates.set(pendingShimData.file.fileName, pendingShimData.file.render());
|
||||
updates.set(
|
||||
pendingShimData.file.fileName, pendingShimData.file.render(false /* removeComments */));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -49,12 +49,12 @@ export class TypeCheckFile extends Environment {
|
||||
this.tcbStatements.push(fn);
|
||||
}
|
||||
|
||||
render(): string {
|
||||
render(removeComments: boolean): string {
|
||||
let source: string = this.importManager.getAllImports(this.contextFile.fileName)
|
||||
.map(i => `import * as ${i.qualifier.text} from '${i.specifier}';`)
|
||||
.join('\n') +
|
||||
'\n\n';
|
||||
const printer = ts.createPrinter();
|
||||
const printer = ts.createPrinter({removeComments});
|
||||
source += '\n';
|
||||
for (const stmt of this.pipeInstStatements) {
|
||||
source += printer.printNode(ts.EmitHint.Unspecified, stmt, this.contextFile) + '\n';
|
||||
|
||||
@@ -6,9 +6,12 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {initMockFileSystem} from '../../file_system/testing';
|
||||
import {tcb, TestDeclaration} from './test_utils';
|
||||
|
||||
describe('type check blocks diagnostics', () => {
|
||||
beforeEach(() => initMockFileSystem('Native'));
|
||||
|
||||
describe('parse spans', () => {
|
||||
it('should annotate unary ops', () => {
|
||||
expect(tcbWithSpans('{{ -a }}')).toContain('(-((ctx).a /*4,5*/) /*4,5*/) /*3,5*/');
|
||||
@@ -147,8 +150,9 @@ describe('type check blocks diagnostics', () => {
|
||||
pipeName: 'test',
|
||||
}];
|
||||
const block = tcbWithSpans(TEMPLATE, PIPES);
|
||||
expect(block).toContain('var _pipe1: i0.TestPipe = (null!)');
|
||||
expect(block).toContain(
|
||||
'((null as TestPipe).transform /*7,11*/(((ctx).a /*3,4*/) /*3,4*/, ((ctx).b /*12,13*/) /*12,13*/) /*3,13*/);');
|
||||
'(_pipe1.transform /*7,11*/(((ctx).a /*3,4*/) /*3,4*/, ((ctx).b /*12,13*/) /*12,13*/) /*3,13*/);');
|
||||
});
|
||||
|
||||
describe('attaching multiple comments for multiple references', () => {
|
||||
|
||||
@@ -11,7 +11,7 @@ import * as ts from 'typescript';
|
||||
|
||||
import {absoluteFrom, AbsoluteFsPath, getSourceFileOrError, LogicalFileSystem} from '../../file_system';
|
||||
import {TestFile} from '../../file_system/testing';
|
||||
import {AbsoluteModuleStrategy, LocalIdentifierStrategy, LogicalProjectStrategy, ModuleResolver, Reexport, Reference, ReferenceEmitter} from '../../imports';
|
||||
import {AbsoluteModuleStrategy, LocalIdentifierStrategy, LogicalProjectStrategy, ModuleResolver, Reexport, Reference, ReferenceEmitter, RelativePathStrategy} from '../../imports';
|
||||
import {NOOP_INCREMENTAL_BUILD} from '../../incremental';
|
||||
import {ClassPropertyMapping, CompoundMetadataReader} from '../../metadata';
|
||||
import {ClassDeclaration, isNamedClassDeclaration, TypeScriptReflectionHost} from '../../reflection';
|
||||
@@ -28,6 +28,7 @@ import {Environment} from '../src/environment';
|
||||
import {OutOfBandDiagnosticRecorder} from '../src/oob';
|
||||
import {TypeCheckShimGenerator} from '../src/shim';
|
||||
import {generateTypeCheckBlock} from '../src/type_check_block';
|
||||
import {TypeCheckFile} from '../src/type_check_file';
|
||||
|
||||
export function typescriptLibDts(): TestFile {
|
||||
return {
|
||||
@@ -201,6 +202,7 @@ export type TestDirective = Partial<Pick<
|
||||
coercedInputFields?: string[], restrictedInputFields?: string[],
|
||||
stringLiteralInputFields?: string[], undeclaredInputFields?: string[], isGeneric?: boolean;
|
||||
};
|
||||
|
||||
export type TestPipe = {
|
||||
name: string,
|
||||
file?: AbsoluteFsPath, pipeName: string, type: 'pipe',
|
||||
@@ -212,9 +214,14 @@ export function tcb(
|
||||
template: string, declarations: TestDeclaration[] = [], config?: TypeCheckingConfig,
|
||||
options?: {emitSpans?: boolean}): string {
|
||||
const classes = ['Test', ...declarations.map(decl => decl.name)];
|
||||
const code = classes.map(name => `class ${name}<T extends string> {}`).join('\n');
|
||||
const code = classes.map(name => `export class ${name}<T extends string> {}`).join('\n');
|
||||
|
||||
const sf = ts.createSourceFile('synthetic.ts', code, ts.ScriptTarget.Latest, true);
|
||||
const rootFilePath = absoluteFrom('/synthetic.ts');
|
||||
const {program, host} = makeProgram([
|
||||
{name: rootFilePath, contents: code, isRoot: true},
|
||||
]);
|
||||
|
||||
const sf = getSourceFileOrError(program, rootFilePath);
|
||||
const clazz = getClass(sf, 'Test');
|
||||
const templateUrl = 'synthetic.html';
|
||||
const {nodes} = parseTemplate(template, templateUrl);
|
||||
@@ -251,13 +258,25 @@ export function tcb(
|
||||
emitSpans: false,
|
||||
};
|
||||
|
||||
const tcb = generateTypeCheckBlock(
|
||||
FakeEnvironment.newFake(config), new Reference(clazz), ts.createIdentifier('Test_TCB'), meta,
|
||||
new NoopSchemaChecker(), new NoopOobRecorder());
|
||||
const fileName = absoluteFrom('/type-check-file.ts');
|
||||
|
||||
const removeComments = !options.emitSpans;
|
||||
const res = ts.createPrinter({removeComments}).printNode(ts.EmitHint.Unspecified, tcb, sf);
|
||||
return res.replace(/\s+/g, ' ');
|
||||
const reflectionHost = new TypeScriptReflectionHost(program.getTypeChecker());
|
||||
|
||||
const refEmmiter: ReferenceEmitter = new ReferenceEmitter(
|
||||
[new LocalIdentifierStrategy(), new RelativePathStrategy(reflectionHost)]);
|
||||
|
||||
const env = new TypeCheckFile(fileName, config, refEmmiter, reflectionHost, host);
|
||||
|
||||
const ref = new Reference(clazz);
|
||||
|
||||
const tcb = generateTypeCheckBlock(
|
||||
env, ref, ts.createIdentifier('Test_TCB'), meta, new NoopSchemaChecker(),
|
||||
new NoopOobRecorder());
|
||||
|
||||
env.addTypeCheckBlock(ref, meta, new NoopSchemaChecker(), new NoopOobRecorder());
|
||||
|
||||
const rendered = env.render(!options.emitSpans /* removeComments */);
|
||||
return rendered.replace(/\s+/g, ' ');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -43,7 +43,7 @@ runInEachFileSystem(() => {
|
||||
const file = new TypeCheckFile(
|
||||
_('/_typecheck_.ts'), ALL_ENABLED_CONFIG, new ReferenceEmitter([]),
|
||||
/* reflector */ null!, host);
|
||||
const sf = file.render();
|
||||
const sf = file.render(false /* removeComments */);
|
||||
expect(sf).toContain('export const IS_A_MODULE = true;');
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user