/
test_utils.ts
391 lines (345 loc) Β· 13.4 KB
/
test_utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/**
* @license
* Copyright Google Inc. 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.io/license
*/
import {CssSelector, ParseSourceFile, ParseSourceSpan, R3TargetBinder, SchemaMetadata, SelectorMatcher, TmplAstElement, TmplAstReference, Type, parseTemplate} from '@angular/compiler';
import * as ts from 'typescript';
import {AbsoluteFsPath, LogicalFileSystem, absoluteFrom} from '../../file_system';
import {TestFile} from '../../file_system/testing';
import {AbsoluteModuleStrategy, LocalIdentifierStrategy, LogicalProjectStrategy, Reference, ReferenceEmitter} from '../../imports';
import {ClassDeclaration, TypeScriptReflectionHost, isNamedClassDeclaration} from '../../reflection';
import {makeProgram} from '../../testing';
import {getRootDirs} from '../../util/src/typescript';
import {TemplateSourceMapping, TypeCheckBlockMetadata, TypeCheckableDirectiveMeta, TypeCheckingConfig} from '../src/api';
import {TypeCheckContext} from '../src/context';
import {DomSchemaChecker} from '../src/dom';
import {Environment} from '../src/environment';
import {OutOfBandDiagnosticRecorder} from '../src/oob';
import {generateTypeCheckBlock} from '../src/type_check_block';
export function typescriptLibDts(): TestFile {
return {
name: absoluteFrom('/lib.d.ts'),
contents: `
type Partial<T> = { [P in keyof T]?: T[P]; };
type Pick<T, K extends keyof T> = { [P in K]: T[P]; };
type NonNullable<T> = T extends null | undefined ? never : T;
// The following native type declarations are required for proper type inference
declare interface Function {
call(...args: any[]): any;
}
declare interface Array<T> {
length: number;
}
declare interface String {
length: number;
}
declare interface Event {
preventDefault(): void;
}
declare interface MouseEvent extends Event {
readonly x: number;
readonly y: number;
}
declare interface HTMLElementEventMap {
"click": MouseEvent;
}
declare interface HTMLElement {
addEventListener<K extends keyof HTMLElementEventMap>(type: K, listener: (this: HTMLElement, ev: HTMLElementEventMap[K]) => any): void;
addEventListener(type: string, listener: (evt: Event): void;): void;
}
declare interface HTMLDivElement extends HTMLElement {}
declare interface HTMLImageElement extends HTMLElement {
src: string;
alt: string;
width: number;
height: number;
}
declare interface HTMLQuoteElement extends HTMLElement {
cite: string;
}
declare interface HTMLElementTagNameMap {
"blockquote": HTMLQuoteElement;
"div": HTMLDivElement;
"img": HTMLImageElement;
}
declare interface Document {
createElement<K extends keyof HTMLElementTagNameMap>(tagName: K): HTMLElementTagNameMap[K];
createElement(tagName: string): HTMLElement;
}
declare const document: Document;
`
};
}
export function angularCoreDts(): TestFile {
return {
name: absoluteFrom('/node_modules/@angular/core/index.d.ts'),
contents: `
export declare class TemplateRef<C> {
abstract readonly elementRef: unknown;
abstract createEmbeddedView(context: C): unknown;
}
export declare class EventEmitter<T> {
subscribe(generatorOrNext?: any, error?: any, complete?: any): unknown;
}
`
};
}
export function angularAnimationsDts(): TestFile {
return {
name: absoluteFrom('/node_modules/@angular/animations/index.d.ts'),
contents: `
export declare class AnimationEvent {
element: any;
}
`
};
}
export function ngForDeclaration(): TestDeclaration {
return {
type: 'directive',
file: absoluteFrom('/ngfor.d.ts'),
selector: '[ngForOf]',
name: 'NgForOf',
inputs: {ngForOf: 'ngForOf'},
hasNgTemplateContextGuard: true,
};
}
export function ngForDts(): TestFile {
return {
name: absoluteFrom('/ngfor.d.ts'),
contents: `
export declare class NgForOf<T> {
ngForOf: T[];
ngForTrackBy: TrackByFunction<T>;
static ngTemplateContextGuard<T>(dir: NgForOf<T>, ctx: any): ctx is NgForOfContext<T>;
}
export interface TrackByFunction<T> {
(index: number, item: T): any;
}
export declare class NgForOfContext<T> {
$implicit: T;
index: number;
count: number;
readonly odd: boolean;
readonly even: boolean;
readonly first: boolean;
readonly last: boolean;
}`,
};
}
export const ALL_ENABLED_CONFIG: TypeCheckingConfig = {
applyTemplateContextGuards: true,
checkQueries: false,
checkTemplateBodies: true,
checkTypeOfInputBindings: true,
strictNullInputBindings: true,
checkTypeOfAttributes: true,
// Feature is still in development.
// TODO(alxhub): enable when DOM checking via lib.dom.d.ts is further along.
checkTypeOfDomBindings: false,
checkTypeOfOutputEvents: true,
checkTypeOfAnimationEvents: true,
checkTypeOfDomEvents: true,
checkTypeOfDomReferences: true,
checkTypeOfNonDomReferences: true,
checkTypeOfPipes: true,
strictSafeNavigationTypes: true,
};
// Remove 'ref' from TypeCheckableDirectiveMeta and add a 'selector' instead.
export type TestDirective =
Partial<Pick<
TypeCheckableDirectiveMeta,
Exclude<keyof TypeCheckableDirectiveMeta, 'ref'|'coercedInputFields'>>>&
{
selector: string,
name: string, file?: AbsoluteFsPath,
type: 'directive', coercedInputFields?: string[],
};
export type TestPipe = {
name: string,
file?: AbsoluteFsPath,
pipeName: string,
type: 'pipe',
};
export type TestDeclaration = TestDirective | TestPipe;
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 sf = ts.createSourceFile('synthetic.ts', code, ts.ScriptTarget.Latest, true);
const clazz = getClass(sf, 'Test');
const templateUrl = 'synthetic.html';
const {nodes} = parseTemplate(template, templateUrl);
const {matcher, pipes} = prepareDeclarations(declarations, decl => getClass(sf, decl.name));
const binder = new R3TargetBinder(matcher);
const boundTarget = binder.bind({template: nodes});
const meta: TypeCheckBlockMetadata = {boundTarget, pipes, id: 'tcb', schemas: []};
config = config || {
applyTemplateContextGuards: true,
checkQueries: false,
checkTypeOfInputBindings: true,
strictNullInputBindings: true,
checkTypeOfAttributes: true,
checkTypeOfDomBindings: false,
checkTypeOfOutputEvents: true,
checkTypeOfAnimationEvents: true,
checkTypeOfDomEvents: true,
checkTypeOfDomReferences: true,
checkTypeOfNonDomReferences: true,
checkTypeOfPipes: true,
checkTemplateBodies: true,
strictSafeNavigationTypes: true,
};
options = options || {
emitSpans: false,
};
const tcb = generateTypeCheckBlock(
FakeEnvironment.newFake(config), new Reference(clazz), ts.createIdentifier('Test_TCB'), 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, ' ');
}
export function typecheck(
template: string, source: string, declarations: TestDeclaration[] = [],
additionalSources: {name: AbsoluteFsPath; contents: string}[] = []): ts.Diagnostic[] {
const typeCheckFilePath = absoluteFrom('/_typecheck_.ts');
const files = [
typescriptLibDts(),
angularCoreDts(),
angularAnimationsDts(),
// Add the typecheck file to the program, as the typecheck program is created with the
// assumption that the typecheck file was already a root file in the original program.
{name: typeCheckFilePath, contents: 'export const TYPECHECK = true;'},
{name: absoluteFrom('/main.ts'), contents: source},
...additionalSources,
];
const {program, host, options} =
makeProgram(files, {strictNullChecks: true, noImplicitAny: true}, undefined, false);
const sf = program.getSourceFile(absoluteFrom('/main.ts')) !;
const checker = program.getTypeChecker();
const logicalFs = new LogicalFileSystem(getRootDirs(host, options));
const reflectionHost = new TypeScriptReflectionHost(checker);
const emitter = new ReferenceEmitter([
new LocalIdentifierStrategy(),
new AbsoluteModuleStrategy(
program, checker, options, host, new TypeScriptReflectionHost(checker)),
new LogicalProjectStrategy(reflectionHost, logicalFs),
]);
const ctx = new TypeCheckContext(ALL_ENABLED_CONFIG, emitter, typeCheckFilePath);
const templateUrl = 'synthetic.html';
const templateFile = new ParseSourceFile(template, templateUrl);
const {nodes, errors} = parseTemplate(template, templateUrl);
if (errors !== undefined) {
throw new Error('Template parse errors: \n' + errors.join('\n'));
}
const {matcher, pipes} = prepareDeclarations(declarations, decl => {
let declFile = sf;
if (decl.file !== undefined) {
declFile = program.getSourceFile(decl.file) !;
if (declFile === undefined) {
throw new Error(`Unable to locate ${decl.file} for ${decl.type} ${decl.name}`);
}
}
return getClass(declFile, decl.name);
});
const binder = new R3TargetBinder(matcher);
const boundTarget = binder.bind({template: nodes});
const clazz = new Reference(getClass(sf, 'TestComponent'));
const sourceMapping: TemplateSourceMapping = {
type: 'external',
template,
templateUrl,
componentClass: clazz.node,
// Use the class's name for error mappings.
node: clazz.node.name,
};
ctx.addTemplate(clazz, boundTarget, pipes, [], sourceMapping, templateFile);
return ctx.calculateTemplateDiagnostics(program, host, options).diagnostics;
}
function prepareDeclarations(
declarations: TestDeclaration[],
resolveDeclaration: (decl: TestDeclaration) => ClassDeclaration<ts.ClassDeclaration>) {
const matcher = new SelectorMatcher();
for (const decl of declarations) {
if (decl.type !== 'directive') {
continue;
}
const selector = CssSelector.parse(decl.selector);
const meta: TypeCheckableDirectiveMeta = {
name: decl.name,
ref: new Reference(resolveDeclaration(decl)),
exportAs: decl.exportAs || null,
hasNgTemplateContextGuard: decl.hasNgTemplateContextGuard || false,
inputs: decl.inputs || {},
isComponent: decl.isComponent || false,
ngTemplateGuards: decl.ngTemplateGuards || [],
coercedInputFields: new Set<string>(decl.coercedInputFields || []),
outputs: decl.outputs || {},
queries: decl.queries || [],
};
matcher.addSelectables(selector, meta);
}
const pipes = new Map<string, Reference<ClassDeclaration<ts.ClassDeclaration>>>();
for (const decl of declarations) {
if (decl.type === 'pipe') {
pipes.set(decl.pipeName, new Reference(resolveDeclaration(decl)));
}
}
return {matcher, pipes};
}
export function getClass(sf: ts.SourceFile, name: string): ClassDeclaration<ts.ClassDeclaration> {
for (const stmt of sf.statements) {
if (isNamedClassDeclaration(stmt) && stmt.name.text === name) {
return stmt;
}
}
throw new Error(`Class ${name} not found in file`);
}
class FakeEnvironment /* implements Environment */ {
constructor(readonly config: TypeCheckingConfig) {}
typeCtorFor(dir: TypeCheckableDirectiveMeta): ts.Expression {
return ts.createPropertyAccess(ts.createIdentifier(dir.name), 'ngTypeCtor');
}
pipeInst(ref: Reference<ClassDeclaration<ts.ClassDeclaration>>): ts.Expression {
return ts.createParen(ts.createAsExpression(ts.createNull(), this.referenceType(ref)));
}
declareOutputHelper(): ts.Expression { return ts.createIdentifier('_outputHelper'); }
reference(ref: Reference<ClassDeclaration<ts.ClassDeclaration>>): ts.Expression {
return ref.node.name;
}
referenceType(ref: Reference<ClassDeclaration<ts.ClassDeclaration>>): ts.TypeNode {
return ts.createTypeReferenceNode(ref.node.name, /* typeArguments */ undefined);
}
referenceExternalType(moduleName: string, name: string, typeParams?: Type[]): ts.TypeNode {
const typeArgs: ts.TypeNode[] = [];
if (typeParams !== undefined) {
for (let i = 0; i < typeParams.length; i++) {
typeArgs.push(ts.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword));
}
}
const ns = ts.createIdentifier(moduleName.replace('@angular/', ''));
const qName = ts.createQualifiedName(ns, name);
return ts.createTypeReferenceNode(qName, typeArgs.length > 0 ? typeArgs : undefined);
}
getPreludeStatements(): ts.Statement[] { return []; }
static newFake(config: TypeCheckingConfig): Environment {
return new FakeEnvironment(config) as Environment;
}
}
export class NoopSchemaChecker implements DomSchemaChecker {
get diagnostics(): ReadonlyArray<ts.Diagnostic> { return []; }
checkElement(id: string, element: TmplAstElement, schemas: SchemaMetadata[]): void {}
checkProperty(
id: string, element: TmplAstElement, name: string, span: ParseSourceSpan,
schemas: SchemaMetadata[]): void {}
}
export class NoopOobRecorder implements OutOfBandDiagnosticRecorder {
get diagnostics(): ReadonlyArray<ts.Diagnostic> { return []; }
missingReferenceTarget(): void {}
missingPipe(): void {}
}