From 033f08092c109ff0c7ac71d78adaee18de558cf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=AF=E7=84=B6?= Date: Thu, 3 Jun 2021 16:39:44 +0800 Subject: [PATCH] chore: review suggestions 1. always normalize ecmaversion 2. add a test: default ecmaversion is 5 --- lib/linter/linter.js | 18 ++++++++---------- tests/lib/linter/linter.js | 30 ++++++++++++++++++------------ 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/lib/linter/linter.js b/lib/linter/linter.js index 247f87932698..b553e44af613 100644 --- a/lib/linter/linter.js +++ b/lib/linter/linter.js @@ -434,17 +434,15 @@ function getDirectiveComments(filename, ast, ruleMapper, warnInlineConfig) { /** * Normalize ECMAScript version from the initial config * @param {number} ecmaVersion ECMAScript version from the initial config - * @param {string} parserName the parser's name * @returns {number} normalized ECMAScript version */ -function normalizeEcmaVersion(ecmaVersion, parserName) { - if (parserName === DEFAULT_PARSER_NAME || parserName === void 0) { - if (ecmaVersion === "latest") { - return espree.latestEcmaVersion; - } - if (ecmaVersion === void 0) { - return DEFAULT_ECMA_VERSION; - } +function normalizeEcmaVersion(ecmaVersion) { + if (ecmaVersion === void 0) { + return DEFAULT_ECMA_VERSION; + } + + if (ecmaVersion === "latest") { + return espree.latestEcmaVersion; } /* @@ -552,7 +550,7 @@ function resolveParserOptions(parserName, providedOptions, enabledEnvironments) mergedParserOptions.ecmaFeatures = Object.assign({}, mergedParserOptions.ecmaFeatures, { globalReturn: false }); } - mergedParserOptions.ecmaVersion = normalizeEcmaVersion(mergedParserOptions.ecmaVersion, parserName); + mergedParserOptions.ecmaVersion = normalizeEcmaVersion(mergedParserOptions.ecmaVersion); return mergedParserOptions; } diff --git a/tests/lib/linter/linter.js b/tests/lib/linter/linter.js index 8501f3cccd19..454a1925b2db 100644 --- a/tests/lib/linter/linter.js +++ b/tests/lib/linter/linter.js @@ -3425,6 +3425,22 @@ var a = "test2"; }); describe("ecmaVersion", () => { + it("the default ECMAScript version is 5", () => { + const messages = linter.verify("let x = 0;", {}); + + assert.strictEqual(messages.length, 1); + }); + + it("supports ECMAScript version 'latest'", () => { + const messages = linter.verify("let x = 5 ** 7;", { + parserOptions: { + ecmaVersion: "latest" + } + }); + + assert.strictEqual(messages.length, 0); + }); + describe("it should properly parse let declaration when", () => { it("the ECMAScript version number is 6", () => { const messages = linter.verify("let x = 5;", { @@ -3445,16 +3461,6 @@ var a = "test2"; assert.strictEqual(messages.length, 0); }); - - it("the ECMAScript version is 'latest'", () => { - const messages = linter.verify("let x = 5 ** 7;", { - parserOptions: { - ecmaVersion: "latest" - } - }); - - assert.strictEqual(messages.length, 0); - }); }); it("should fail to parse exponentiation operator when the ECMAScript version number is 2015", () => { @@ -5396,11 +5402,11 @@ var a = "test2"; }); }); - it("should not pass any default parserOptions to the parser", () => { + it("should pass default ecmaVersion parserOptions to the parser", () => { linter.defineParser("throws-with-options", testParsers.throwsWithOptions); const messages = linter.verify(";", { parser: "throws-with-options" }, "filename"); - assert.strictEqual(messages.length, 0); + assert.strictEqual(messages.length, 1); }); }); });