Skip to content

Commit

Permalink
Chore: refactor option default handling in linter.verify
Browse files Browse the repository at this point in the history
  • Loading branch information
not-an-aardvark committed Feb 25, 2018
1 parent 8f7b279 commit 76abcea
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions lib/linter.js
Expand Up @@ -395,6 +395,24 @@ function findEslintEnv(text) {
return retv;
}

/**
* Normalizes the possible options for `linter.verify` and `linter.verifyAndFix` to a
* consistent shape.
* @param {(string|{reportUnusedDisableDirectives: boolean, filename: string, allowInlineConfig: boolean, fix: (boolean|function(Fix): boolean)})} providedOptions Options
* @returns {{reportUnusedDisableDirectives: boolean, filename: string, allowInlineConfig: boolean, fix: (boolean|function(Fix): boolean)}} Normalized options
*/
function normalizeVerifyOptions(providedOptions) {
const isObjectOptions = typeof providedOptions === "object";
const providedFilename = isObjectOptions ? providedOptions.filename : providedOptions;

return {
filename: typeof providedFilename === "string" ? providedFilename : "<input>",
allowInlineConfig: !isObjectOptions || providedOptions.allowInlineConfig !== false,
reportUnusedDisableDirectives: isObjectOptions && !!providedOptions.reportUnusedDisableDirectives,
fix: isObjectOptions && typeof providedOptions.fix !== "undefined" ? providedOptions.fix : true
};
}

/**
* Combines the provided parserOptions with the options from environments
* @param {Object} providedOptions The provided 'parserOptions' key in a config
Expand Down Expand Up @@ -882,22 +900,10 @@ module.exports = class Linter {
*/
_verifyWithoutProcessors(textOrSourceCode, providedConfig, filenameOrOptions) {
const config = providedConfig || {};

let text,
allowInlineConfig,
providedFilename,
reportUnusedDisableDirectives;
const options = normalizeVerifyOptions(filenameOrOptions);
let text;

// evaluate arguments
if (typeof filenameOrOptions === "object") {
providedFilename = filenameOrOptions.filename;
allowInlineConfig = filenameOrOptions.allowInlineConfig !== false;
reportUnusedDisableDirectives = filenameOrOptions.reportUnusedDisableDirectives;
} else {
providedFilename = filenameOrOptions;
allowInlineConfig = true;
}

if (typeof textOrSourceCode === "string") {
lastSourceCodes.set(this, null);
text = textOrSourceCode;
Expand All @@ -906,15 +912,13 @@ module.exports = class Linter {
text = textOrSourceCode.text;
}

const filename = typeof providedFilename === "string" ? providedFilename : "<input>";

// search and apply "eslint-env *".
const envInFile = findEslintEnv(text);
const resolvedEnvConfig = Object.assign({ builtin: true }, config.env, envInFile);
const enabledEnvs = Object.keys(resolvedEnvConfig)
.filter(envName => resolvedEnvConfig[envName])
.map(envName => this.environments.get(envName))
.filter(Boolean);
.filter(env => env);

const parserOptions = resolveParserOptions(config.parserOptions || {}, enabledEnvs);
const configuredGlobals = resolveGlobals(config.globals || {}, enabledEnvs);
Expand Down Expand Up @@ -948,7 +952,7 @@ module.exports = class Linter {
text,
parserOptions,
parser,
filename
options.filename
);

if (!parseResult.success) {
Expand Down Expand Up @@ -976,35 +980,35 @@ module.exports = class Linter {
}

const sourceCode = lastSourceCodes.get(this);
const commentDirectives = allowInlineConfig
? getDirectiveComments(filename, sourceCode.ast, ruleId => this.rules.get(ruleId))
const commentDirectives = options.allowInlineConfig
? getDirectiveComments(options.filename, sourceCode.ast, ruleId => this.rules.get(ruleId))
: { configuredRules: {}, enabledGlobals: {}, exportedVariables: {}, problems: [], disableDirectives: [] };

const configuredRules = Object.assign({}, config.rules, commentDirectives.configuredRules);

// augment global scope with declared global variables
addDeclaredGlobals(
sourceCode.scopeManager.scopes[0],
configuredGlobals,
{ exportedVariables: commentDirectives.exportedVariables, enabledGlobals: commentDirectives.enabledGlobals }
);

const configuredRules = Object.assign({}, config.rules, commentDirectives.configuredRules);

const lintingProblems = runRules(
sourceCode,
configuredRules,
ruleId => this.rules.get(ruleId),
parserOptions,
parserName,
settings,
filename
options.filename
);

return applyDisableDirectives({
directives: commentDirectives.disableDirectives,
problems: lintingProblems
.concat(commentDirectives.problems)
.sort((problemA, problemB) => problemA.line - problemB.line || problemA.column - problemB.column),
reportUnusedDisableDirectives
reportUnusedDisableDirectives: options.reportUnusedDisableDirectives
});
}

Expand Down

0 comments on commit 76abcea

Please sign in to comment.