Skip to content

Commit

Permalink
Fix: correctly handle commented code in indent autofixer (fixes #7604
Browse files Browse the repository at this point in the history
…) (#7606)
  • Loading branch information
not-an-aardvark authored and ilyavolodin committed Nov 15, 2016
1 parent bd0514c commit 0643bfe
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
13 changes: 4 additions & 9 deletions lib/rules/indent.js
Expand Up @@ -255,20 +255,18 @@ module.exports = {
* @param {int} lastNodeCheckEndOffset Number of charecters to skip from the end
* @returns {void}
*/
function report(node, needed, gottenSpaces, gottenTabs, loc, isLastNodeCheck, lastNodeCheckEndOffset) {
function report(node, needed, gottenSpaces, gottenTabs, loc, isLastNodeCheck) {
if (gottenSpaces && gottenTabs) {

// To avoid conflicts with `no-mixed-spaces-and-tabs`, don't report lines that have both spaces and tabs.
return;
}

lastNodeCheckEndOffset = lastNodeCheckEndOffset || 0;

const desiredIndent = (indentType === "space" ? " " : "\t").repeat(needed);

const textRange = isLastNodeCheck
? [node.range[1] - gottenSpaces - gottenTabs - 1 - lastNodeCheckEndOffset, node.range[1] - 1 - lastNodeCheckEndOffset]
: [node.range[0] - gottenSpaces - gottenTabs, node.range[0]];
? [node.range[1] - node.loc.end.column, node.range[1] - node.loc.end.column + gottenSpaces + gottenTabs]
: [node.range[0] - node.loc.start.column, node.range[0] - node.loc.start.column + gottenSpaces + gottenTabs];

context.report({
node,
Expand Down Expand Up @@ -406,13 +404,11 @@ module.exports = {
*/
function checkLastReturnStatementLineIndent(node, firstLineIndent) {
const nodeLastToken = sourceCode.getLastToken(node);
let lastNodeCheckEndOffset = 0;
let lastToken = nodeLastToken;

// in case if return statement ends with ');' we have traverse back to ')'
// otherwise we'll measure indent for ';' and replace ')'
while (lastToken.value !== ")") {
lastNodeCheckEndOffset++;
lastToken = sourceCode.getTokenBefore(lastToken);
}

Expand All @@ -433,8 +429,7 @@ module.exports = {
endIndent.space,
endIndent.tab,
{ line: lastToken.loc.start.line, column: lastToken.loc.start.column },
true,
lastNodeCheckEndOffset
true
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/rules/indent/indent-valid-fixture-1.js
Expand Up @@ -43,7 +43,7 @@ if (a) {
/**/var b; // NO ERROR: single line multi-line comments followed by code is OK
/*
*
*/var b; // ERROR: multi-line comments followed by code is not OK
*/ var b; // ERROR: multi-line comments followed by code is not OK

var arr = [
a,
Expand Down
26 changes: 26 additions & 0 deletions tests/lib/rules/indent.js
Expand Up @@ -3504,6 +3504,32 @@ ruleTester.run("indent", rule, {
")",
parserOptions: {ecmaFeatures: {globalReturn: true}},
errors: expectedErrors([3, 0, 4, "ReturnStatement"])
},

// https://github.com/eslint/eslint/issues/7604
{
code:
"if (foo) {\n" +
" /* comment */bar();\n" +
"}",
output:
"if (foo) {\n" +
" /* comment */bar();\n" +
"}",
errors: expectedErrors([2, 4, 8, "ExpressionStatement"])
},
{
code:
"foo('bar',\n" +
" /** comment */{\n" +
" ok: true" +
" });",
output:
"foo('bar',\n" +
" /** comment */{\n" +
" ok: true" +
" });",
errors: expectedErrors([2, 4, 8, "ObjectExpression"])
}
]
});

0 comments on commit 0643bfe

Please sign in to comment.