Skip to content

Commit 97d9bd2

Browse files
authored
Revert "Update: ecmaVersion defaults to 5, and allows "latest" (#14622)" (#14711)
This reverts commit 831f6b3.
1 parent a47e5e3 commit 97d9bd2

3 files changed

Lines changed: 14 additions & 86 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ For ES6 syntax, use `{ "parserOptions": { "ecmaVersion": 6 } }`; for new ES6 glo
187187

188188
Parser options are set in your `.eslintrc.*` file by using the `parserOptions` property. The available options are:
189189

190-
* `ecmaVersion` - set to 3, 5 (default), 6, 7, 8, 9, 10, 11, or 12 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), or 2021 (same as 12) to use the year-based naming. You can also set "latest" to use the most recently supported version.
190+
* `ecmaVersion` - set to 3, 5 (default), 6, 7, 8, 9, 10, 11, or 12 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), or 2021 (same as 12) to use the year-based naming.
191191
* `sourceType` - set to `"script"` (default) or `"module"` if your code is in ECMAScript modules.
192192
* `ecmaFeatures` - an object indicating which additional language features you'd like to use:
193193
* `globalReturn` - allow `return` statements in the global scope
@@ -199,7 +199,7 @@ Here's an example `.eslintrc.json` file:
199199
```json
200200
{
201201
"parserOptions": {
202-
"ecmaVersion": "latest",
202+
"ecmaVersion": 6,
203203
"sourceType": "module",
204204
"ecmaFeatures": {
205205
"jsx": true

lib/linter/linter.js

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ const
3737
const debug = require("debug")("eslint:linter");
3838
const MAX_AUTOFIX_PASSES = 10;
3939
const DEFAULT_PARSER_NAME = "espree";
40-
const DEFAULT_ECMA_VERSION = 5;
4140
const commentParser = new ConfigCommentParser();
4241
const DEFAULT_ERROR_LOC = { start: { line: 1, column: 0 }, end: { line: 1, column: 1 } };
4342

@@ -433,20 +432,10 @@ function getDirectiveComments(filename, ast, ruleMapper, warnInlineConfig) {
433432

434433
/**
435434
* Normalize ECMAScript version from the initial config
436-
* @param {Parser} parser The parser which uses this options.
437-
* @param {number} ecmaVersion ECMAScript version from the initial config
435+
* @param {number} ecmaVersion ECMAScript version from the initial config
438436
* @returns {number} normalized ECMAScript version
439437
*/
440-
function normalizeEcmaVersion(parser, ecmaVersion) {
441-
if (parser === espree) {
442-
if (ecmaVersion === void 0) {
443-
return DEFAULT_ECMA_VERSION;
444-
}
445-
446-
if (ecmaVersion === "latest") {
447-
return espree.latestEcmaVersion;
448-
}
449-
}
438+
function normalizeEcmaVersion(ecmaVersion) {
450439

451440
/*
452441
* Calculate ECMAScript edition number from official year version starting with
@@ -532,13 +521,12 @@ function normalizeVerifyOptions(providedOptions, config) {
532521

533522
/**
534523
* Combines the provided parserOptions with the options from environments
535-
* @param {Parser} parser The parser which uses this options.
524+
* @param {string} parserName The parser name which uses this options.
536525
* @param {ParserOptions} providedOptions The provided 'parserOptions' key in a config
537526
* @param {Environment[]} enabledEnvironments The environments enabled in configuration and with inline comments
538527
* @returns {ParserOptions} Resulting parser options after merge
539528
*/
540-
function resolveParserOptions(parser, providedOptions, enabledEnvironments) {
541-
529+
function resolveParserOptions(parserName, providedOptions, enabledEnvironments) {
542530
const parserOptionsFromEnv = enabledEnvironments
543531
.filter(env => env.parserOptions)
544532
.reduce((parserOptions, env) => merge(parserOptions, env.parserOptions), {});
@@ -554,7 +542,12 @@ function resolveParserOptions(parser, providedOptions, enabledEnvironments) {
554542
mergedParserOptions.ecmaFeatures = Object.assign({}, mergedParserOptions.ecmaFeatures, { globalReturn: false });
555543
}
556544

557-
mergedParserOptions.ecmaVersion = normalizeEcmaVersion(parser, mergedParserOptions.ecmaVersion);
545+
/*
546+
* TODO: @aladdin-add
547+
* 1. for a 3rd-party parser, do not normalize parserOptions
548+
* 2. for espree, no need to do this (espree will do it)
549+
*/
550+
mergedParserOptions.ecmaVersion = normalizeEcmaVersion(mergedParserOptions.ecmaVersion);
558551

559552
return mergedParserOptions;
560553
}
@@ -613,7 +606,7 @@ function getRuleOptions(ruleConfig) {
613606
*/
614607
function analyzeScope(ast, parserOptions, visitorKeys) {
615608
const ecmaFeatures = parserOptions.ecmaFeatures || {};
616-
const ecmaVersion = parserOptions.ecmaVersion || DEFAULT_ECMA_VERSION;
609+
const ecmaVersion = parserOptions.ecmaVersion || 5;
617610

618611
return eslintScope.analyze(ast, {
619612
ignoreEval: true,
@@ -1130,7 +1123,7 @@ class Linter {
11301123
.map(envName => getEnv(slots, envName))
11311124
.filter(env => env);
11321125

1133-
const parserOptions = resolveParserOptions(parser, config.parserOptions || {}, enabledEnvs);
1126+
const parserOptions = resolveParserOptions(parserName, config.parserOptions || {}, enabledEnvs);
11341127
const configuredGlobals = resolveGlobals(config.globals || {}, enabledEnvs);
11351128
const settings = config.settings || {};
11361129

tests/lib/linter/linter.js

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
const assert = require("chai").assert,
1313
sinon = require("sinon"),
14-
espree = require("espree"),
1514
esprima = require("esprima"),
1615
testParsers = require("../../fixtures/parsers/linter-test-parsers");
1716

@@ -3494,70 +3493,6 @@ var a = "test2";
34943493
});
34953494

34963495
describe("ecmaVersion", () => {
3497-
3498-
it("should not support ES6 when no ecmaVersion provided", () => {
3499-
const messages = linter.verify("let x = 0;");
3500-
3501-
assert.strictEqual(messages.length, 1);
3502-
});
3503-
3504-
it("the default ECMAScript version is 5", () => {
3505-
let ecmaVersion = null;
3506-
const config = { rules: { "ecma-version": 2 } };
3507-
3508-
linter.defineRule("ecma-version", context => ({
3509-
Program() {
3510-
ecmaVersion = context.parserOptions.ecmaVersion;
3511-
}
3512-
}));
3513-
linter.verify("", config);
3514-
assert.strictEqual(ecmaVersion, 5);
3515-
});
3516-
3517-
it("supports ECMAScript version 'latest'", () => {
3518-
const messages = linter.verify("let x = 5 ** 7;", {
3519-
parserOptions: { ecmaVersion: "latest" }
3520-
});
3521-
3522-
assert.strictEqual(messages.length, 0);
3523-
});
3524-
3525-
it("the 'latest' is equal to espree.lastEcmaVersion", () => {
3526-
let ecmaVersion = null;
3527-
const config = { rules: { "ecma-version": 2 }, parserOptions: { ecmaVersion: "latest" } };
3528-
3529-
linter.defineRule("ecma-version", context => ({
3530-
Program() {
3531-
ecmaVersion = context.parserOptions.ecmaVersion;
3532-
}
3533-
}));
3534-
linter.verify("", config);
3535-
assert.strictEqual(ecmaVersion, espree.latestEcmaVersion);
3536-
});
3537-
3538-
it("should pass normalized ecmaVersion to eslint-scope", () => {
3539-
let blockScope = null;
3540-
3541-
linter.defineRule("block-scope", context => ({
3542-
BlockStatement() {
3543-
blockScope = context.getScope();
3544-
}
3545-
}));
3546-
3547-
linter.verify("{}", {
3548-
rules: { "block-scope": 2 },
3549-
parserOptions: { ecmaVersion: "latest" }
3550-
});
3551-
3552-
assert.strictEqual(blockScope.type, "block");
3553-
3554-
linter.verify("{}", {
3555-
rules: { "block-scope": 2 },
3556-
parserOptions: {} // ecmaVersion defaults to 5
3557-
});
3558-
assert.strictEqual(blockScope.type, "global");
3559-
});
3560-
35613496
describe("it should properly parse let declaration when", () => {
35623497
it("the ECMAScript version number is 6", () => {
35633498
const messages = linter.verify("let x = 5;", {

0 commit comments

Comments
 (0)