From 0338fd201614247eeb21e68a26e4b4c8a74f71b0 Mon Sep 17 00:00:00 2001 From: Yosuke Ota Date: Tue, 9 Nov 2021 10:34:56 +0900 Subject: [PATCH] feat: Normalize ecmaVersion to eslint-scope when using custom parser (#15268) * feat: Normalize ecmaVersion to eslint-scope when using custom parser * test: Add testcase --- lib/linter/linter.js | 2 +- tests/lib/linter/linter.js | 20 ++++++++++++++++++++ 2 files changed, 21 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..24dc4de43ce 100644 --- a/tests/lib/linter/linter.js +++ b/tests/lib/linter/linter.js @@ -4137,9 +4137,28 @@ var a = "test2"; blockScope = context.getScope(); } })); + linter.defineParser("custom-parser", { + parse: (...args) => espree.parse(...args) + }); + + // Use standard parser + linter.verify("{}", { + rules: { "block-scope": 2 }, + parserOptions: { ecmaVersion: "latest" } + }); + + assert.strictEqual(blockScope.type, "block"); + + 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", parserOptions: { ecmaVersion: "latest" } }); @@ -4147,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");