Skip to content

Commit

Permalink
Update: indentation of comment followed by semicolon (fixes #12232) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicataldo authored and ilyavolodin committed Sep 14, 2019
1 parent ae17d1c commit db2a29b
Show file tree
Hide file tree
Showing 2 changed files with 506 additions and 6 deletions.
22 changes: 16 additions & 6 deletions lib/rules/indent.js
Expand Up @@ -1588,18 +1588,23 @@ module.exports = {
return;
}

// If the token matches the expected expected indentation, don't report it.
if (validateTokenIndent(firstTokenOfLine, offsets.getDesiredIndent(firstTokenOfLine))) {
return;
}

if (astUtils.isCommentToken(firstTokenOfLine)) {
const tokenBefore = precedingTokens.get(firstTokenOfLine);
const tokenAfter = tokenBefore ? sourceCode.getTokenAfter(tokenBefore) : sourceCode.ast.tokens[0];

const mayAlignWithBefore = tokenBefore && !hasBlankLinesBetween(tokenBefore, firstTokenOfLine);
const mayAlignWithAfter = tokenAfter && !hasBlankLinesBetween(firstTokenOfLine, tokenAfter);

/*
* If a comment precedes a line that begins with a semicolon token, align to that token, i.e.
*
* let foo
* // comment
* ;(async () => {})()
*/
if (tokenAfter && astUtils.isSemicolonToken(tokenAfter) && !astUtils.isTokenOnSameLine(firstTokenOfLine, tokenAfter)) {
offsets.setDesiredOffset(firstTokenOfLine, tokenAfter, 0);
}

// If a comment matches the expected indentation of the token immediately before or after, don't report it.
if (
mayAlignWithBefore && validateTokenIndent(firstTokenOfLine, offsets.getDesiredIndent(tokenBefore)) ||
Expand All @@ -1609,6 +1614,11 @@ module.exports = {
}
}

// If the token matches the expected indentation, don't report it.
if (validateTokenIndent(firstTokenOfLine, offsets.getDesiredIndent(firstTokenOfLine))) {
return;
}

// Otherwise, report the token/comment.
report(firstTokenOfLine, offsets.getDesiredIndent(firstTokenOfLine));
});
Expand Down

0 comments on commit db2a29b

Please sign in to comment.