Skip to content

Commit

Permalink
fix: check if the parser is built-in
Browse files Browse the repository at this point in the history
chore: update tests
  • Loading branch information
aladdin-add committed Jun 6, 2021
1 parent 391a8f8 commit ea46670
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
24 changes: 14 additions & 10 deletions lib/linter/linter.js
Expand Up @@ -433,16 +433,19 @@ function getDirectiveComments(filename, ast, ruleMapper, warnInlineConfig) {

/**
* Normalize ECMAScript version from the initial config
* @param {Parser} parser The parser which uses this options.
* @param {number} ecmaVersion ECMAScript version from the initial config
* @returns {number} normalized ECMAScript version
*/
function normalizeEcmaVersion(ecmaVersion) {
if (ecmaVersion === void 0) {
return DEFAULT_ECMA_VERSION;
}
function normalizeEcmaVersion(parser, ecmaVersion) {
if (parser === espree) {
if (ecmaVersion === void 0) {
return DEFAULT_ECMA_VERSION;
}

if (ecmaVersion === "latest") {
return espree.latestEcmaVersion;
if (ecmaVersion === "latest") {
return espree.latestEcmaVersion;
}
}

/*
Expand Down Expand Up @@ -529,12 +532,13 @@ function normalizeVerifyOptions(providedOptions, config) {

/**
* Combines the provided parserOptions with the options from environments
* @param {string} parserName The parser name which uses this options.
* @param {Parser} parser The parser which uses this options.
* @param {ParserOptions} providedOptions The provided 'parserOptions' key in a config
* @param {Environment[]} enabledEnvironments The environments enabled in configuration and with inline comments
* @returns {ParserOptions} Resulting parser options after merge
*/
function resolveParserOptions(parserName, providedOptions, enabledEnvironments) {
function resolveParserOptions(parser, providedOptions, enabledEnvironments) {

const parserOptionsFromEnv = enabledEnvironments
.filter(env => env.parserOptions)
.reduce((parserOptions, env) => merge(parserOptions, env.parserOptions), {});
Expand All @@ -550,7 +554,7 @@ function resolveParserOptions(parserName, providedOptions, enabledEnvironments)
mergedParserOptions.ecmaFeatures = Object.assign({}, mergedParserOptions.ecmaFeatures, { globalReturn: false });
}

mergedParserOptions.ecmaVersion = normalizeEcmaVersion(mergedParserOptions.ecmaVersion);
mergedParserOptions.ecmaVersion = normalizeEcmaVersion(parser, mergedParserOptions.ecmaVersion);

return mergedParserOptions;
}
Expand Down Expand Up @@ -1126,7 +1130,7 @@ class Linter {
.map(envName => getEnv(slots, envName))
.filter(env => env);

const parserOptions = resolveParserOptions(parserName, config.parserOptions || {}, enabledEnvs);
const parserOptions = resolveParserOptions(parser, config.parserOptions || {}, enabledEnvs);
const configuredGlobals = resolveGlobals(config.globals || {}, enabledEnvs);
const settings = config.settings || {};

Expand Down
4 changes: 2 additions & 2 deletions tests/lib/linter/linter.js
Expand Up @@ -5479,11 +5479,11 @@ var a = "test2";
});
});

it("should pass default ecmaVersion parserOptions to the parser", () => {
it("should not pass any default parserOptions to the parser", () => {
linter.defineParser("throws-with-options", testParsers.throwsWithOptions);
const messages = linter.verify(";", { parser: "throws-with-options" }, "filename");

assert.strictEqual(messages.length, 1);
assert.strictEqual(messages.length, 0);
});
});
});

0 comments on commit ea46670

Please sign in to comment.