-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
parser.ts
105 lines (98 loc) · 3.27 KB
/
parser.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
import * as scopeManager from '@typescript-eslint/scope-manager';
import type { ParserOptions } from '@typescript-eslint/types';
import * as typescriptESTree from '@typescript-eslint/typescript-estree';
import { parse, parseForESLint } from '../../src/parser';
describe('parser', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('parse() should return just the AST from parseForESLint()', () => {
const code = 'const valid = true;';
expect(parse(code)).toEqual(parseForESLint(code).ast);
});
it('parseForESLint() should work if options are `null`', () => {
const code = 'const valid = true;';
expect(() => parseForESLint(code, null)).not.toThrow();
});
it('parseAndGenerateServices() should be called with options', () => {
const code = 'const valid = true;';
const spy = jest.spyOn(typescriptESTree, 'parseAndGenerateServices');
const config: ParserOptions = {
loc: false,
comment: false,
range: false,
tokens: false,
sourceType: 'module' as const,
ecmaFeatures: {
globalReturn: false,
jsx: false,
},
// ts-estree specific
filePath: 'isolated-file.src.ts',
project: 'tsconfig.json',
errorOnTypeScriptSyntacticAndSemanticIssues: false,
tsconfigRootDir: 'tests/fixtures/services',
extraFileExtensions: ['.foo'],
};
parseForESLint(code, config);
expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenLastCalledWith(code, {
jsx: false,
...config,
});
});
it('`warnOnUnsupportedTypeScriptVersion: false` should set `loggerFn: false` on typescript-estree', () => {
const code = 'const valid = true;';
const spy = jest.spyOn(typescriptESTree, 'parseAndGenerateServices');
parseForESLint(code, { warnOnUnsupportedTypeScriptVersion: true });
expect(spy).toHaveBeenCalledWith(code, {
ecmaFeatures: {},
jsx: false,
sourceType: 'script',
warnOnUnsupportedTypeScriptVersion: true,
});
spy.mockClear();
parseForESLint(code, { warnOnUnsupportedTypeScriptVersion: false });
expect(spy).toHaveBeenCalledWith(code, {
ecmaFeatures: {},
jsx: false,
sourceType: 'script',
loggerFn: false,
warnOnUnsupportedTypeScriptVersion: false,
});
});
it('analyze() should be called with options', () => {
const code = 'const valid = true;';
const spy = jest.spyOn(scopeManager, 'analyze');
const config: ParserOptions = {
loc: false,
comment: false,
range: false,
tokens: false,
sourceType: 'module' as const,
ecmaFeatures: {
globalReturn: false,
jsx: false,
},
// scope-manager specific
lib: ['dom.iterable'],
jsxPragma: 'Foo',
jsxFragmentName: 'Bar',
// ts-estree specific
filePath: 'isolated-file.src.ts',
project: 'tsconfig.json',
errorOnTypeScriptSyntacticAndSemanticIssues: false,
tsconfigRootDir: 'tests/fixtures/services',
extraFileExtensions: ['.foo'],
};
parseForESLint(code, config);
expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenLastCalledWith(expect.anything(), {
globalReturn: false,
lib: ['dom.iterable'],
jsxPragma: 'Foo',
jsxFragmentName: 'Bar',
sourceType: 'module',
});
});
});