forked from typescript-eslint/typescript-eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
persistentParse.test.ts
355 lines (286 loc) · 11.6 KB
/
persistentParse.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
import fs from 'fs';
import path from 'path';
import tmp from 'tmp';
import { clearCaches } from '../../src/clear-caches';
import { clearWatchCaches } from '../../src/create-program/getWatchProgramsForProjects';
import { parseAndGenerateServices } from '../../src/parser';
const CONTENTS = {
foo: 'console.log("foo")',
bar: 'console.log("bar")',
'baz/bar': 'console.log("baz bar")',
'bat/baz/bar': 'console.log("bat/baz/bar")',
number: 'const foo = 1;',
object: '(() => { })();',
string: 'let a: "a" | "b";',
};
const cwdCopy = process.cwd();
const tmpDirs = new Set<tmp.DirResult>();
afterEach(() => {
// stop watching the files and folders
clearWatchCaches();
// clean up the temporary files and folders
tmpDirs.forEach(t => t.removeCallback());
tmpDirs.clear();
// restore original cwd
process.chdir(cwdCopy);
});
function writeTSConfig(dirName: string, config: Record<string, unknown>): void {
fs.writeFileSync(path.join(dirName, 'tsconfig.json'), JSON.stringify(config));
}
function writeFile(dirName: string, file: keyof typeof CONTENTS): void {
fs.writeFileSync(path.join(dirName, 'src', `${file}.ts`), CONTENTS[file]);
}
function renameFile(dirName: string, src: 'bar', dest: 'baz/bar'): void {
fs.renameSync(
path.join(dirName, 'src', `${src}.ts`),
path.join(dirName, 'src', `${dest}.ts`),
);
}
function createTmpDir(): tmp.DirResult {
const tmpDir = tmp.dirSync({
keep: false,
unsafeCleanup: true,
});
tmpDirs.add(tmpDir);
return tmpDir;
}
function setup(tsconfig: Record<string, unknown>, writeBar = true): string {
const tmpDir = createTmpDir();
writeTSConfig(tmpDir.name, tsconfig);
fs.mkdirSync(path.join(tmpDir.name, 'src'));
fs.mkdirSync(path.join(tmpDir.name, 'src', 'baz'));
writeFile(tmpDir.name, 'foo');
writeBar && writeFile(tmpDir.name, 'bar');
return tmpDir.name;
}
function parseFile(
filename: keyof typeof CONTENTS,
tmpDir: string,
relative?: boolean,
ignoreTsconfigRootDir?: boolean,
): void {
parseAndGenerateServices(CONTENTS[filename], {
project: './tsconfig.json',
tsconfigRootDir: ignoreTsconfigRootDir ? undefined : tmpDir,
filePath: relative
? path.join('src', `${filename}.ts`)
: path.join(tmpDir, 'src', `${filename}.ts`),
});
}
function existsSync(filename: keyof typeof CONTENTS, tmpDir = ''): boolean {
return fs.existsSync(path.join(tmpDir, 'src', `${filename}.ts`));
}
function baseTests(
tsConfigExcludeBar: Record<string, unknown>,
tsConfigIncludeAll: Record<string, unknown>,
): void {
// The experimental project server creates a default project for files
if (process.env.TYPESCRIPT_ESLINT_EXPERIMENTAL_TSSERVER === 'true') {
return;
}
it('parses both files successfully when included', () => {
const PROJECT_DIR = setup(tsConfigIncludeAll);
expect(() => parseFile('foo', PROJECT_DIR)).not.toThrow();
expect(() => parseFile('bar', PROJECT_DIR)).not.toThrow();
});
it('parses included files, and throws on excluded files', () => {
const PROJECT_DIR = setup(tsConfigExcludeBar);
expect(() => parseFile('foo', PROJECT_DIR)).not.toThrow();
expect(() => parseFile('bar', PROJECT_DIR)).toThrow();
});
it('allows parsing of new files', () => {
const PROJECT_DIR = setup(tsConfigIncludeAll, false);
// parse once to: assert the config as correct, and to make sure the program is setup
expect(() => parseFile('foo', PROJECT_DIR)).not.toThrow();
// bar should throw because it doesn't exist yet
expect(() => parseFile('bar', PROJECT_DIR)).toThrow();
// write a new file and attempt to parse it
writeFile(PROJECT_DIR, 'bar');
// both files should parse fine now
expect(() => parseFile('foo', PROJECT_DIR)).not.toThrow();
expect(() => parseFile('bar', PROJECT_DIR)).not.toThrow();
});
it('allows parsing of deeply nested new files', () => {
const PROJECT_DIR = setup(tsConfigIncludeAll, false);
const bazSlashBar = 'baz/bar';
// parse once to: assert the config as correct, and to make sure the program is setup
expect(() => parseFile('foo', PROJECT_DIR)).not.toThrow();
// bar should throw because it doesn't exist yet
expect(() => parseFile(bazSlashBar, PROJECT_DIR)).toThrow();
// write a new file and attempt to parse it
writeFile(PROJECT_DIR, bazSlashBar);
// both files should parse fine now
expect(() => parseFile('foo', PROJECT_DIR)).not.toThrow();
expect(() => parseFile(bazSlashBar, PROJECT_DIR)).not.toThrow();
});
it('allows parsing of deeply nested new files in new folder', () => {
const PROJECT_DIR = setup(tsConfigIncludeAll);
expect(() => parseFile('foo', PROJECT_DIR)).not.toThrow();
// Create deep folder structure after first parse (this is important step)
// context: https://github.com/typescript-eslint/typescript-eslint/issues/1394
fs.mkdirSync(path.join(PROJECT_DIR, 'src', 'bat'));
fs.mkdirSync(path.join(PROJECT_DIR, 'src', 'bat', 'baz'));
const bazSlashBar = 'bat/baz/bar';
// write a new file and attempt to parse it
writeFile(PROJECT_DIR, bazSlashBar);
expect(() => parseFile(bazSlashBar, PROJECT_DIR)).not.toThrow();
});
it('allows renaming of files', () => {
const PROJECT_DIR = setup(tsConfigIncludeAll, true);
const bazSlashBar = 'baz/bar';
// parse once to: assert the config as correct, and to make sure the program is setup
expect(() => parseFile('foo', PROJECT_DIR)).not.toThrow();
// bar should throw because it doesn't exist yet
expect(() => parseFile(bazSlashBar, PROJECT_DIR)).toThrow();
// write a new file and attempt to parse it
renameFile(PROJECT_DIR, 'bar', bazSlashBar);
// both files should parse fine now
expect(() => parseFile('foo', PROJECT_DIR)).not.toThrow();
expect(() => parseFile(bazSlashBar, PROJECT_DIR)).not.toThrow();
});
it('reacts to changes in the tsconfig', () => {
const PROJECT_DIR = setup(tsConfigExcludeBar);
// parse once to: assert the config as correct, and to make sure the program is setup
expect(() => parseFile('foo', PROJECT_DIR)).not.toThrow();
expect(() => parseFile('bar', PROJECT_DIR)).toThrow();
// change the config file so it now includes all files
writeTSConfig(PROJECT_DIR, tsConfigIncludeAll);
clearCaches();
expect(() => parseFile('foo', PROJECT_DIR)).not.toThrow();
expect(() => parseFile('bar', PROJECT_DIR)).not.toThrow();
});
it('should work with relative paths', () => {
const PROJECT_DIR = setup(tsConfigIncludeAll, false);
// parse once to: assert the config as correct, and to make sure the program is setup
expect(() => parseFile('foo', PROJECT_DIR, true)).not.toThrow();
// bar should throw because it doesn't exist yet
expect(() => parseFile('bar', PROJECT_DIR, true)).toThrow();
// write a new file and attempt to parse it
writeFile(PROJECT_DIR, 'bar');
// make sure that file is correctly created
expect(existsSync('bar', PROJECT_DIR)).toBe(true);
// both files should parse fine now
expect(() => parseFile('foo', PROJECT_DIR, true)).not.toThrow();
expect(() => parseFile('bar', PROJECT_DIR, true)).not.toThrow();
});
it('should work with relative paths without tsconfig root', () => {
const PROJECT_DIR = setup(tsConfigIncludeAll, false);
process.chdir(PROJECT_DIR);
// parse once to: assert the config as correct, and to make sure the program is setup
expect(() => parseFile('foo', PROJECT_DIR, true, true)).not.toThrow();
// bar should throw because it doesn't exist yet
expect(() => parseFile('bar', PROJECT_DIR, true, true)).toThrow();
// write a new file and attempt to parse it
writeFile(PROJECT_DIR, 'bar');
// make sure that file is correctly created
expect(existsSync('bar')).toBe(true);
expect(existsSync('bar', PROJECT_DIR)).toBe(true);
// both files should parse fine now
expect(() => parseFile('foo', PROJECT_DIR, true, true)).not.toThrow();
expect(() => parseFile('bar', PROJECT_DIR, true, true)).not.toThrow();
});
}
describe('persistent parse', () => {
describe('includes not ending in a slash', () => {
const tsConfigExcludeBar = {
include: ['src'],
exclude: ['./src/bar.ts'],
};
const tsConfigIncludeAll = {
include: ['src'],
exclude: [],
};
baseTests(tsConfigExcludeBar, tsConfigIncludeAll);
});
/*
If the includes ends in a slash, typescript will ask for watchers ending in a slash.
These tests ensure the normalization of code works as expected in this case.
*/
describe('includes ending in a slash', () => {
const tsConfigExcludeBar = {
include: ['src/'],
exclude: ['./src/bar.ts'],
};
const tsConfigIncludeAll = {
include: ['src/'],
exclude: [],
};
baseTests(tsConfigExcludeBar, tsConfigIncludeAll);
});
/*
If there is no includes, then typescript will ask for a slightly different set of watchers.
*/
if (process.env.TYPESCRIPT_ESLINT_EXPERIMENTAL_TSSERVER !== 'true') {
describe('tsconfig with no includes / files', () => {
const tsConfigExcludeBar = {
exclude: ['./src/bar.ts'],
};
const tsConfigIncludeAll = {};
baseTests(tsConfigExcludeBar, tsConfigIncludeAll);
it('handles tsconfigs with no includes/excludes (single level)', () => {
const PROJECT_DIR = setup({}, false);
// parse once to: assert the config as correct, and to make sure the program is setup
expect(() => parseFile('foo', PROJECT_DIR)).not.toThrow();
expect(() => parseFile('bar', PROJECT_DIR)).toThrow();
// write a new file and attempt to parse it
writeFile(PROJECT_DIR, 'bar');
clearCaches();
expect(() => parseFile('foo', PROJECT_DIR)).not.toThrow();
expect(() => parseFile('bar', PROJECT_DIR)).not.toThrow();
});
it('handles tsconfigs with no includes/excludes (nested)', () => {
const PROJECT_DIR = setup({}, false);
const bazSlashBar = 'baz/bar';
// parse once to: assert the config as correct, and to make sure the program is setup
expect(() => parseFile('foo', PROJECT_DIR)).not.toThrow();
expect(() => parseFile(bazSlashBar, PROJECT_DIR)).toThrow();
// write a new file and attempt to parse it
writeFile(PROJECT_DIR, bazSlashBar);
clearCaches();
expect(() => parseFile('foo', PROJECT_DIR)).not.toThrow();
expect(() => parseFile(bazSlashBar, PROJECT_DIR)).not.toThrow();
});
});
}
/*
If there is no includes, then typescript will ask for a slightly different set of watchers.
*/
describe('tsconfig with overlapping globs', () => {
const tsConfigExcludeBar = {
include: ['./*', './**/*', './src/**/*'],
exclude: ['./src/bar.ts'],
};
const tsConfigIncludeAll = {
include: ['./*', './**/*', './src/**/*'],
};
baseTests(tsConfigExcludeBar, tsConfigIncludeAll);
});
describe('tsconfig with module set', () => {
const moduleTypes = [
'None',
'CommonJS',
'AMD',
'System',
'UMD',
'ES6',
'ES2015',
'ESNext',
];
for (const module of moduleTypes) {
describe(`module ${module}`, () => {
const tsConfigIncludeAll = {
compilerOptions: { module },
include: ['./**/*'],
};
const testNames = ['object', 'number', 'string', 'foo'] as const;
for (const name of testNames) {
it(`first parse of ${name} should not throw`, () => {
const PROJECT_DIR = setup(tsConfigIncludeAll);
writeFile(PROJECT_DIR, name);
expect(() => parseFile(name, PROJECT_DIR)).not.toThrow();
});
}
});
}
});
});