/
semanticInfo.test.ts
416 lines (370 loc) · 14.1 KB
/
semanticInfo.test.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import * as fs from 'fs';
import glob from 'glob';
import * as path from 'path';
import * as ts from 'typescript';
import { clearWatchCaches } from '../../src/create-program/getWatchProgramsForProjects';
import { createProgramFromConfigFile as createProgram } from '../../src/create-program/useProvidedPrograms';
import type { ParseAndGenerateServicesResult } from '../../src/parser';
import { parseAndGenerateServices } from '../../src/parser';
import type { TSESTreeOptions } from '../../src/parser-options';
import type { TSESTree } from '../../src/ts-estree';
import {
createSnapshotTestBlock,
formatSnapshotName,
parseCodeAndGenerateServices,
} from '../../tools/test-utils';
const FIXTURES_DIR = './tests/fixtures/semanticInfo';
const testFiles = glob.sync(`**/*.src.ts`, {
cwd: FIXTURES_DIR,
});
function createOptions(fileName: string): TSESTreeOptions & { cwd?: string } {
return {
loc: true,
range: true,
tokens: true,
comment: true,
jsx: false,
errorOnUnknownASTType: true,
filePath: fileName,
tsconfigRootDir: path.join(process.cwd(), FIXTURES_DIR),
project: `./tsconfig.json`,
loggerFn: false,
};
}
// ensure tsconfig-parser watch caches are clean for each test
beforeEach(() => clearWatchCaches());
describe('semanticInfo', () => {
// test all AST snapshots
testFiles.forEach(filename => {
const code = fs.readFileSync(path.join(FIXTURES_DIR, filename), 'utf8');
it(
formatSnapshotName(filename, FIXTURES_DIR, path.extname(filename)),
createSnapshotTestBlock(
code,
createOptions(filename),
/*generateServices*/ true,
),
);
});
it(`should cache the created ts.program`, () => {
const filename = testFiles[0];
const code = fs.readFileSync(path.join(FIXTURES_DIR, filename), 'utf8');
const options = createOptions(filename);
const optionsProjectString = {
...options,
project: './tsconfig.json',
};
expect(
parseAndGenerateServices(code, optionsProjectString).services.program,
).toBe(
parseAndGenerateServices(code, optionsProjectString).services.program,
);
});
it(`should handle "project": "./tsconfig.json" and "project": ["./tsconfig.json"] the same`, () => {
const filename = testFiles[0];
const code = fs.readFileSync(path.join(FIXTURES_DIR, filename), 'utf8');
const options = createOptions(filename);
const optionsProjectString = {
...options,
project: './tsconfig.json',
};
const optionsProjectArray = {
...options,
project: ['./tsconfig.json'],
};
expect(parseAndGenerateServices(code, optionsProjectString)).toEqual(
parseAndGenerateServices(code, optionsProjectArray),
);
});
it(`should resolve absolute and relative tsconfig paths the same`, () => {
const filename = testFiles[0];
const code = fs.readFileSync(path.join(FIXTURES_DIR, filename), 'utf8');
const options = createOptions(filename);
const optionsAbsolutePath = {
...options,
project: `${__dirname}/../fixtures/semanticInfo/tsconfig.json`,
};
const optionsRelativePath = {
...options,
project: `./tsconfig.json`,
};
const absolutePathResult = parseAndGenerateServices(
code,
optionsAbsolutePath,
);
const relativePathResult = parseAndGenerateServices(
code,
optionsRelativePath,
);
if (absolutePathResult.services.program === undefined) {
throw new Error('Unable to create ts.program for absolute tsconfig');
} else if (relativePathResult.services.program === undefined) {
throw new Error('Unable to create ts.program for relative tsconfig');
}
expect(
absolutePathResult.services.program.getResolvedProjectReferences(),
).toEqual(
relativePathResult.services.program.getResolvedProjectReferences(),
);
});
// case-specific tests
it('isolated-file tests', () => {
const fileName = path.resolve(FIXTURES_DIR, 'isolated-file.src.ts');
const parseResult = parseCodeAndGenerateServices(
fs.readFileSync(fileName, 'utf8'),
createOptions(fileName),
);
testIsolatedFile(parseResult);
});
it('isolated-vue-file tests', () => {
const fileName = path.resolve(FIXTURES_DIR, 'extra-file-extension.vue');
const parseResult = parseCodeAndGenerateServices(
fs.readFileSync(fileName, 'utf8'),
{
...createOptions(fileName),
extraFileExtensions: ['.vue'],
},
);
testIsolatedFile(parseResult);
});
it('non-existent-estree-nodes tests', () => {
const fileName = path.resolve(
FIXTURES_DIR,
'non-existent-estree-nodes.src.ts',
);
const parseResult = parseCodeAndGenerateServices(
fs.readFileSync(fileName, 'utf8'),
createOptions(fileName),
);
expect(parseResult).toHaveProperty('services.esTreeNodeToTSNodeMap');
const binaryExpression = (
parseResult.ast.body[0] as TSESTree.VariableDeclaration
).declarations[0].init!;
const tsBinaryExpression =
parseResult.services.esTreeNodeToTSNodeMap.get(binaryExpression);
expect(tsBinaryExpression.kind).toEqual(ts.SyntaxKind.BinaryExpression);
const computedPropertyString = (
(parseResult.ast.body[1] as TSESTree.ClassDeclaration).body
.body[0] as TSESTree.PropertyDefinition
).key;
const tsComputedPropertyString =
parseResult.services.esTreeNodeToTSNodeMap.get(computedPropertyString);
expect(tsComputedPropertyString.kind).toEqual(ts.SyntaxKind.StringLiteral);
});
it('imported-file tests', () => {
const fileName = path.resolve(FIXTURES_DIR, 'import-file.src.ts');
const parseResult = parseCodeAndGenerateServices(
fs.readFileSync(fileName, 'utf8'),
createOptions(fileName),
);
// get type checker
expect(parseResult).toHaveProperty('services.program.getTypeChecker');
const checker = parseResult.services.program.getTypeChecker();
// get array node (ast shape validated by snapshot)
// node is defined in other file than the parsed one
const arrayBoundName = (
(
(parseResult.ast.body[1] as TSESTree.ExpressionStatement)
.expression as TSESTree.CallExpression
).callee as TSESTree.MemberExpression
).object as TSESTree.Identifier;
expect(arrayBoundName.name).toBe('arr');
expect(parseResult).toHaveProperty('services.esTreeNodeToTSNodeMap');
const tsArrayBoundName =
parseResult.services.esTreeNodeToTSNodeMap.get(arrayBoundName);
expect(tsArrayBoundName).toBeDefined();
checkNumberArrayType(checker, tsArrayBoundName);
expect(
parseResult.services.tsNodeToESTreeNodeMap.get(tsArrayBoundName),
).toBe(arrayBoundName);
});
it('non-existent file tests', () => {
const parseResult = parseCodeAndGenerateServices(
`const x = [parseInt("5")];`,
{
...createOptions('<input>'),
project: undefined,
preserveNodeMaps: true,
},
);
expect(parseResult.services.program).toBeDefined();
// get bound name
const boundName = (parseResult.ast.body[0] as TSESTree.VariableDeclaration)
.declarations[0].id as TSESTree.Identifier;
expect(boundName.name).toBe('x');
const tsBoundName =
parseResult.services.esTreeNodeToTSNodeMap.get(boundName);
expect(tsBoundName).toBeDefined();
expect(parseResult.services.tsNodeToESTreeNodeMap.get(tsBoundName)).toBe(
boundName,
);
});
it('non-existent file should provide parents nodes', () => {
const parseResult = parseCodeAndGenerateServices(
`function M() { return Base }`,
{ ...createOptions('<input>'), project: undefined },
);
expect(parseResult.services.program).toBeDefined();
});
it(`non-existent file should throw error when project provided`, () => {
expect(() =>
parseCodeAndGenerateServices(
`function M() { return Base }`,
createOptions('<input>'),
),
).toThrow(
/ESLint was configured to run on `<tsconfigRootDir>\/estree\.ts` using/,
);
});
it('non-existent project file', () => {
const fileName = path.resolve(FIXTURES_DIR, 'isolated-file.src.ts');
const badConfig = createOptions(fileName);
badConfig.project = './tsconfigs.json';
expect(() =>
parseCodeAndGenerateServices(
fs.readFileSync(fileName, 'utf8'),
badConfig,
),
).toThrow(/Cannot read file .+tsconfigs\.json'/);
});
it('fail to read project file', () => {
const fileName = path.resolve(FIXTURES_DIR, 'isolated-file.src.ts');
const badConfig = createOptions(fileName);
badConfig.project = '.';
expect(() =>
parseCodeAndGenerateServices(
fs.readFileSync(fileName, 'utf8'),
badConfig,
),
).toThrow(
// case insensitive because unix based systems are case insensitive
/Cannot read file .+semanticInfo'./i,
);
});
it('malformed project file', () => {
const fileName = path.resolve(FIXTURES_DIR, 'isolated-file.src.ts');
const badConfig = createOptions(fileName);
badConfig.project = './badTSConfig/tsconfig.json';
expect(() =>
parseCodeAndGenerateServices(
fs.readFileSync(fileName, 'utf8'),
badConfig,
),
).toThrowErrorMatchingSnapshot();
});
it('default program produced with option', () => {
const parseResult = parseCodeAndGenerateServices('var foo = 5;', {
...createOptions('<input>'),
createDefaultProgram: true,
});
expect(parseResult.services.program).toBeDefined();
});
it('empty programs array should throw', () => {
const fileName = path.resolve(FIXTURES_DIR, 'isolated-file.src.ts');
const badConfig = createOptions(fileName);
badConfig.programs = [];
expect(() => parseAndGenerateServices('const foo = 5;', badConfig)).toThrow(
'You have set parserOptions.programs to an empty array. This will cause all files to not be found in existing programs. Either provide one or more existing TypeScript Program instances in the array, or remove the parserOptions.programs setting.',
);
});
it(`first matching provided program instance is returned in result`, () => {
const filename = testFiles[0];
const program1 = createProgram(path.join(FIXTURES_DIR, 'tsconfig.json'));
const program2 = createProgram(path.join(FIXTURES_DIR, 'tsconfig.json'));
const code = fs.readFileSync(path.join(FIXTURES_DIR, filename), 'utf8');
const options = createOptions(filename);
const optionsProjectString = {
...options,
programs: [program1, program2],
project: './tsconfig.json',
};
const parseResult = parseAndGenerateServices(code, optionsProjectString);
expect(parseResult.services.program).toBe(program1);
});
it('file not in provided program instance(s)', () => {
const filename = 'non-existent-file.ts';
const program1 = createProgram(path.join(FIXTURES_DIR, 'tsconfig.json'));
const options = createOptions(filename);
const optionsWithSingleProgram = {
...options,
programs: [program1],
};
expect(() =>
parseAndGenerateServices('const foo = 5;', optionsWithSingleProgram),
).toThrow(
`The file was not found in any of the provided program instance(s): ${filename}`,
);
const program2 = createProgram(path.join(FIXTURES_DIR, 'tsconfig.json'));
const optionsWithMultiplePrograms = {
...options,
programs: [program1, program2],
};
expect(() =>
parseAndGenerateServices('const foo = 5;', optionsWithMultiplePrograms),
).toThrow(
`The file was not found in any of the provided program instance(s): ${filename}`,
);
});
it('createProgram fails on non-existent file', () => {
expect(() => createProgram('tsconfig.non-existent.json')).toThrow();
});
it('createProgram fails on tsconfig with errors', () => {
expect(() =>
createProgram(path.join(FIXTURES_DIR, 'badTSConfig', 'tsconfig.json')),
).toThrow();
});
});
function testIsolatedFile(
parseResult: ParseAndGenerateServicesResult<TSESTreeOptions>,
): void {
// get type checker
expect(parseResult).toHaveProperty('services.program.getTypeChecker');
const checker = parseResult.services.program.getTypeChecker();
// get number node (ast shape validated by snapshot)
const declaration = (parseResult.ast.body[0] as TSESTree.VariableDeclaration)
.declarations[0];
const arrayMember = (declaration.init! as TSESTree.ArrayExpression)
.elements[0]!;
expect(parseResult).toHaveProperty('services.esTreeNodeToTSNodeMap');
// get corresponding TS node
const tsArrayMember =
parseResult.services.esTreeNodeToTSNodeMap.get(arrayMember);
expect(tsArrayMember).toBeDefined();
expect(tsArrayMember.kind).toBe(ts.SyntaxKind.NumericLiteral);
expect((tsArrayMember as ts.NumericLiteral).text).toBe('3');
// get type of TS node
const arrayMemberType = checker.getTypeAtLocation(tsArrayMember);
expect(arrayMemberType.flags).toBe(ts.TypeFlags.NumberLiteral);
// using an internal api
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect((arrayMemberType as any).value).toBe(3);
// make sure it maps back to original ESTree node
expect(parseResult).toHaveProperty('services.tsNodeToESTreeNodeMap');
expect(parseResult.services.tsNodeToESTreeNodeMap.get(tsArrayMember)).toBe(
arrayMember,
);
// get bound name
const boundName = declaration.id as TSESTree.Identifier;
expect(boundName.name).toBe('x');
const tsBoundName = parseResult.services.esTreeNodeToTSNodeMap.get(boundName);
expect(tsBoundName).toBeDefined();
checkNumberArrayType(checker, tsBoundName);
expect(parseResult.services.tsNodeToESTreeNodeMap.get(tsBoundName)).toBe(
boundName,
);
}
/**
* Verifies that the type of a TS node is number[] as expected
* @param {ts.TypeChecker} checker
* @param {ts.Node} tsNode
*/
function checkNumberArrayType(checker: ts.TypeChecker, tsNode: ts.Node): void {
const nodeType = checker.getTypeAtLocation(tsNode);
expect(nodeType.flags).toBe(ts.TypeFlags.Object);
expect((nodeType as ts.ObjectType).objectFlags).toBe(
ts.ObjectFlags.Reference,
);
const typeArguments = checker.getTypeArguments(nodeType as ts.TypeReference);
expect(typeArguments).toHaveLength(1);
expect(typeArguments[0].flags).toBe(ts.TypeFlags.Number);
}