-
-
Notifications
You must be signed in to change notification settings - Fork 189
/
ecma-version.js
220 lines (170 loc) · 7.16 KB
/
ecma-version.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
/**
* @fileoverview Tests for ECMAScript version features.
* @author Nicholas C. Zakas
*/
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
import fs from "fs";
import path from "path";
import shelljs from "shelljs";
import tester from "./tester.js";
import * as espree from "../../espree.js";
import assert from "assert";
import { fileURLToPath, pathToFileURL } from "url";
// eslint-disable-next-line no-underscore-dangle
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const allPiecesJson = JSON.parse(fs.readFileSync(`${__dirname}/../fixtures/parse/all-pieces.json`, "utf8"));
// var espree = require("esprima-fb");
//------------------------------------------------------------------------------
// Setup
//------------------------------------------------------------------------------
const FIXTURES_DIR = path.resolve(__dirname, "..", "fixtures/ecma-version");
const allTestFiles = shelljs.find(FIXTURES_DIR)
.filter(filename => filename.includes(".src.js"))
.map(filename => filename.slice(FIXTURES_DIR.length, filename.length - 7)); // strip off ".src.js"
const scriptOnlyTestFiles = allTestFiles.filter(filename => !filename.includes("modules"));
const moduleTestFiles = allTestFiles.filter(filename => !filename.includes("not-strict") && !filename.includes("edge-cases"));
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
describe("ecmaVersion", () => {
let config;
beforeEach(() => {
config = {
loc: true,
range: true,
tokens: true,
ecmaVersion: 5
};
});
describe("Scripts", () => {
scriptOnlyTestFiles.forEach(filename => {
describe(filename, () => {
const version = filename.slice(1, filename.indexOf("/", 1));
// Uncomment and fill in filename to focus on a single file
// var filename = "newTarget/simple-new-target";
const code = shelljs.cat(`${FIXTURES_DIR}/${filename}.src.js`);
it("should parse correctly when sourceType is script", async () => {
config.ecmaVersion = Number(version);
const absolutePath = path.resolve(__dirname, FIXTURES_DIR, filename.slice(1));
// eslint-disable-next-line node/no-unsupported-features/es-syntax
const expected = await import(`${pathToFileURL(absolutePath).href}.result.js`);
tester.assertMatches(code, config, expected.default);
});
});
});
});
describe("Modules", () => {
moduleTestFiles.forEach(filename => {
describe(filename, () => {
const version = filename.slice(1, filename.indexOf("/", 1));
const code = shelljs.cat(`${FIXTURES_DIR}/${filename}.src.js`);
it("should parse correctly when sourceType is module", async () => {
const absolutePath = path.resolve(__dirname, FIXTURES_DIR, filename.slice(1));
let expected;
try {
// eslint-disable-next-line node/no-unsupported-features/es-syntax
expected = await import(`${pathToFileURL(absolutePath).href}.module-result.js`);
} catch {
// eslint-disable-next-line node/no-unsupported-features/es-syntax
expected = await import(`${pathToFileURL(absolutePath).href}.result.js`);
}
if (expected) {
expected = expected.default;
}
config.ecmaVersion = Number(version);
config.sourceType = "module";
// set sourceType of program node to module
if (expected.type === "Program") {
expected.sourceType = "module";
}
tester.assertMatches(code, config, expected);
});
});
});
});
describe("general", () => {
it("Should parse using 2015 instead of 6", () => {
const ast = espree.parse("let foo = bar;", {
ecmaVersion: 2015,
comment: true,
tokens: true,
range: true,
loc: true
});
assert.deepStrictEqual(tester.getRaw(ast), allPiecesJson);
});
it("Should throw error using invalid number", () => {
assert.throws(() => {
espree.parse(
"let foo = bar;", {
ecmaVersion: 32,
comment: true,
tokens: true,
range: true,
loc: true
}
);
}, /Invalid ecmaVersion/u);
});
it("Should throw error using invalid year", () => {
assert.throws(() => {
espree.parse(
"let foo = bar;", {
ecmaVersion: 2050,
comment: true,
tokens: true,
range: true,
loc: true
}
);
}, /Invalid ecmaVersion/u);
});
it("Should throw error when non-numeric year is provided", () => {
assert.throws(() => {
espree.parse(
"let foo = bar;", {
ecmaVersion: "2015",
comment: true,
tokens: true,
range: true,
loc: true
}
);
}, /ecmaVersion must be a number or "latest". Received value of type string instead/u);
});
it("Should throw error when using module in pre-ES6", () => {
assert.throws(() => {
espree.parse(
"let foo = bar;", {
ecmaVersion: 5,
sourceType: "module"
}
);
}, /sourceType 'module' is not supported when ecmaVersion < 2015/u);
});
it("Should allow 'latest' as value", () => {
const expected = espree.parse(
"let foo = bar;", {
ecmaVersion: espree.latestEcmaVersion,
sourceType: "module"
}
);
const actual = espree.parse(
"let foo = bar;", {
ecmaVersion: "latest",
sourceType: "module"
}
);
assert.deepStrictEqual(actual, expected);
});
it("Should use the 5 as the default for ecmaVersion", () => {
assert.throws(() => {
espree.parse(
"let foo = bar;"
);
}, /Unexpected token foo/u);
});
});
});