Skip to content

Commit 5771663

Browse files
authored
feat: add allowReserved parser option (eslint#15387)
* feat: add `allowReserved` parser option Fixes eslint#15327 * update package.json
1 parent 32ac37a commit 5771663

4 files changed

Lines changed: 79 additions & 1 deletion

File tree

‎docs/user-guide/configuring/language-options.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ Parser options are set in your `.eslintrc.*` file by using the `parserOptions` p
187187

188188
* `ecmaVersion` - set to 3, 5 (default), 6, 7, 8, 9, 10, 11, 12, or 13 to specify the version of ECMAScript syntax you want to use. You can also set to 2015 (same as 6), 2016 (same as 7), 2017 (same as 8), 2018 (same as 9), 2019 (same as 10), 2020 (same as 11), 2021 (same as 12), or 2022 (same as 13) to use the year-based naming. You can also set "latest" to use the most recently supported version.
189189
* `sourceType` - set to `"script"` (default) or `"module"` if your code is in ECMAScript modules.
190+
* `allowReserved` - allow the use of reserved words as identifiers (if `ecmaVersion` is 3).
190191
* `ecmaFeatures` - an object indicating which additional language features you'd like to use:
191192
* `globalReturn` - allow `return` statements in the global scope
192193
* `impliedStrict` - enable global [strict mode](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode) (if `ecmaVersion` is 5 or greater)

‎lib/shared/types.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module.exports = {};
2323
* @property {EcmaFeatures} [ecmaFeatures] The optional features.
2424
* @property {3|5|6|7|8|9|10|11|12|13|2015|2016|2017|2018|2019|2020|2021|2022} [ecmaVersion] The ECMAScript version (or revision number).
2525
* @property {"script"|"module"} [sourceType] The source code type.
26+
* @property {boolean} [allowReserved] Allowing the use of reserved words as identifiers in ES3.
2627
*/
2728

2829
/**

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"eslint-scope": "^7.1.0",
6060
"eslint-utils": "^3.0.0",
6161
"eslint-visitor-keys": "^3.1.0",
62-
"espree": "^9.1.0",
62+
"espree": "^9.2.0",
6363
"esquery": "^1.4.0",
6464
"esutils": "^2.0.2",
6565
"fast-deep-equal": "^3.1.3",

‎tests/lib/linter/linter.js‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4437,6 +4437,82 @@ var a = "test2";
44374437
assert.strictEqual(messages.length, 0);
44384438
});
44394439

4440+
it("should not allow the use of reserved words as variable names in ES3", () => {
4441+
const code = "var char;";
4442+
const messages = linter.verify(code, { parserOptions: { ecmaVersion: 3 } }, filename);
4443+
4444+
assert.strictEqual(messages.length, 1);
4445+
assert.strictEqual(messages[0].severity, 2);
4446+
assert.isTrue(messages[0].fatal);
4447+
assert.match(messages[0].message, /^Parsing error:.*'char'/u);
4448+
});
4449+
4450+
it("should not allow the use of reserved words as property names in member expressions in ES3", () => {
4451+
const code = "obj.char;";
4452+
const messages = linter.verify(code, { parserOptions: { ecmaVersion: 3 } }, filename);
4453+
4454+
assert.strictEqual(messages.length, 1);
4455+
assert.strictEqual(messages[0].severity, 2);
4456+
assert.isTrue(messages[0].fatal);
4457+
assert.match(messages[0].message, /^Parsing error:.*'char'/u);
4458+
});
4459+
4460+
it("should not allow the use of reserved words as property names in object literals in ES3", () => {
4461+
const code = "var obj = { char: 1 };";
4462+
const messages = linter.verify(code, { parserOptions: { ecmaVersion: 3 } }, filename);
4463+
4464+
assert.strictEqual(messages.length, 1);
4465+
assert.strictEqual(messages[0].severity, 2);
4466+
assert.isTrue(messages[0].fatal);
4467+
assert.match(messages[0].message, /^Parsing error:.*'char'/u);
4468+
});
4469+
4470+
it("should allow the use of reserved words as variable and property names in ES3 when allowReserved is true", () => {
4471+
const code = "var char; obj.char; var obj = { char: 1 };";
4472+
const messages = linter.verify(code, { parserOptions: { ecmaVersion: 3, allowReserved: true } }, filename);
4473+
4474+
assert.strictEqual(messages.length, 0);
4475+
});
4476+
4477+
it("should not allow the use of reserved words as variable names in ES > 3", () => {
4478+
const ecmaVersions = [void 0, ...espree.supportedEcmaVersions.filter(ecmaVersion => ecmaVersion > 3)];
4479+
4480+
ecmaVersions.forEach(ecmaVersion => {
4481+
const code = "var enum;";
4482+
const messages = linter.verify(code, { parserOptions: { ecmaVersion } }, filename);
4483+
4484+
assert.strictEqual(messages.length, 1);
4485+
assert.strictEqual(messages[0].severity, 2);
4486+
assert.isTrue(messages[0].fatal);
4487+
assert.match(messages[0].message, /^Parsing error:.*'enum'/u);
4488+
});
4489+
});
4490+
4491+
it("should allow the use of reserved words as property names in ES > 3", () => {
4492+
const ecmaVersions = [void 0, ...espree.supportedEcmaVersions.filter(ecmaVersion => ecmaVersion > 3)];
4493+
4494+
ecmaVersions.forEach(ecmaVersion => {
4495+
const code = "obj.enum; obj.function; var obj = { enum: 1, function: 2 };";
4496+
const messages = linter.verify(code, { parserOptions: { ecmaVersion } }, filename);
4497+
4498+
assert.strictEqual(messages.length, 0);
4499+
});
4500+
});
4501+
4502+
it("should not allow `allowReserved: true` in ES > 3", () => {
4503+
const ecmaVersions = [void 0, ...espree.supportedEcmaVersions.filter(ecmaVersion => ecmaVersion > 3)];
4504+
4505+
ecmaVersions.forEach(ecmaVersion => {
4506+
const code = "";
4507+
const messages = linter.verify(code, { parserOptions: { ecmaVersion, allowReserved: true } }, filename);
4508+
4509+
assert.strictEqual(messages.length, 1);
4510+
assert.strictEqual(messages[0].severity, 2);
4511+
assert.isTrue(messages[0].fatal);
4512+
assert.match(messages[0].message, /^Parsing error:.*allowReserved/u);
4513+
});
4514+
});
4515+
44404516
it("should be able to use es6 features if there is a comment which has \"eslint-env es6\"", () => {
44414517
const code = [
44424518
"/* eslint-env es6 */",

0 commit comments

Comments
 (0)