From f70173654a844102d30c8206596795e64803675e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=AF=E7=84=B6?= Date: Thu, 17 Jun 2021 10:57:19 +0800 Subject: [PATCH] chore: use parser.$parser to check if it's espree --- lib/linter/linter.js | 4 ++- lib/rule-tester/rule-tester.js | 45 +++------------------------------- 2 files changed, 6 insertions(+), 43 deletions(-) diff --git a/lib/linter/linter.js b/lib/linter/linter.js index 77682ea9548..82df6a55a95 100644 --- a/lib/linter/linter.js +++ b/lib/linter/linter.js @@ -438,7 +438,9 @@ function getDirectiveComments(filename, ast, ruleMapper, warnInlineConfig) { * @returns {number} normalized ECMAScript version */ function normalizeEcmaVersion(parser, ecmaVersion) { - if (parser === espree) { + + // RuleTester always wraps parser, parser.$parser is the original parser object + if ((parser.$parser || parser) === espree) { if (ecmaVersion === void 0) { return DEFAULT_ECMA_VERSION; } diff --git a/lib/rule-tester/rule-tester.js b/lib/rule-tester/rule-tester.js index 702a1651c82..9efd2993b5f 100644 --- a/lib/rule-tester/rule-tester.js +++ b/lib/rule-tester/rule-tester.js @@ -48,8 +48,7 @@ const equal = require("fast-deep-equal"), Traverser = require("../../lib/shared/traverser"), { getRuleOptionsSchema, validate } = require("../shared/config-validator"), - { Linter, SourceCodeFixer, interpolate } = require("../linter"), - espree = require("espree"); + { Linter, SourceCodeFixer, interpolate } = require("../linter"); const ajv = require("../shared/ajv")({ strictDefaults: true }); @@ -258,6 +257,7 @@ function defineStartEndAsErrorInTree(ast, visitorKeys) { function wrapParser(parser) { if (typeof parser.parseForESLint === "function") { return { + $parser: parser, parseForESLint(...args) { const ret = parser.parseForESLint(...args); @@ -267,6 +267,7 @@ function wrapParser(parser) { }; } return { + $parser: parser, parse(...args) { const ast = parser.parse(...args); @@ -529,46 +530,6 @@ class RuleTester { linter.defineParser(config.parser, wrapParser(require(config.parser))); - /* - * RuleTester always wraps parser, so Linter can't know whether or not it's espree. - * Therefore, RuleTester has to duplicate Linter's espree-specific normalizations. - */ - if ( - config.parser === espreePath && - ( - typeof config.parserOptions === "undefined" || - typeof config.parserOptions === "object" && config.parserOptions !== null - ) - ) { - const ecmaVersion = config.parserOptions && config.parserOptions.ecmaVersion; - - if ( - typeof ecmaVersion === "undefined" && - !( - - // skip if an environment sets ecmaVersion, because parserOptions.ecmaVersion has precedence - typeof config.env === "object" && config.env !== null && - Object.keys(config.env).some( - envName => config.env[envName] && /^es\d+/u.test(envName) - ) - ) - ) { - config = merge(config, { - parserOptions: { - ecmaVersion: 5 - } - }); - } - - if (ecmaVersion === "latest") { - config = merge(config, { - parserOptions: { - ecmaVersion: espree.latestEcmaVersion - } - }); - } - } - if (schema) { ajv.validateSchema(schema);