Skip to content

Commit

Permalink
milestone 1; save work
Browse files Browse the repository at this point in the history
  • Loading branch information
zarend committed Mar 2, 2021
1 parent 3370ade commit 9ba69df
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 60 deletions.
Expand Up @@ -49,12 +49,12 @@ export class TypeCheckFile extends Environment {
this.tcbStatements.push(fn);
}

render(): string {
render(options?: {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(options);
source += '\n';
for (const stmt of this.pipeInstStatements) {
source += printer.printNode(ts.EmitHint.Unspecified, stmt, this.contextFile) + '\n';
Expand Down
Expand Up @@ -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*/');
Expand Down Expand Up @@ -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', () => {
Expand Down
35 changes: 27 additions & 8 deletions packages/compiler-cli/src/ngtsc/typecheck/test/test_utils.ts
Expand Up @@ -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';
Expand All @@ -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 {
Expand Down Expand Up @@ -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',
Expand All @@ -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 rootFilePath = absoluteFrom('/synthetic.ts');
const {program, host} = makeProgram([
{name: rootFilePath, contents: code, isRoot: true},
]);

const sf = ts.createSourceFile('synthetic.ts', code, ts.ScriptTarget.Latest, true);
const sf = getSourceFileOrError(program, rootFilePath);
const clazz = getClass(sf, 'Test');
const templateUrl = 'synthetic.html';
const {nodes} = parseTemplate(template, templateUrl);
Expand Down Expand Up @@ -251,13 +258,25 @@ export function tcb(
emitSpans: false,
};

const fileName = absoluteFrom('/type-check-file.ts');

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(
FakeEnvironment.newFake(config), new Reference(clazz), ts.createIdentifier('Test_TCB'), meta,
new NoopSchemaChecker(), new NoopOobRecorder());
env, ref, ts.createIdentifier('Test_TCB'), meta, new NoopSchemaChecker(),
new NoopOobRecorder());

env.addTypeCheckBlock(ref, meta, new NoopSchemaChecker(), new NoopOobRecorder());

const removeComments = !options.emitSpans;
const res = ts.createPrinter({removeComments}).printNode(ts.EmitHint.Unspecified, tcb, sf);
return res.replace(/\s+/g, ' ');
const rendered = env.render({removeComments: !options.emitSpans});
return rendered.replace(/\s+/g, ' ');
}

/**
Expand Down

0 comments on commit 9ba69df

Please sign in to comment.