Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: improve error messaging when validating ecmaVersion #421

Merged
merged 3 commits into from
Aug 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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