Skip to content
Permalink
Browse files

Update: indentation of comment followed by semicolon (fixes #12232) (#…

  • Loading branch information
kaicataldo authored and ilyavolodin committed Sep 14, 2019
1 parent ae17d1c commit db2a29beb0fa28183f65bf9e659c66c03a8918b5
Showing with 506 additions and 6 deletions.
  1. +16 −6 lib/rules/indent.js
  2. +490 −0 tests/lib/rules/indent.js
@@ -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)) ||
@@ -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));
});

0 comments on commit db2a29b

Please sign in to comment.
You can’t perform that action at this time.