forked from angular/angular
/
type_constructor_spec.ts
172 lines (164 loc) · 6.99 KB
/
type_constructor_spec.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
/**
* @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 * as ts from 'typescript';
import {LogicalFileSystem, absoluteFrom, getSourceFileOrError} from '../../file_system';
import {TestFile, runInEachFileSystem} from '../../file_system/testing';
import {AbsoluteModuleStrategy, LocalIdentifierStrategy, LogicalProjectStrategy, Reference, ReferenceEmitter} from '../../imports';
import {TypeScriptReflectionHost, isNamedClassDeclaration} from '../../reflection';
import {getDeclaration, makeProgram} from '../../testing';
import {getRootDirs} from '../../util/src/typescript';
import {TypeCheckContext} from '../src/context';
import {TypeCheckFile} from '../src/type_check_file';
import {ALL_ENABLED_CONFIG} from './test_utils';
runInEachFileSystem(() => {
describe('ngtsc typechecking', () => {
let _: typeof absoluteFrom;
let LIB_D_TS: TestFile;
let TYPE_CHECK_TS: TestFile;
beforeEach(() => {
_ = absoluteFrom;
LIB_D_TS = {
name: _('/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;`
};
TYPE_CHECK_TS = {
name: _('/_typecheck_.ts'),
contents: `
export const IS_A_MODULE = true;
`
};
});
it('should not produce an empty SourceFile when there is nothing to typecheck', () => {
const file =
new TypeCheckFile(_('/_typecheck_.ts'), ALL_ENABLED_CONFIG, new ReferenceEmitter([]));
const sf = file.render();
expect(sf.statements.length).toBe(1);
});
describe('ctors', () => {
it('compiles a basic type constructor', () => {
const files: TestFile[] = [
LIB_D_TS, TYPE_CHECK_TS, {
name: _('/main.ts'),
contents: `
class TestClass<T extends string> {
value: T;
}
TestClass.ngTypeCtor({value: 'test'});
`
}
];
const {program, host, options} = makeProgram(files, undefined, undefined, false);
const checker = program.getTypeChecker();
const reflectionHost = new TypeScriptReflectionHost(checker);
const logicalFs = new LogicalFileSystem(getRootDirs(host, options));
const emitter = new ReferenceEmitter([
new LocalIdentifierStrategy(),
new AbsoluteModuleStrategy(program, checker, options, host, reflectionHost),
new LogicalProjectStrategy(reflectionHost, logicalFs),
]);
const ctx = new TypeCheckContext(ALL_ENABLED_CONFIG, emitter, _('/_typecheck_.ts'));
const TestClass =
getDeclaration(program, _('/main.ts'), 'TestClass', isNamedClassDeclaration);
ctx.addInlineTypeCtor(
getSourceFileOrError(program, _('/main.ts')), new Reference(TestClass), {
fnName: 'ngTypeCtor',
body: true,
fields: {
inputs: ['value'],
outputs: [],
queries: [],
},
coercedInputFields: new Set(),
});
ctx.calculateTemplateDiagnostics(program, host, options);
});
it('should not consider query fields', () => {
const files: TestFile[] = [
LIB_D_TS, TYPE_CHECK_TS, {
name: _('/main.ts'),
contents: `class TestClass { value: any; }`,
}
];
const {program, host, options} = makeProgram(files, undefined, undefined, false);
const checker = program.getTypeChecker();
const reflectionHost = new TypeScriptReflectionHost(checker);
const logicalFs = new LogicalFileSystem(getRootDirs(host, options));
const emitter = new ReferenceEmitter([
new LocalIdentifierStrategy(),
new AbsoluteModuleStrategy(program, checker, options, host, reflectionHost),
new LogicalProjectStrategy(reflectionHost, logicalFs),
]);
const ctx = new TypeCheckContext(ALL_ENABLED_CONFIG, emitter, _('/_typecheck_.ts'));
const TestClass =
getDeclaration(program, _('/main.ts'), 'TestClass', isNamedClassDeclaration);
ctx.addInlineTypeCtor(
getSourceFileOrError(program, _('/main.ts')), new Reference(TestClass), {
fnName: 'ngTypeCtor',
body: true,
fields: {
inputs: ['value'],
outputs: [],
queries: ['queryField'],
},
coercedInputFields: new Set(),
});
const res = ctx.calculateTemplateDiagnostics(program, host, options);
const TestClassWithCtor =
getDeclaration(res.program, _('/main.ts'), 'TestClass', isNamedClassDeclaration);
const typeCtor = TestClassWithCtor.members.find(isTypeCtor) !;
expect(typeCtor.getText()).not.toContain('queryField');
});
});
describe('input type coercion', () => {
it('should coerce input types', () => {
const files: TestFile[] = [
LIB_D_TS, TYPE_CHECK_TS, {
name: _('/main.ts'),
contents: `class TestClass { value: any; }`,
}
];
const {program, host, options} = makeProgram(files, undefined, undefined, false);
const checker = program.getTypeChecker();
const reflectionHost = new TypeScriptReflectionHost(checker);
const logicalFs = new LogicalFileSystem(getRootDirs(host, options));
const emitter = new ReferenceEmitter([
new LocalIdentifierStrategy(),
new AbsoluteModuleStrategy(program, checker, options, host, reflectionHost),
new LogicalProjectStrategy(reflectionHost, logicalFs),
]);
const ctx = new TypeCheckContext(ALL_ENABLED_CONFIG, emitter, _('/_typecheck_.ts'));
const TestClass =
getDeclaration(program, _('/main.ts'), 'TestClass', isNamedClassDeclaration);
ctx.addInlineTypeCtor(
getSourceFileOrError(program, _('/main.ts')), new Reference(TestClass), {
fnName: 'ngTypeCtor',
body: true,
fields: {
inputs: ['foo', 'bar'],
outputs: [],
queries: [],
},
coercedInputFields: new Set(['bar']),
});
const res = ctx.calculateTemplateDiagnostics(program, host, options);
const TestClassWithCtor =
getDeclaration(res.program, _('/main.ts'), 'TestClass', isNamedClassDeclaration);
const typeCtor = TestClassWithCtor.members.find(isTypeCtor) !;
const ctorText = typeCtor.getText().replace(/[ \r\n]+/g, ' ');
expect(ctorText).toContain(
'init: Pick<TestClass, "foo"> | { bar: typeof TestClass.ngAcceptInputType_bar; }');
});
});
});
function isTypeCtor(el: ts.ClassElement): el is ts.MethodDeclaration {
return ts.isMethodDeclaration(el) && ts.isIdentifier(el.name) && el.name.text === 'ngTypeCtor';
}
});