forked from typescript-eslint/typescript-eslint
/
parse.ts
241 lines (210 loc) · 7.22 KB
/
parse.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
import * as parser from '../../src/parser';
import * as astConverter from '../../src/ast-converter';
import { TSESTreeOptions } from '../../src/parser-options';
import { createSnapshotTestBlock } from '../../tools/test-utils';
import { join } from 'path';
const FIXTURES_DIR = './tests/fixtures/simpleProject';
describe('parse()', () => {
describe('basic functionality', () => {
it('should parse an empty string', () => {
expect(parser.parse('').body).toEqual([]);
expect(parser.parse('', {}).body).toEqual([]);
});
});
describe('modules', () => {
it('should have correct column number when strict mode error occurs', () => {
try {
parser.parse('function fn(a, a) {\n}', { sourceType: 'module' } as any);
} catch (err) {
expect(err.column).toEqual(16);
}
});
});
describe('general', () => {
const code = 'let foo = bar;';
const config: TSESTreeOptions = {
comment: true,
tokens: true,
range: true,
loc: true,
};
it(
'output tokens, comments, locs, and ranges when called with those options',
createSnapshotTestBlock(code, config),
);
});
describe('non string code', () => {
const code = (12345 as any) as string;
const config: TSESTreeOptions = {
comment: true,
tokens: true,
range: true,
loc: true,
};
it(
'should correctly convert code to a string for parse()',
createSnapshotTestBlock(code, config),
);
it(
'should correctly convert code to a string for parseAndGenerateServices()',
createSnapshotTestBlock(code, config, true),
);
});
describe('loggerFn should be propagated to ast-converter', () => {
it('output tokens, comments, locs, and ranges when called with those options', () => {
const spy = jest.spyOn(astConverter, 'default');
const loggerFn = jest.fn(() => true);
parser.parse('let foo = bar;', {
loggerFn,
comment: true,
tokens: true,
range: true,
loc: true,
});
expect(spy).toHaveBeenCalledWith(
expect.any(Object),
{
code: 'let foo = bar;',
comment: true,
comments: [],
errorOnTypeScriptSyntacticAndSemanticIssues: false,
errorOnUnknownASTType: false,
extraFileExtensions: [],
jsx: false,
loc: true,
log: loggerFn,
projects: [],
range: true,
strict: false,
tokens: expect.any(Array),
tsconfigRootDir: expect.any(String),
useJSXTextNode: false,
preserveNodeMaps: false,
},
false,
);
});
});
describe('errorOnTypeScriptSyntacticAndSemanticIssues', () => {
const code = '@test const foo = 2';
const options: TSESTreeOptions = {
comment: true,
tokens: true,
range: true,
loc: true,
errorOnTypeScriptSyntacticAndSemanticIssues: true,
};
it('should throw on invalid option when used in parse', () => {
expect(() => {
parser.parse(code, options);
}).toThrow(
`"errorOnTypeScriptSyntacticAndSemanticIssues" is only supported for parseAndGenerateServices()`,
);
});
it('should not throw when used in parseAndGenerateServices', () => {
expect(() => {
parser.parseAndGenerateServices(code, options);
}).not.toThrow(
`"errorOnTypeScriptSyntacticAndSemanticIssues" is only supported for parseAndGenerateServices()`,
);
});
it('should error on invalid code', () => {
expect(() => {
parser.parseAndGenerateServices(code, options);
}).toThrow('Decorators are not valid here.');
});
});
describe('preserveNodeMaps', () => {
const code = 'var a = true';
const baseConfig: TSESTreeOptions = {
comment: true,
tokens: true,
range: true,
loc: true,
filePath: 'tests/fixtures/simpleProject/file.ts',
};
const projectConfig: TSESTreeOptions = {
...baseConfig,
tsconfigRootDir: join(process.cwd(), FIXTURES_DIR),
project: './tsconfig.json',
};
it('should not impact the use of parse()', () => {
const resultWithNoOptionSet = parser.parse(code, baseConfig);
const resultWithOptionSetToTrue = parser.parse(code, {
...baseConfig,
preserveNodeMaps: true,
});
const resultWithOptionSetToFalse = parser.parse(code, {
...baseConfig,
preserveNodeMaps: false,
});
const resultWithOptionSetExplicitlyToUndefined = parser.parse(code, {
...baseConfig,
preserveNodeMaps: undefined,
});
expect(resultWithNoOptionSet).toMatchObject(resultWithOptionSetToTrue);
expect(resultWithNoOptionSet).toMatchObject(resultWithOptionSetToFalse);
expect(resultWithNoOptionSet).toMatchObject(
resultWithOptionSetExplicitlyToUndefined,
);
});
it('should not preserve node maps by default for parseAndGenerateServices(), unless `project` is set', () => {
const noOptionSet = parser.parseAndGenerateServices(code, baseConfig);
expect(noOptionSet.services.esTreeNodeToTSNodeMap).toBeUndefined();
expect(noOptionSet.services.tsNodeToESTreeNodeMap).toBeUndefined();
const withProjectNoOptionSet = parser.parseAndGenerateServices(
code,
projectConfig,
);
expect(withProjectNoOptionSet.services.esTreeNodeToTSNodeMap).toEqual(
expect.any(WeakMap),
);
expect(withProjectNoOptionSet.services.tsNodeToESTreeNodeMap).toEqual(
expect.any(WeakMap),
);
});
it('should preserve node maps for parseAndGenerateServices() when option is `true`, regardless of `project` config', () => {
const optionSetToTrue = parser.parseAndGenerateServices(code, {
...baseConfig,
preserveNodeMaps: true,
});
expect(optionSetToTrue.services.esTreeNodeToTSNodeMap).toEqual(
expect.any(WeakMap),
);
expect(optionSetToTrue.services.tsNodeToESTreeNodeMap).toEqual(
expect.any(WeakMap),
);
const withProjectOptionSetToTrue = parser.parseAndGenerateServices(code, {
...projectConfig,
preserveNodeMaps: true,
});
expect(withProjectOptionSetToTrue.services.esTreeNodeToTSNodeMap).toEqual(
expect.any(WeakMap),
);
expect(withProjectOptionSetToTrue.services.tsNodeToESTreeNodeMap).toEqual(
expect.any(WeakMap),
);
});
it('should not preserve node maps for parseAndGenerateServices() when option is `false`, regardless of `project` config', () => {
const optionSetToFalse = parser.parseAndGenerateServices(code, {
...baseConfig,
preserveNodeMaps: false,
});
expect(optionSetToFalse.services.esTreeNodeToTSNodeMap).toBeUndefined();
expect(optionSetToFalse.services.tsNodeToESTreeNodeMap).toBeUndefined();
const withProjectOptionSetToFalse = parser.parseAndGenerateServices(
code,
{
...projectConfig,
preserveNodeMaps: false,
},
);
expect(
withProjectOptionSetToFalse.services.esTreeNodeToTSNodeMap,
).toBeUndefined();
expect(
withProjectOptionSetToFalse.services.tsNodeToESTreeNodeMap,
).toBeUndefined();
});
});
});