Skip to content

Commit 9870c55

Browse files
kaicataldoaladdin-add
authored andcommitted
Update: improve error messaging when validating ecmaVersion (#421)
* Update: improve error messaging when validating ecmaVersion * Print found type in error message * Inline typeof check
1 parent 3f49224 commit 9870c55

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

lib/espree.js

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,30 @@ const tokTypes = Object.assign({}, acorn.tokTypes, jsx.tokTypes);
1818
* @returns {number} normalized ECMAScript version
1919
*/
2020
function normalizeEcmaVersion(ecmaVersion = DEFAULT_ECMA_VERSION) {
21-
if (typeof ecmaVersion === "number") {
22-
let version = ecmaVersion;
21+
if (typeof ecmaVersion !== "number") {
22+
throw new Error(`ecmaVersion must be a number. Received value of type ${typeof ecmaVersion} instead.`);
23+
}
2324

24-
// Calculate ECMAScript edition number from official year version starting with
25-
// ES2015, which corresponds with ES6 (or a difference of 2009).
26-
if (version >= 2015) {
27-
version -= 2009;
28-
}
25+
let version = ecmaVersion;
2926

30-
switch (version) {
31-
case 3:
32-
case 5:
33-
case 6:
34-
case 7:
35-
case 8:
36-
case 9:
37-
case 10:
38-
case 11:
39-
return version;
40-
41-
// no default
42-
}
27+
// Calculate ECMAScript edition number from official year version starting with
28+
// ES2015, which corresponds with ES6 (or a difference of 2009).
29+
if (version >= 2015) {
30+
version -= 2009;
31+
}
32+
33+
switch (version) {
34+
case 3:
35+
case 5:
36+
case 6:
37+
case 7:
38+
case 8:
39+
case 9:
40+
case 10:
41+
case 11:
42+
return version;
43+
44+
// no default
4345
}
4446

4547
throw new Error("Invalid ecmaVersion.");

tests/lib/ecma-version.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ describe("ecmaVersion", () => {
152152
loc: true
153153
}
154154
);
155-
}, /Invalid ecmaVersion/);
155+
}, /ecmaVersion must be a number. Received value of type string instead/);
156156
});
157157

158158
it("Should throw error when using module in pre-ES6", () => {

0 commit comments

Comments
 (0)