From 7952c6fd4155ce88ec726eac69cadd3bc5b3f3a5 Mon Sep 17 00:00:00 2001 From: "Nicholas C. Zakas" Date: Fri, 10 Sep 2021 04:20:34 -0700 Subject: [PATCH] Breaking: Disallow reserved words in ES3 (#513) * Breaking: Disallow reserved words in ES3 * Switch allowReserved to false for ES > 3 --- lib/espree.js | 1 + lib/options.js | 9 ++++++++- tests/lib/ecma-version.js | 20 ++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/espree.js b/lib/espree.js index aec180d7..e0be3b5a 100644 --- a/lib/espree.js +++ b/lib/espree.js @@ -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), diff --git a/lib/options.js b/lib/options.js index ae441ac3..acc1e3a2 100644 --- a/lib/options.js +++ b/lib/options.js @@ -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 + }); } diff --git a/tests/lib/ecma-version.js b/tests/lib/ecma-version.js index 179afa49..05d76e0f 100644 --- a/tests/lib/ecma-version.js +++ b/tests/lib/ecma-version.js @@ -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(