diff options
Diffstat (limited to 'tools/node_modules/eslint/lib/linter.js')
-rw-r--r-- | tools/node_modules/eslint/lib/linter.js | 128 |
1 files changed, 8 insertions, 120 deletions
diff --git a/tools/node_modules/eslint/lib/linter.js b/tools/node_modules/eslint/lib/linter.js index b47e6eb1fc..d6d635f9f4 100644 --- a/tools/node_modules/eslint/lib/linter.js +++ b/tools/node_modules/eslint/lib/linter.js @@ -11,7 +11,6 @@ const eslintScope = require("eslint-scope"), evk = require("eslint-visitor-keys"), - levn = require("levn"), lodash = require("lodash"), CodePathAnalyzer = require("./code-path-analysis/code-path-analyzer"), ConfigOps = require("./config/config-ops"), @@ -22,9 +21,10 @@ const eslintScope = require("eslint-scope"), NodeEventGenerator = require("./util/node-event-generator"), SourceCode = require("./util/source-code"), Traverser = require("./util/traverser"), - createReportTranslator = require("./report-translator"), + createReportTranslator = require("./util/report-translator"), Rules = require("./rules"), timing = require("./util/timing"), + ConfigCommentParser = require("./util/config-comment-parser"), astUtils = require("./util/ast-utils"), pkg = require("../package.json"), SourceCodeFixer = require("./util/source-code-fixer"); @@ -32,6 +32,7 @@ const eslintScope = require("eslint-scope"), const debug = require("debug")("eslint:linter"); const MAX_AUTOFIX_PASSES = 10; const DEFAULT_PARSER_NAME = "espree"; +const commentParser = new ConfigCommentParser(); //------------------------------------------------------------------------------ // Typedefs @@ -60,117 +61,6 @@ const DEFAULT_PARSER_NAME = "espree"; //------------------------------------------------------------------------------ /** - * Parses a list of "name:boolean_value" or/and "name" options divided by comma or - * whitespace. - * @param {string} string The string to parse. - * @param {Comment} comment The comment node which has the string. - * @returns {Object} Result map object of names and boolean values - */ -function parseBooleanConfig(string, comment) { - const items = {}; - - // Collapse whitespace around `:` and `,` to make parsing easier - const trimmedString = string.replace(/\s*([:,])\s*/g, "$1"); - - trimmedString.split(/\s|,+/).forEach(name => { - if (!name) { - return; - } - const pos = name.indexOf(":"); - - if (pos === -1) { - items[name] = { - value: false, - comment - }; - } else { - items[name.slice(0, pos)] = { - value: name.slice(pos + 1) === "true", - comment - }; - } - }); - return items; -} - -/** - * Parses a JSON-like config. - * @param {string} string The string to parse. - * @param {Object} location Start line and column of comments for potential error message. - * @returns {({success: true, config: Object}|{success: false, error: Problem})} Result map object - */ -function parseJsonConfig(string, location) { - let items = {}; - - // Parses a JSON-like comment by the same way as parsing CLI option. - try { - items = levn.parse("Object", string) || {}; - - // Some tests say that it should ignore invalid comments such as `/*eslint no-alert:abc*/`. - // Also, commaless notations have invalid severity: - // "no-alert: 2 no-console: 2" --> {"no-alert": "2 no-console: 2"} - // Should ignore that case as well. - if (ConfigOps.isEverySeverityValid(items)) { - return { - success: true, - config: items - }; - } - } catch (ex) { - - // ignore to parse the string by a fallback. - } - - /* - * Optionator cannot parse commaless notations. - * But we are supporting that. So this is a fallback for that. - */ - items = {}; - const normalizedString = string.replace(/([a-zA-Z0-9\-/]+):/g, "\"$1\":").replace(/(]|[0-9])\s+(?=")/, "$1,"); - - try { - items = JSON.parse(`{${normalizedString}}`); - } catch (ex) { - return { - success: false, - error: { - ruleId: null, - fatal: true, - severity: 2, - message: `Failed to parse JSON from '${normalizedString}': ${ex.message}`, - line: location.start.line, - column: location.start.column + 1 - } - }; - - } - - return { - success: true, - config: items - }; -} - -/** - * Parses a config of values separated by comma. - * @param {string} string The string to parse. - * @returns {Object} Result map of values and true values - */ -function parseListConfig(string) { - const items = {}; - - // Collapse whitespace around , - string.replace(/\s*,\s*/g, ",").split(/,+/).forEach(name => { - const trimmedName = name.trim(); - - if (trimmedName) { - items[trimmedName] = true; - } - }); - return items; -} - -/** * Ensures that variables representing built-in properties of the Global Object, * and any globals declared by special block comments, are present in the global * scope. @@ -248,7 +138,7 @@ function addDeclaredGlobals(globalScope, configGlobals, commentDirectives) { * @returns {DisableDirective[]} Directives from the comment */ function createDisableDirectives(type, loc, value) { - const ruleIds = Object.keys(parseListConfig(value)); + const ruleIds = Object.keys(commentParser.parseListConfig(value)); const directiveRules = ruleIds.length ? ruleIds : [null]; return directiveRules.map(ruleId => ({ type, line: loc.line, column: loc.column + 1, ruleId })); @@ -301,12 +191,12 @@ function getDirectiveComments(filename, ast, ruleMapper) { } else if (comment.type === "Block") { switch (match[1]) { case "exported": - Object.assign(exportedVariables, parseBooleanConfig(directiveValue, comment)); + Object.assign(exportedVariables, commentParser.parseBooleanConfig(directiveValue, comment)); break; case "globals": case "global": - Object.assign(enabledGlobals, parseBooleanConfig(directiveValue, comment)); + Object.assign(enabledGlobals, commentParser.parseBooleanConfig(directiveValue, comment)); break; case "eslint-disable": @@ -318,7 +208,7 @@ function getDirectiveComments(filename, ast, ruleMapper) { break; case "eslint": { - const parseResult = parseJsonConfig(directiveValue, comment.loc); + const parseResult = commentParser.parseJsonConfig(directiveValue, comment.loc); if (parseResult.success) { Object.keys(parseResult.config).forEach(name => { @@ -398,7 +288,7 @@ function findEslintEnv(text) { eslintEnvPattern.lastIndex = 0; while ((match = eslintEnvPattern.exec(text))) { - retv = Object.assign(retv || {}, parseListConfig(match[1])); + retv = Object.assign(retv || {}, commentParser.parseListConfig(match[1])); } return retv; @@ -1032,8 +922,6 @@ module.exports = class Linter { * @param {(string|Object)} [filenameOrOptions] The optional filename of the file being checked. * If this is not set, the filename will default to '<input>' in the rule context. If * an object, then it has "filename", "saveState", and "allowInlineConfig" properties. - * @param {boolean} [saveState] Indicates if the state from the last run should be saved. - * Mostly useful for testing purposes. * @param {boolean} [filenameOrOptions.allowInlineConfig] Allow/disallow inline comments' ability to change config once it is set. Defaults to true if not supplied. * Useful if you want to validate JS without comments overriding rules. * @param {function(string): string[]} [filenameOrOptions.preprocess] preprocessor for source text. If provided, |