From 2db8392f1105d4a0154525cae08d10bdb06a0686 Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Sat, 6 Nov 2021 11:37:02 +0900 Subject: [PATCH 1/2] feat: Normalize ecmaVersion to eslint-scope when using custom parser --- lib/linter/linter.js | 2 +- tests/lib/linter/linter.js | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/linter/linter.js b/lib/linter/linter.js index 410c07fee51..4e07a25751e 100644 --- a/lib/linter/linter.js +++ b/lib/linter/linter.js @@ -626,7 +626,7 @@ function analyzeScope(ast, parserOptions, visitorKeys) { ignoreEval: true, nodejsScope: ecmaFeatures.globalReturn, impliedStrict: ecmaFeatures.impliedStrict, - ecmaVersion, + ecmaVersion: typeof ecmaVersion === "number" ? ecmaVersion : 6, sourceType: parserOptions.sourceType || "script", childVisitorKeys: visitorKeys || evk.KEYS, fallback: Traverser.getKeys diff --git a/tests/lib/linter/linter.js b/tests/lib/linter/linter.js index c2bb5fdd746..82651ba43a6 100644 --- a/tests/lib/linter/linter.js +++ b/tests/lib/linter/linter.js @@ -4137,9 +4137,22 @@ var a = "test2"; blockScope = context.getScope(); } })); + linter.defineParser("custom-parser", { + parse: (...args) => espree.parse(...args) + }); + + // standard parser with latest + linter.verify("{}", { + rules: { "block-scope": 2 }, + parserOptions: { ecmaVersion: "latest" } + }); + + assert.strictEqual(blockScope.type, "block"); + // custom parser with latest linter.verify("{}", { rules: { "block-scope": 2 }, + parser: "custom-parser", parserOptions: { ecmaVersion: "latest" } }); From e998be154bf5d5e74566021030bfbb02c17e5324 Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Mon, 8 Nov 2021 10:29:12 +0900 Subject: [PATCH 2/2] test: Add testcase --- tests/lib/linter/linter.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/lib/linter/linter.js b/tests/lib/linter/linter.js index 82651ba43a6..24dc4de43ce 100644 --- a/tests/lib/linter/linter.js +++ b/tests/lib/linter/linter.js @@ -4141,7 +4141,7 @@ var a = "test2"; parse: (...args) => espree.parse(...args) }); - // standard parser with latest + // Use standard parser linter.verify("{}", { rules: { "block-scope": 2 }, parserOptions: { ecmaVersion: "latest" } @@ -4149,7 +4149,13 @@ var a = "test2"; assert.strictEqual(blockScope.type, "block"); - // custom parser with latest + linter.verify("{}", { + rules: { "block-scope": 2 }, + parserOptions: {} // ecmaVersion defaults to 5 + }); + assert.strictEqual(blockScope.type, "global"); + + // Use custom parser linter.verify("{}", { rules: { "block-scope": 2 }, parser: "custom-parser", @@ -4160,6 +4166,7 @@ var a = "test2"; linter.verify("{}", { rules: { "block-scope": 2 }, + parser: "custom-parser", parserOptions: {} // ecmaVersion defaults to 5 }); assert.strictEqual(blockScope.type, "global");