Skip to content

Commit

Permalink
Update: improve error messaging when validating ecmaVersion (#421)
Browse files Browse the repository at this point in the history
* Update: improve error messaging when validating ecmaVersion

* Print found type in error message

* Inline typeof check
  • Loading branch information
kaicataldo authored and aladdin-add committed Aug 18, 2019
1 parent 3f49224 commit 9870c55
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
42 changes: 22 additions & 20 deletions lib/espree.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,30 @@ const tokTypes = Object.assign({}, acorn.tokTypes, jsx.tokTypes);
* @returns {number} normalized ECMAScript version
*/
function normalizeEcmaVersion(ecmaVersion = DEFAULT_ECMA_VERSION) {
if (typeof ecmaVersion === "number") {
let version = ecmaVersion;
if (typeof ecmaVersion !== "number") {
throw new Error(`ecmaVersion must be a number. Received value of type ${typeof ecmaVersion} instead.`);
}

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

switch (version) {
case 3:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
return version;

// no default
}
// Calculate ECMAScript edition number from official year version starting with
// ES2015, which corresponds with ES6 (or a difference of 2009).
if (version >= 2015) {
version -= 2009;
}

switch (version) {
case 3:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
return version;

// no default
}

throw new Error("Invalid ecmaVersion.");
Expand Down
2 changes: 1 addition & 1 deletion tests/lib/ecma-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ describe("ecmaVersion", () => {
loc: true
}
);
}, /Invalid ecmaVersion/);
}, /ecmaVersion must be a number. Received value of type string instead/);
});

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

0 comments on commit 9870c55

Please sign in to comment.