forked from eslint/espree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.js
176 lines (136 loc) · 6.49 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
/**
* @fileoverview Tests for tokenize().
* @author Nicholas C. Zakas
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const assert = require("assert"),
espree = require("../../espree"),
tester = require("./tester");
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
describe("parse()", () => {
describe("ecmaVersion", () => {
it("should be 5 if not specified", () => {
// `ecmaVersion: 3` would throw on getters/setters
espree.parse("var foo = { get bar() {} }");
// needs `ecmaVersion: 6` or higher
assert.throws(() => {
espree.parse("let foo");
});
});
});
describe("modules", () => {
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("ES5", () => {
it("should throw an error when using the y regex flag", () => {
assert.throws(() => {
espree.parse("/./y");
});
});
it("should throw an error when using the u regex flag", () => {
assert.throws(() => {
espree.parse("/./u");
});
});
});
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), require("../fixtures/parse/all-pieces.json"));
});
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 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[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 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({}) }));
});
});
});