Skip to content

Commit

Permalink
wip!!!
Browse files Browse the repository at this point in the history
todo: 1. add tests, 2. fix failing tests 3. docs update
  • Loading branch information
aladdin-add committed May 22, 2019
1 parent f525bb8 commit 66f442d
Showing 1 changed file with 51 additions and 40 deletions.
91 changes: 51 additions & 40 deletions lib/linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,29 +147,15 @@ function addDeclaredGlobals(globalScope, configGlobals, { exportedVariables, ena
});
}

/**
* Creates a collection of disable directives from a comment
* @param {("disable"|"enable"|"disable-line"|"disable-next-line")} type The type of directive comment
* @param {{line: number, column: number}} loc The 0-based location of the comment token
* @param {string} value The value after the directive in the comment
* comment specified no specific rules, so it applies to all rules (e.g. `eslint-disable`)
* @returns {DisableDirective[]} Directives from the comment
*/
function createDisableDirectives(type, loc, 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 }));
}

/**
* 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
* @private
*/
function createMissingProblem(ruleId, loc = DEFAULT_ERROR_LOC) {
function createLintingProblem(ruleId, loc = DEFAULT_ERROR_LOC) {
const message = 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.`;
Expand All @@ -186,6 +172,37 @@ function createMissingProblem(ruleId, loc = DEFAULT_ERROR_LOC) {
};
}

/**
* Creates a collection of disable directives from a comment
* @param {Object} options to create disable directives
* @param {("disable"|"enable"|"disable-line"|"disable-next-line")} options.type The type of directive comment
* @param {{line: number, column: number}} options.loc The 0-based location of the comment token
* @param {string} options.value The value after the directive in the comment
* comment specified no specific rules, so it applies to all rules (e.g. `eslint-disable`)
* @param {function(string): {create: Function}} options.ruleMapper A map from rule IDs to defined rules
* @returns {Object} Directives and problems from the comment
*/
function createDisableDirectives(options) {
const { type, loc, value, ruleMapper } = options;
const ruleIds = Object.keys(commentParser.parseListConfig(value));
const directiveRules = ruleIds.length ? ruleIds : [null];
const result = {
directives: [], // valid disable directives
directiveProblems: [] // problems in directives
};

for (const ruleId of directiveRules) {

// if the rule is defined, push to directives
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));
}
}
return result;
}

/**
* Parses comments in file to extract file-specific config of rules, globals
* and environments and merges them with global config; also code blocks
Expand All @@ -212,12 +229,11 @@ function getDirectiveComments(filename, ast, ruleMapper) {
}

const directiveValue = trimmedCommentText.slice(match.index + match[1].length);
let directiveType = "";

if (/^eslint-disable-(next-)?line$/u.test(match[1])) {
if (comment.loc.start.line === comment.loc.end.line) {
const directiveType = match[1].slice("eslint-".length);

disableDirectives.push(...createDisableDirectives(directiveType, comment.loc.start, directiveValue));
directiveType = match[1].slice("eslint-".length);
} else {
problems.push({
ruleId: null,
Expand Down Expand Up @@ -269,27 +285,12 @@ function getDirectiveComments(filename, ast, ruleMapper) {
}
break;

case "eslint-disable": {
const directiveRules = createDisableDirectives("disable", comment.loc.start, directiveValue);

// validate disable directives
for (const directive of directiveRules) {
const ruleId = directive.ruleId;

if (ruleId !== null && !ruleMapper(ruleId)) {
problems.push(createMissingProblem(ruleId, comment.loc));

// do not apply the config, if found invalid options.
return;

}
disableDirectives.push(directive);
}
case "eslint-disable":
directiveType = "disable";
break;
}

case "eslint-enable":
disableDirectives.push(...createDisableDirectives("enable", comment.loc.start, directiveValue));
directiveType = "enable";
break;

case "eslint": {
Expand All @@ -301,7 +302,7 @@ function getDirectiveComments(filename, ast, ruleMapper) {
const ruleValue = parseResult.config[name];

if (rule === null) {
problems.push(createMissingProblem(name));
problems.push(createLintingProblem(name, comment.loc));
return;
}

Expand Down Expand Up @@ -335,6 +336,14 @@ function getDirectiveComments(filename, ast, ruleMapper) {
// no default
}
}

if (directiveType !== "") {
const options = { type: directiveType, loc: comment.loc, value: directiveValue, ruleMapper };
const { directives, directiveProblems } = createDisableDirectives(options);

disableDirectives.push(...directives);
problems.push(...directiveProblems);
}
});

return {
Expand Down Expand Up @@ -750,14 +759,16 @@ function runRules(sourceCode, configuredRules, ruleMapper, parserOptions, parser

Object.keys(configuredRules).forEach(ruleId => {
const severity = ConfigOps.getRuleSeverity(configuredRules[ruleId]);
const rule = ruleMapper(ruleId);

// not load disabled rules
if (severity === 0) {
return;
}

const rule = ruleMapper(ruleId);

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

Expand Down

0 comments on commit 66f442d

Please sign in to comment.