/
persistentParse.ts
233 lines (188 loc) · 7.61 KB
/
persistentParse.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
import fs from 'fs';
import path from 'path';
import tmp from 'tmp';
import { clearCaches, parseAndGenerateServices } from '../../src/parser';
const CONTENTS = {
foo: 'console.log("foo")',
bar: 'console.log("bar")',
'baz/bar': 'console.log("baz bar")',
};
const tmpDirs = new Set<tmp.DirResult>();
afterEach(() => {
// stop watching the files and folders
clearCaches();
// clean up the temporary files and folders
tmpDirs.forEach(t => t.removeCallback());
tmpDirs.clear();
});
function writeTSConfig(dirName: string, config: Record<string, unknown>): void {
fs.writeFileSync(path.join(dirName, 'tsconfig.json'), JSON.stringify(config));
}
function writeFile(dirName: string, file: 'foo' | 'bar' | 'baz/bar'): 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: 'foo' | 'bar' | 'baz/bar', tmpDir: string): void {
parseAndGenerateServices(CONTENTS.foo, {
project: './tsconfig.json',
tsconfigRootDir: tmpDir,
filePath: path.join(tmpDir, 'src', `${filename}.ts`),
});
}
function baseTests(
tsConfigExcludeBar: Record<string, unknown>,
tsConfigIncludeAll: Record<string, unknown>,
): void {
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 = path.join('baz', 'bar') as '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 renaming of files', () => {
const PROJECT_DIR = setup(tsConfigIncludeAll, true);
const bazSlashBar = path.join('baz', 'bar') as '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);
expect(() => parseFile('foo', PROJECT_DIR)).not.toThrow();
expect(() => parseFile('bar', PROJECT_DIR)).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.
*/
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');
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 = path.join('baz', 'bar') as '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);
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);
});
});