From 456b366ed0abf7513fd9ba4fdd41736ba31dba8b Mon Sep 17 00:00:00 2001 From: "weiran.zsd" Date: Wed, 22 May 2019 16:05:24 +0800 Subject: [PATCH] chore: small refactor --- lib/linter/linter.js | 70 ++++++++++++++++++-------------------- tests/lib/linter/linter.js | 2 +- 2 files changed, 35 insertions(+), 37 deletions(-) diff --git a/lib/linter/linter.js b/lib/linter/linter.js index 73fe673882ef..b441fb7f4c1e 100644 --- a/lib/linter/linter.js +++ b/lib/linter/linter.js @@ -150,16 +150,27 @@ function addDeclaredGlobals(globalScope, configGlobals, { exportedVariables, ena /** * creating a linting problem for a undefined rule. - * todo: accept passed message - * @param {string} ruleId given ruleId - * @param {Loc} loc the loc to report - * @returns {Problem} created problem + * @param {string} ruleId the ruleId to create + * @returns {string} created error message * @private */ -function createLintingProblem(ruleId, loc = DEFAULT_ERROR_LOC) { - const message = Object.prototype.hasOwnProperty.call(ruleReplacements.rules, ruleId) +function createMissingRuleMessage(ruleId) { + return Object.prototype.hasOwnProperty.call(ruleReplacements.rules, ruleId) ? `Rule '${ruleId}' was removed and replaced by: ${ruleReplacements.rules[ruleId].join(", ")}` : `Definition for rule '${ruleId}' was not found.`; +} + +/** + * creating a linting problem, returns a missing-rule problem if only provided ruleId. + * @param {Object} options to create linting error + * @param {string} options.ruleId the ruleId to report + * @param {Object} options.loc the loc to report + * @param {string} options.message the error message to report + * @returns {Problem} created problem + * @private + */ +function createLintingProblem(options) { + const { ruleId, loc = DEFAULT_ERROR_LOC, message = createMissingRuleMessage(options.ruleId) } = options; return { ruleId, @@ -169,7 +180,7 @@ function createLintingProblem(ruleId, loc = DEFAULT_ERROR_LOC) { endLine: loc.end.line, endColumn: loc.end.column + 1, severity: 2, - nodeType: [null] + nodeType: null }; } @@ -198,7 +209,7 @@ function createDisableDirectives(options) { if (ruleId === null || ruleMapper(ruleId)) { result.directives.push({ type, line: loc.start.line, column: loc.start.column + 1, ruleId }); } else { - result.directiveProblems.push(createLintingProblem(ruleId, loc)); + result.directiveProblems.push(createLintingProblem({ ruleId, loc })); } } return result; @@ -236,16 +247,13 @@ function getDirectiveComments(filename, ast, ruleMapper) { if (comment.loc.start.line === comment.loc.end.line) { directiveType = match[1].slice("eslint-".length); } else { - problems.push({ + const message = `${match[1]} comment should not span multiple lines.`; + + problems.push(createLintingProblem({ ruleId: null, - severity: 2, - message: `${match[1]} comment should not span multiple lines.`, - line: comment.loc.start.line, - column: comment.loc.start.column + 1, - endLine: comment.loc.end.line, - endColumn: comment.loc.end.column + 1, - nodeType: null - }); + message, + loc: comment.loc + })); } } else if (comment.type === "Block") { switch (match[1]) { @@ -261,16 +269,11 @@ function getDirectiveComments(filename, ast, ruleMapper) { try { normalizedValue = ConfigOps.normalizeConfigGlobal(value); } catch (err) { - problems.push({ + problems.push(createLintingProblem({ ruleId: null, - severity: 2, - message: err.message, - line: comment.loc.start.line, - column: comment.loc.start.column + 1, - endLine: comment.loc.end.line, - endColumn: comment.loc.end.column + 1, - nodeType: null - }); + loc: comment.loc, + message: err.message + })); continue; } @@ -303,23 +306,18 @@ function getDirectiveComments(filename, ast, ruleMapper) { const ruleValue = parseResult.config[name]; if (rule === null) { - problems.push(createLintingProblem(name, comment.loc)); + problems.push(createLintingProblem({ ruleId: name, loc: comment.loc })); return; } try { validator.validateRuleOptions(rule, name, ruleValue); } catch (err) { - problems.push({ + problems.push(createLintingProblem({ ruleId: name, - severity: 2, message: err.message, - line: comment.loc.start.line, - column: comment.loc.start.column + 1, - endLine: comment.loc.end.line, - endColumn: comment.loc.end.column + 1, - nodeType: null - }); + loc: comment.loc + })); // do not apply the config, if found invalid options. return; @@ -769,7 +767,7 @@ function runRules(sourceCode, configuredRules, ruleMapper, parserOptions, parser const rule = ruleMapper(ruleId); if (rule === null) { - lintingProblems.push(createLintingProblem(ruleId)); + lintingProblems.push(createLintingProblem({ ruleId })); return; } diff --git a/tests/lib/linter/linter.js b/tests/lib/linter/linter.js index 08b76a0eddd6..c6d50ace14ee 100644 --- a/tests/lib/linter/linter.js +++ b/tests/lib/linter/linter.js @@ -2818,7 +2818,7 @@ describe("Linter", () => { endLine: 1, endColumn: 2, severity: 2, - nodeType: [null] + nodeType: null } ); });