Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix: refactor no-multi-spaces to avoid regex backtracking (fixes #9001)…
… (#9008)
  • Loading branch information
not-an-aardvark committed Jul 28, 2017
1 parent b74514d commit 0f97279
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 114 deletions.
161 changes: 48 additions & 113 deletions lib/rules/no-multi-spaces.js
Expand Up @@ -44,68 +44,11 @@ module.exports = {
},

create(context) {

// the index of the last comment that was checked
const sourceCode = context.getSourceCode(),
exceptions = { Property: true },
options = context.options[0] || {},
ignoreEOLComments = options.ignoreEOLComments;
let hasExceptions = true,
lastCommentIndex = 0;

if (options && options.exceptions) {
Object.keys(options.exceptions).forEach(key => {
if (options.exceptions[key]) {
exceptions[key] = true;
} else {
delete exceptions[key];
}
});
hasExceptions = Object.keys(exceptions).length > 0;
}

/**
* Checks if a given token is the last token of the line or not.
* @param {Token} token The token to check.
* @returns {boolean} Whether or not a token is at the end of the line it occurs in.
* @private
*/
function isLastTokenOfLine(token) {
const nextToken = sourceCode.getTokenAfter(token, { includeComments: true });

// nextToken is null if the comment is the last token in the program.
if (!nextToken) {
return true;
}

return !astUtils.isTokenOnSameLine(token, nextToken);
}

/**
* Determines if a given source index is in a comment or not by checking
* the index against the comment range. Since the check goes straight
* through the file, once an index is passed a certain comment, we can
* go to the next comment to check that.
* @param {int} index The source index to check.
* @param {ASTNode[]} comments An array of comment nodes.
* @returns {boolean} True if the index is within a comment, false if not.
* @private
*/
function isIndexInComment(index, comments) {
while (lastCommentIndex < comments.length) {
const comment = comments[lastCommentIndex];

if (comment.range[0] < index && index < comment.range[1]) {
return true;
} else if (index > comment.range[1]) {
lastCommentIndex++;
} else {
break;
}
}

return false;
}
const sourceCode = context.getSourceCode();
const options = context.options[0] || {};
const ignoreEOLComments = options.ignoreEOLComments;
const exceptions = Object.assign({ Property: true }, options.exceptions);
const hasExceptions = Object.keys(exceptions).filter(key => exceptions[key]).length > 0;

/**
* Formats value of given comment token for error message by truncating its length.
Expand All @@ -121,70 +64,62 @@ module.exports = {
return valueLines.length === 1 && value.length <= 12 ? value : formattedValue;
}

/**
* Creates a fix function that removes the multiple spaces between the two tokens
* @param {Token} leftToken left token
* @param {Token} rightToken right token
* @returns {Function} fix function
* @private
*/
function createFix(leftToken, rightToken) {
return function(fixer) {
return fixer.replaceTextRange([leftToken.range[1], rightToken.range[0]], " ");
};
}

//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------

return {
Program() {
sourceCode.tokensAndComments.forEach((leftToken, leftIndex, tokensAndComments) => {
if (leftIndex === tokensAndComments.length - 1) {
return;
}
const rightToken = tokensAndComments[leftIndex + 1];

const source = sourceCode.getText(),
allComments = sourceCode.getAllComments(),
pattern = /[^\s].*? {2,}/g;
let parent;

while (pattern.test(source)) {

// do not flag anything inside of comments
if (!isIndexInComment(pattern.lastIndex, allComments)) {

const token = sourceCode.getTokenByRangeStart(pattern.lastIndex, { includeComments: true });

if (token) {
if (ignoreEOLComments && astUtils.isCommentToken(token) && isLastTokenOfLine(token)) {
return;
}
// Ignore tokens that have less than 2 spaces between them or are on different lines
if (leftToken.range[1] + 2 > rightToken.range[0] || leftToken.loc.end.line < rightToken.loc.start.line) {
return;
}

const previousToken = sourceCode.getTokenBefore(token, { includeComments: true });
// Ignore comments that are the last token on their line if `ignoreEOLComments` is active.
if (
ignoreEOLComments &&
astUtils.isCommentToken(rightToken) &&
(
leftIndex === tokensAndComments.length - 2 ||
rightToken.loc.end.line < tokensAndComments[leftIndex + 2].loc.start.line
)
) {
return;
}

if (hasExceptions) {
parent = sourceCode.getNodeByRangeIndex(pattern.lastIndex - 1);
}
// Ignore tokens that are in a node in the "exceptions" object
if (hasExceptions) {
const parentNode = sourceCode.getNodeByRangeIndex(rightToken.range[0] - 1);

if (!parent || !exceptions[parent.type]) {
let value = token.value;

if (token.type === "Block") {
value = `/*${formatReportedCommentValue(token)}*/`;
} else if (token.type === "Line") {
value = `//${formatReportedCommentValue(token)}`;
}

context.report({
node: token,
loc: token.loc.start,
message: "Multiple spaces found before '{{value}}'.",
data: { value },
fix: createFix(previousToken, token)
});
}
if (parentNode && exceptions[parentNode.type]) {
return;
}
}

let displayValue;

if (rightToken.type === "Block") {
displayValue = `/*${formatReportedCommentValue(rightToken)}*/`;
} else if (rightToken.type === "Line") {
displayValue = `//${formatReportedCommentValue(rightToken)}`;
} else {
displayValue = rightToken.value;
}
}

context.report({
node: rightToken,
loc: rightToken.loc.start,
message: "Multiple spaces found before '{{displayValue}}'.",
data: { displayValue },
fix: fixer => fixer.replaceTextRange([leftToken.range[1], rightToken.range[0]], " ")
});
});
}
};

Expand Down
5 changes: 4 additions & 1 deletion tests/lib/rules/no-multi-spaces.js
Expand Up @@ -98,7 +98,10 @@ ruleTester.run("no-multi-spaces", rule, {

"foo\n\f bar",
"foo\n\u2003 bar",
"foo\n \f bar"
"foo\n \f bar",

// https://github.com/eslint/eslint/issues/9001
"a".repeat(2e5)
],

invalid: [
Expand Down

0 comments on commit 0f97279

Please sign in to comment.