Skip to content

Commit

Permalink
chore: use parser.$parser to check if it's espree
Browse files Browse the repository at this point in the history
  • Loading branch information
aladdin-add committed Jun 17, 2021
1 parent 512f1c4 commit f701736
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 43 deletions.
4 changes: 3 additions & 1 deletion lib/linter/linter.js
Expand Up @@ -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;
}
Expand Down
45 changes: 3 additions & 42 deletions lib/rule-tester/rule-tester.js
Expand Up @@ -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 });

Expand Down Expand Up @@ -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);

Expand All @@ -267,6 +267,7 @@ function wrapParser(parser) {
};
}
return {
$parser: parser,
parse(...args) {
const ast = parser.parse(...args);

Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit f701736

Please sign in to comment.