Skip to content

Commit

Permalink
chore: small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
aladdin-add committed May 22, 2019
1 parent 66f442d commit e1df65b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 37 deletions.
70 changes: 34 additions & 36 deletions lib/linter.js
Expand Up @@ -149,16 +149,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,
Expand All @@ -168,7 +179,7 @@ function createLintingProblem(ruleId, loc = DEFAULT_ERROR_LOC) {
endLine: loc.end.line,
endColumn: loc.end.column + 1,
severity: 2,
nodeType: [null]
nodeType: null
};
}

Expand Down Expand Up @@ -197,7 +208,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;
Expand Down Expand Up @@ -235,16 +246,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]) {
Expand All @@ -260,16 +268,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;
}

Expand Down Expand Up @@ -302,23 +305,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;
Expand Down Expand Up @@ -768,7 +766,7 @@ function runRules(sourceCode, configuredRules, ruleMapper, parserOptions, parser
const rule = ruleMapper(ruleId);

if (rule === null) {
lintingProblems.push(createLintingProblem(ruleId));
lintingProblems.push(createLintingProblem({ ruleId }));
return;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/lib/linter.js
Expand Up @@ -2818,7 +2818,7 @@ describe("Linter", () => {
endLine: 1,
endColumn: 2,
severity: 2,
nodeType: [null]
nodeType: null
}
);
});
Expand Down

0 comments on commit e1df65b

Please sign in to comment.