/
parse.js
264 lines (202 loc) · 10 KB
/
parse.js
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
/**
* @fileoverview Tests for parse().
* @author Nicholas C. Zakas
*/
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
import fs from "fs";
import tester from "./tester.js";
import * as espree from "../../espree.js";
import assert from "assert";
const allPiecesJson = JSON.parse(fs.readFileSync("./tests/fixtures/parse/all-pieces.json", "utf8"));
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
describe("parse()", () => {
describe("ecmaVersion", () => {
it("should be 5 if not specified", () => {
// needs `ecmaVersion: 5` or higher (`ecmaVersion: 3` would throw on getters/setters)
espree.parse("var foo = { get bar() {} }");
assert.throws(() => {
espree.parse(
// needs `ecmaVersion: 6` or higher
"let foo = bar;"
);
}, /Unexpected token foo/u);
});
});
describe("sourceType", () => {
describe("module", () => {
it("should have correct column number when strict mode error occurs", () => {
try {
espree.parse("function fn(a, a) {\n}", { ecmaVersion: 6, sourceType: "module" });
} catch (err) {
assert.strictEqual(err.column, 16);
}
});
});
describe("commonjs", () => {
it("should parse top-level return", () => {
espree.parse("return;", { sourceType: "commonjs" });
});
it("should have sourceType:commonjs on Program node", () => {
const result = espree.parse("return;", { sourceType: "commonjs" });
assert.strictEqual(result.sourceType, "commonjs");
});
});
});
describe("ES5", () => {
it("should throw an error when using the y regex flag", () => {
assert.throws(() => {
espree.parse("/./y", { ecmaVersion: 5 });
});
});
it("should throw an error when using the u regex flag", () => {
assert.throws(() => {
espree.parse("/./u", { ecmaVersion: 5 });
});
});
});
describe("general", () => {
it("should output tokens, comments, locs, and ranges when called with those options", () => {
const ast = espree.parse("let foo = bar;", {
ecmaVersion: 6,
comment: true,
tokens: true,
range: true,
loc: true
});
assert.deepStrictEqual(tester.getRaw(ast), allPiecesJson);
});
it("should output the same value for program.start, end and range when there is a leading/trailing comments", () => {
const ast = espree.parse("/* foo */ bar /* baz */", {
range: true
});
assert.strictEqual(ast.start, ast.range[0]);
assert.strictEqual(ast.end, ast.range[1]);
});
it("should output the same value for program.start, end as when ranges are returned and there is a leading/trailing comments using default options", () => {
const ast = espree.parse("/* foo */ bar /* baz */");
assert.strictEqual(ast.start, 10);
assert.strictEqual(ast.end, 13);
});
it("should output the same value for program.start, end and range and loc when there is a leading comments with range and loc true", () => {
const ast = espree.parse("/* foo */ bar", {
range: true,
loc: true
});
assert.strictEqual(ast.start, ast.range[0]);
assert.strictEqual(ast.end, ast.range[1]);
assert.strictEqual(ast.start, ast.loc.start.column);
assert.strictEqual(ast.end, ast.loc.end.column);
});
it("should output the same value for program.start, end and loc when there is a leading comments with loc", () => {
const ast = espree.parse("/* foo */ bar", {
loc: true
});
assert.strictEqual(ast.start, ast.loc.start.column);
assert.strictEqual(ast.end, ast.loc.end.column);
});
it("should output the same value for program.start, end and range when there is a trailing comments", () => {
const ast = espree.parse(" bar /* foo */", {
range: true
});
assert.strictEqual(ast.start, ast.range[0]);
assert.strictEqual(ast.end, ast.range[1]);
});
it("should output the same value for range and start and end in templateElement with range", () => {
const ast = espree.parse("`foo ${bar}`;", {
ecmaVersion: 6,
range: true
});
assert.strictEqual(ast.body[0].expression.quasis[0].start, ast.body[0].expression.quasis[0].range[0]);
assert.strictEqual(ast.body[0].expression.quasis[0].end, ast.body[0].expression.quasis[0].range[1]);
assert.strictEqual(ast.body[0].expression.quasis[1].start, ast.body[0].expression.quasis[1].range[0]);
assert.strictEqual(ast.body[0].expression.quasis[1].end, ast.body[0].expression.quasis[1].range[1]);
});
it("should output the same value for loc and start and end in templateElement with loc", () => {
const ast = espree.parse("`foo ${bar}`;", {
ecmaVersion: 6,
loc: true
});
assert.strictEqual(ast.body[0].expression.quasis[0].start, ast.body[0].expression.quasis[0].loc.start.column);
assert.strictEqual(ast.body[0].expression.quasis[0].end, ast.body[0].expression.quasis[0].loc.end.column);
assert.strictEqual(ast.body[0].expression.quasis[1].start, ast.body[0].expression.quasis[1].loc.start.column);
assert.strictEqual(ast.body[0].expression.quasis[1].end, ast.body[0].expression.quasis[1].loc.end.column);
});
it("should output the same value for loc, range and start and end in templateElement with loc and range", () => {
const ast = espree.parse("`foo ${bar}`;", {
ecmaVersion: 6,
loc: true,
range: true
});
assert.strictEqual(ast.body[0].expression.quasis[0].start, ast.body[0].expression.quasis[0].loc.start.column);
assert.strictEqual(ast.body[0].expression.quasis[0].end, ast.body[0].expression.quasis[0].loc.end.column);
assert.strictEqual(ast.body[0].expression.quasis[0].start, ast.body[0].expression.quasis[0].range[0]);
assert.strictEqual(ast.body[0].expression.quasis[0].end, ast.body[0].expression.quasis[0].range[1]);
assert.strictEqual(ast.body[0].expression.quasis[1].start, ast.body[0].expression.quasis[1].loc.start.column);
assert.strictEqual(ast.body[0].expression.quasis[1].end, ast.body[0].expression.quasis[1].loc.end.column);
assert.strictEqual(ast.body[0].expression.quasis[1].start, ast.body[0].expression.quasis[1].range[0]);
assert.strictEqual(ast.body[0].expression.quasis[1].end, ast.body[0].expression.quasis[1].range[1]);
});
it("should output the same value for start and end in templateElement as when ranges are present", () => {
const ast = espree.parse("`foo ${bar}`;", {
ecmaVersion: 6
});
assert.strictEqual(ast.body[0].expression.quasis[0].start, 0);
assert.strictEqual(ast.body[0].expression.quasis[0].end, 7);
assert.strictEqual(ast.body[0].expression.quasis[1].start, 10);
assert.strictEqual(ast.body[0].expression.quasis[1].end, 12);
});
it("should reset lastToken on each parse", () => {
espree.parse("var foo = bar;");
const ast = espree.parse("//foo", {
comment: true,
tokens: true,
range: true,
loc: true
});
assert.deepStrictEqual(ast.range, [0, 5]);
assert.deepStrictEqual([ast.loc.start.line, ast.loc.start.column], [1, 0]);
assert.deepStrictEqual([ast.loc.end.line, ast.loc.end.column], [1, 5]);
});
it("should not mutate config", () => {
espree.parse("foo", Object.freeze({ ecmaFeatures: Object.freeze({}) }));
});
it("Cast valid non-string input", () => {
const str = "var foo = bar;";
assert.deepStrictEqual(
espree.parse(Buffer.from(str)),
espree.parse(str)
);
});
// https://github.com/eslint/espree/issues/470
it("Should throw on invalid `(a = 1) = t`", () => {
assert.throws(() => {
espree.parse("(a = 1) = t", { ecmaVersion: 6 });
});
});
// https://github.com/eslint/espree/issues/472
it("Should throw on invalid `async () => await 5 ** 6;`", () => {
assert.throws(() => {
espree.parse("async () => await 5 ** 6;", { ecmaVersion: 10 });
});
});
});
describe("nodes", () => {
it("should not re-use the same identifier node in shorthand properties", () => {
const code = "({x, y = 1} = {z})";
const ast = espree.parse(code, { ecmaVersion: 2015 });
const assignment = ast.body[0].expression;
const objectPattern = assignment.left;
const objectExpression = assignment.right;
const propertyX = objectPattern.properties[0];
const propertyY = objectPattern.properties[1];
const propertyZ = objectExpression.properties[0];
assert.notStrictEqual(propertyX.key, propertyX.value);
assert.notStrictEqual(propertyY.key, propertyY.value.left);
assert.notStrictEqual(propertyZ.key, propertyZ.value);
});
});
});