-
Notifications
You must be signed in to change notification settings - Fork 176
/
parser.test.ts
246 lines (220 loc) · 7.99 KB
/
parser.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
import {
parse,
ParseException,
PARSER_CONFIG_MODULE,
PARSER_CONFIG_SCRIPT,
parseJavaScriptSourceFile,
parseTypeScriptSourceFile,
parseVueSourceFile,
checkTypeScriptVersionCompatibility,
ParseExceptionCode,
parseExceptionCodeOf,
} from "../src/parser";
import * as espree from "espree";
import { SourceCode } from "eslint";
import { ParsingError } from "../src/analyzer";
import visit from "../src/utils/visitor";
import * as path from "path";
describe("parseJavaScriptSourceFile", () => {
beforeEach(() => {
console.error = jest.fn();
});
afterEach(() => {
jest.resetAllMocks();
});
it("should parse jsx", () => {
expectToParse("const foo = <div>bar</div>;");
});
it("should parse flow when with @flow", () => {
expectToParse("/* @flow */ const foo: string = 'hello';");
expectToParse("/* @flow */ var eval = 42");
// even without @flow annotation
expectToParse("const foo: string = 'hello';");
});
it("should parse as script (non-strict mode)", () => {
expectToParseInNonStrictMode(`var eval = 42`, `Binding eval in strict mode`);
expectToParseInNonStrictMode(`eval = 42`, `Assigning to eval in strict mode`);
expectToParseInNonStrictMode(
`function foo() {}\n var foo = 42;`,
`Identifier 'foo' has already been declared`,
);
expectToParseInNonStrictMode(`x = 043;`, `Invalid number`);
expectToParseInNonStrictMode(`'\\033'`, `Octal literal in strict mode`);
expectToParseInNonStrictMode(`with (a) {}`, `'with' in strict mode`);
expectToParseInNonStrictMode(`public = 42`, `The keyword 'public' is reserved`);
expectToParseInNonStrictMode(`function foo(a, a) {}`, `Argument name clash`);
expectToParseInNonStrictMode(`delete x`, `Deleting local variable in strict mode`);
});
it("should parse recent javascript syntax", () => {
// ES2018
expectToParse(
`const obj = {foo: 1, bar: 2, baz: 3};
const {foo, ...rest} = obj;`,
);
// ES2017
expectToParse(
`async function f() {
await readFile();
}`,
);
// ES2016
expectToParse(`4**2`);
// ES2015
expectToParse(`const f = (x, y) => x + y`);
// Modules
expectToParse(
`import * as Foo from "foo";
import { ParseExceptionCode } from '../src/parser';
export class A{}`,
);
});
it("should parse next javascript syntax", () => {
let sourceCode;
// ES2019
sourceCode = parseJavaScriptSourceFile(`try {} catch {}`);
expect(sourceCode.ast.body.length).toBeGreaterThan(0);
// next
// class fields
expectToParse(`class A {
static a = 1;
b = 2
}`);
// private fields are not supported
expectToNotParse(
`class A { static #x = 2
#privateMethod() { this.#privateField = 42; }
#privateField = 42
set #x(value) {} }`,
"Unexpected character '#'",
);
});
it("should return ParsingError when parse errors", () => {
expectToNotParse("if()", "Unexpected token )");
expectToNotParse("/* @flow */ if()", "Unexpected token (1:15)");
});
});
describe("parseTypeScriptSourceFile", () => {
it("should parse typescript syntax", () => {
const file = __dirname + "/fixtures/ts-project/sample.lint.ts";
const sourceCode = parseTypeScriptSourceFile(
`if (b == 0) { // Noncompliant
doOneMoreThing();
} else {
doOneMoreThing();
}
`,
file,
[__dirname + "/fixtures/ts-project/tsconfig.json"],
) as SourceCode;
expect(sourceCode.ast).toBeDefined();
expect(sourceCode.parserServices.program).toBeDefined();
const program = sourceCode.parserServices.program;
expect(program.getTypeChecker()).toBeDefined();
});
it("should log parse error with typescript", () => {
const file = __dirname + "/fixtures/ts-project/sample.error.lint.ts";
const parsingError = parseTypeScriptSourceFile(`if (b == 0) {`, file, []) as ParsingError;
expect(parsingError).toBeDefined();
expect(parsingError.line).toEqual(1);
expect(parsingError.message).toEqual("'}' expected.");
expect(parsingError.code).toEqual(ParseExceptionCode.Parsing);
});
it("should return ParsingError with undefined line when file is not part of typescript project", () => {
const file = path.join(path.basename(__dirname), "/fixtures/ts-project/excluded.ts");
const parsingError = parseTypeScriptSourceFile(`if (b == 0) {}`, file, [
__dirname + "/fixtures/ts-project/tsconfig.json",
]) as ParsingError;
expect(parsingError).toBeDefined();
expect(parsingError.line).toBeUndefined();
expect(parsingError.message).toEqual(
`\"parserOptions.project\" has been set for @typescript-eslint/parser.\nThe file does not match your project config: ${file}.\nThe file must be included in at least one of the projects provided.`,
);
});
it("should throw a parsing exception with TypeScript version below minimum expected", () => {
let parsingException = undefined;
try {
checkTypeScriptVersionCompatibility("1.2.3");
} catch (exception) {
parsingException = exception;
}
expect(parsingException).toBeDefined;
expect(parsingException).toEqual({
message:
"You are using version of TypeScript 1.2.3 which is not supported; supported versions >=3.2.1",
});
});
it("should log a warning with TypeScript version above maximum expected", () => {
console.log = jest.fn();
checkTypeScriptVersionCompatibility("3.8.5");
expect(console.log).toHaveBeenCalledWith(
"WARN You are using version of TypeScript 3.8.5 which is not officially supported; supported versions >=3.2.1 <3.8.0",
);
jest.resetAllMocks();
});
it("should return correct parsing exception code from exception message", () => {
expect(parseExceptionCodeOf("Cannot find module 'typescript'")).toEqual(
ParseExceptionCode.MissingTypeScript,
);
expect(parseExceptionCodeOf("You are using version of TypeScript")).toEqual(
ParseExceptionCode.UnsupportedTypeScript,
);
expect(parseExceptionCodeOf("Unexpected token )")).toEqual(ParseExceptionCode.Parsing);
});
});
describe("parseVueSourceFile", () => {
it("should parse Vue.js syntax", () => {
const code = `
module.exports = {
data: function () {
return {
foo: 'bar'
}
}
}`;
const parsedJS = parseJavaScriptSourceFile(code) as SourceCode;
const parsedVueJS = parseVueSourceFile(`
<template>
<p>{{foo}}</p>
</template>
<script>
${code}
</script>
<style>
p { text-align: center; }
</style>
`) as SourceCode;
const expected = [],
actual = [];
visit(parsedJS, node => expected.push(node.type));
visit(parsedVueJS, node => actual.push(node.type));
expect(actual).toEqual(expected);
});
it("should log parse error with Vue.js", () => {
const parsingError = parseVueSourceFile(`
<script>
module.exports = {
</script>`) as ParsingError;
expect(parsingError).toBeDefined();
expect(parsingError.line).toEqual(4);
expect(parsingError.message).toEqual("Unexpected token");
expect(parsingError.code).toEqual(ParseExceptionCode.Parsing);
});
});
function expectToParse(code: string) {
const sourceCode = parseJavaScriptSourceFile(code) as SourceCode;
expect(sourceCode).toBeDefined();
expect(sourceCode.ast.body.length).toBeGreaterThan(0);
expect(console.error).toBeCalledTimes(0);
}
function expectToNotParse(code: string, message: string) {
const parsingError = parseJavaScriptSourceFile(code) as ParsingError;
expect(parsingError).toBeDefined();
expect(parsingError.line).toEqual(1);
expect(parsingError.message).toEqual(message);
}
function expectToParseInNonStrictMode(code: string, msgInStrictMode: string) {
const result1 = parse(espree.parse, PARSER_CONFIG_MODULE, code);
expect((result1 as ParseException).message).toEqual(msgInStrictMode);
const result2 = parse(espree.parse, PARSER_CONFIG_SCRIPT, code);
expect((result2 as SourceCode).ast.body.length).toBeGreaterThan(0);
}