Skip to content

Commit c267c68

Browse files
zarendmhevery
authored andcommitted
refactor(compiler-cli): don't use FakeEnvironment for tcb tests (angular#41043)
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 angular#41043
1 parent 02e8901 commit c267c68

File tree

6 files changed

+95
-62
lines changed

6 files changed

+95
-62
lines changed

packages/compiler-cli/src/ngtsc/typecheck/src/context.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,8 @@ export class TypeCheckContextImpl implements TypeCheckContext {
385385
path: pendingShimData.file.fileName,
386386
templates: pendingShimData.templates,
387387
});
388-
updates.set(pendingShimData.file.fileName, pendingShimData.file.render());
388+
updates.set(
389+
pendingShimData.file.fileName, pendingShimData.file.render(false /* removeComments */));
389390
}
390391
}
391392

packages/compiler-cli/src/ngtsc/typecheck/src/type_check_file.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ export class TypeCheckFile extends Environment {
4949
this.tcbStatements.push(fn);
5050
}
5151

52-
render(): string {
52+
render(removeComments: boolean): string {
5353
let source: string = this.importManager.getAllImports(this.contextFile.fileName)
5454
.map(i => `import * as ${i.qualifier.text} from '${i.specifier}';`)
5555
.join('\n') +
5656
'\n\n';
57-
const printer = ts.createPrinter();
57+
const printer = ts.createPrinter({removeComments});
5858
source += '\n';
5959
for (const stmt of this.pipeInstStatements) {
6060
source += printer.printNode(ts.EmitHint.Unspecified, stmt, this.contextFile) + '\n';

packages/compiler-cli/src/ngtsc/typecheck/test/span_comments_spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import {initMockFileSystem} from '../../file_system/testing';
910
import {tcb, TestDeclaration} from './test_utils';
1011

1112
describe('type check blocks diagnostics', () => {
13+
beforeEach(() => initMockFileSystem('Native'));
14+
1215
describe('parse spans', () => {
1316
it('should annotate unary ops', () => {
1417
expect(tcbWithSpans('{{ -a }}')).toContain('(-((ctx).a /*4,5*/) /*4,5*/) /*3,5*/');
@@ -146,8 +149,9 @@ describe('type check blocks diagnostics', () => {
146149
pipeName: 'test',
147150
}];
148151
const block = tcbWithSpans(TEMPLATE, PIPES);
152+
expect(block).toContain('var _pipe1: i0.TestPipe = null!');
149153
expect(block).toContain(
150-
'((null as TestPipe).transform /*7,11*/(((ctx).a /*3,4*/) /*3,4*/, ((ctx).b /*12,13*/) /*12,13*/) /*3,13*/);');
154+
'(_pipe1.transform /*7,11*/(((ctx).a /*3,4*/) /*3,4*/, ((ctx).b /*12,13*/) /*12,13*/) /*3,13*/);');
151155
});
152156

153157
describe('attaching multiple comments for multiple references', () => {

packages/compiler-cli/src/ngtsc/typecheck/test/test_utils.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import * as ts from 'typescript';
1111

1212
import {absoluteFrom, AbsoluteFsPath, getSourceFileOrError, LogicalFileSystem} from '../../file_system';
1313
import {TestFile} from '../../file_system/testing';
14-
import {AbsoluteModuleStrategy, LocalIdentifierStrategy, LogicalProjectStrategy, ModuleResolver, Reexport, Reference, ReferenceEmitter} from '../../imports';
14+
import {AbsoluteModuleStrategy, LocalIdentifierStrategy, LogicalProjectStrategy, ModuleResolver, Reexport, Reference, ReferenceEmitter, RelativePathStrategy} from '../../imports';
1515
import {NOOP_INCREMENTAL_BUILD} from '../../incremental';
1616
import {ClassPropertyMapping, CompoundMetadataReader} from '../../metadata';
1717
import {ClassDeclaration, isNamedClassDeclaration, TypeScriptReflectionHost} from '../../reflection';
@@ -28,6 +28,7 @@ import {Environment} from '../src/environment';
2828
import {OutOfBandDiagnosticRecorder} from '../src/oob';
2929
import {TypeCheckShimGenerator} from '../src/shim';
3030
import {generateTypeCheckBlock} from '../src/type_check_block';
31+
import {TypeCheckFile} from '../src/type_check_file';
3132

3233
export function typescriptLibDts(): TestFile {
3334
return {
@@ -201,6 +202,7 @@ export type TestDirective = Partial<Pick<
201202
coercedInputFields?: string[], restrictedInputFields?: string[],
202203
stringLiteralInputFields?: string[], undeclaredInputFields?: string[], isGeneric?: boolean;
203204
};
205+
204206
export type TestPipe = {
205207
name: string,
206208
file?: AbsoluteFsPath, pipeName: string, type: 'pipe',
@@ -212,9 +214,14 @@ export function tcb(
212214
template: string, declarations: TestDeclaration[] = [], config?: TypeCheckingConfig,
213215
options?: {emitSpans?: boolean}): string {
214216
const classes = ['Test', ...declarations.map(decl => decl.name)];
215-
const code = classes.map(name => `class ${name}<T extends string> {}`).join('\n');
217+
const code = classes.map(name => `export class ${name}<T extends string> {}`).join('\n');
218+
219+
const rootFilePath = absoluteFrom('/synthetic.ts');
220+
const {program, host} = makeProgram([
221+
{name: rootFilePath, contents: code, isRoot: true},
222+
]);
216223

217-
const sf = ts.createSourceFile('synthetic.ts', code, ts.ScriptTarget.Latest, true);
224+
const sf = getSourceFileOrError(program, rootFilePath);
218225
const clazz = getClass(sf, 'Test');
219226
const templateUrl = 'synthetic.html';
220227
const {nodes} = parseTemplate(template, templateUrl);
@@ -251,13 +258,25 @@ export function tcb(
251258
emitSpans: false,
252259
};
253260

261+
const fileName = absoluteFrom('/type-check-file.ts');
262+
263+
const reflectionHost = new TypeScriptReflectionHost(program.getTypeChecker());
264+
265+
const refEmmiter: ReferenceEmitter = new ReferenceEmitter(
266+
[new LocalIdentifierStrategy(), new RelativePathStrategy(reflectionHost)]);
267+
268+
const env = new TypeCheckFile(fileName, config, refEmmiter, reflectionHost, host);
269+
270+
const ref = new Reference(clazz);
271+
254272
const tcb = generateTypeCheckBlock(
255-
FakeEnvironment.newFake(config), new Reference(clazz), ts.createIdentifier('Test_TCB'), meta,
256-
new NoopSchemaChecker(), new NoopOobRecorder());
273+
env, ref, ts.createIdentifier('Test_TCB'), meta, new NoopSchemaChecker(),
274+
new NoopOobRecorder());
275+
276+
env.addTypeCheckBlock(ref, meta, new NoopSchemaChecker(), new NoopOobRecorder());
257277

258-
const removeComments = !options.emitSpans;
259-
const res = ts.createPrinter({removeComments}).printNode(ts.EmitHint.Unspecified, tcb, sf);
260-
return res.replace(/\s+/g, ' ');
278+
const rendered = env.render(!options.emitSpans /* removeComments */);
279+
return rendered.replace(/\s+/g, ' ');
261280
}
262281

263282
/**

0 commit comments

Comments
 (0)