Skip to content

Commit

Permalink
Breaking: Disallow reserved words in ES3 (#513)
Browse files Browse the repository at this point in the history
* Breaking: Disallow reserved words in ES3

* Switch allowReserved to false for ES > 3
  • Loading branch information
nzakas committed Sep 10, 2021
1 parent ee1d3ec commit 7952c6f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/espree.js
Expand Up @@ -71,6 +71,7 @@ export default () => Parser => {
sourceType: options.sourceType,
ranges: options.ranges,
locations: options.locations,
allowReserved: options.allowReserved,

// Truthy value is true for backward compatibility.
allowReturnOutsideFunction: Boolean(ecmaFeatures.globalReturn),
Expand Down
9 changes: 8 additions & 1 deletion lib/options.js
Expand Up @@ -87,9 +87,16 @@ export function normalizeOptions(options) {
const sourceType = normalizeSourceType(options.sourceType);
const ranges = options.range === true;
const locations = options.loc === true;
const allowReserved = ecmaVersion === 3 ? "never" : false;

if (sourceType === "module" && ecmaVersion < 6) {
throw new Error("sourceType 'module' is not supported when ecmaVersion < 2015. Consider adding `{ ecmaVersion: 2015 }` to the parser options.");
}
return Object.assign({}, options, { ecmaVersion, sourceType, ranges, locations });
return Object.assign({}, options, {
ecmaVersion,
sourceType,
ranges,
locations,
allowReserved
});
}
20 changes: 20 additions & 0 deletions tests/lib/ecma-version.js
Expand Up @@ -176,6 +176,26 @@ describe("ecmaVersion", () => {
}, /ecmaVersion must be a number or "latest". Received value of type string instead/u);
});

it("Should throw error when using ES3 and reserved words", () => {
assert.throws(() => {
espree.parse(
"var char = 'c'", {
ecmaVersion: 3
}
);
}, /'char' is reserved/u);
});

it("Should throw error when using ES3 and reserved words in object literals", () => {
assert.throws(() => {
espree.parse(
"var x = { char: 'c' }", {
ecmaVersion: 3
}
);
}, /'char' is reserved/u);
});

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

0 comments on commit 7952c6f

Please sign in to comment.