From db2a29beb0fa28183f65bf9e659c66c03a8918b5 Mon Sep 17 00:00:00 2001 From: Kai Cataldo <7041728+kaicataldo@users.noreply.github.com> Date: Fri, 13 Sep 2019 20:38:04 -0400 Subject: [PATCH] Update: indentation of comment followed by semicolon (fixes #12232) (#12243) --- lib/rules/indent.js | 22 +- tests/lib/rules/indent.js | 490 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 506 insertions(+), 6 deletions(-) diff --git a/lib/rules/indent.js b/lib/rules/indent.js index 79b1063137e..f44eafa715c 100644 --- a/lib/rules/indent.js +++ b/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)); }); diff --git a/tests/lib/rules/indent.js b/tests/lib/rules/indent.js index b38c97e960b..e2d6202a998 100644 --- a/tests/lib/rules/indent.js +++ b/tests/lib/rules/indent.js @@ -4950,6 +4950,212 @@ ruleTester.run("indent", rule, { bar }]; `, + unIndent` + let foo + + // comment + + ;(async () => {})() + `, + unIndent` + let foo + // comment + + ;(async () => {})() + `, + unIndent` + let foo + + // comment + ;(async () => {})() + `, + unIndent` + let foo + // comment + ;(async () => {})() + `, + unIndent` + let foo + + /* comment */; + + (async () => {})() + `, + unIndent` + let foo + /* comment */; + + (async () => {})() + `, + unIndent` + let foo + + /* comment */; + (async () => {})() + `, + unIndent` + let foo + /* comment */; + (async () => {})() + `, + unIndent` + let foo + /* comment */; + + (async () => {})() + `, + unIndent` + let foo + /* comment */; + (async () => {})() + `, + unIndent` + // comment + + ;(async () => {})() + `, + unIndent` + // comment + ;(async () => {})() + `, + unIndent` + { + let foo + + // comment + + ;(async () => {})() + } + `, + unIndent` + { + let foo + // comment + ;(async () => {})() + } + `, + unIndent` + { + // comment + + ;(async () => {})() + } + `, + unIndent` + { + // comment + ;(async () => {})() + } + `, + unIndent` + const foo = 1 + const bar = foo + + /* comment */ + + ;[1, 2, 3].forEach(() => {}) + `, + unIndent` + const foo = 1 + const bar = foo + /* comment */ + + ;[1, 2, 3].forEach(() => {}) + `, + unIndent` + const foo = 1 + const bar = foo + + /* comment */ + ;[1, 2, 3].forEach(() => {}) + `, + unIndent` + const foo = 1 + const bar = foo + /* comment */ + ;[1, 2, 3].forEach(() => {}) + `, + unIndent` + const foo = 1 + const bar = foo + + /* comment */; + + [1, 2, 3].forEach(() => {}) + `, + unIndent` + const foo = 1 + const bar = foo + /* comment */; + + [1, 2, 3].forEach(() => {}) + `, + unIndent` + const foo = 1 + const bar = foo + + /* comment */; + [1, 2, 3].forEach(() => {}) + `, + unIndent` + const foo = 1 + const bar = foo + /* comment */; + [1, 2, 3].forEach(() => {}) + `, + unIndent` + const foo = 1 + const bar = foo + /* comment */; + + [1, 2, 3].forEach(() => {}) + `, + unIndent` + const foo = 1 + const bar = foo + /* comment */; + [1, 2, 3].forEach(() => {}) + `, + unIndent` + /* comment */ + + ;[1, 2, 3].forEach(() => {}) + `, + unIndent` + /* comment */ + ;[1, 2, 3].forEach(() => {}) + `, + unIndent` + { + const foo = 1 + const bar = foo + + /* comment */ + + ;[1, 2, 3].forEach(() => {}) + } + `, + unIndent` + { + const foo = 1 + const bar = foo + /* comment */ + ;[1, 2, 3].forEach(() => {}) + } + `, + unIndent` + { + /* comment */ + + ;[1, 2, 3].forEach(() => {}) + } + `, + unIndent` + { + /* comment */ + ;[1, 2, 3].forEach(() => {}) + } + `, // import expressions { @@ -9606,6 +9812,290 @@ ruleTester.run("indent", rule, { `, errors: expectedErrors([5, 0, 4, "Line"]) }, + { + code: unIndent` + let foo + + // comment + + ;(async () => {})() + `, + output: unIndent` + let foo + + // comment + + ;(async () => {})() + `, + errors: expectedErrors([3, 0, 4, "Line"]) + }, + { + code: unIndent` + let foo + // comment + ;(async () => {})() + `, + output: unIndent` + let foo + // comment + ;(async () => {})() + `, + errors: expectedErrors([2, 0, 4, "Line"]) + }, + { + code: unIndent` + let foo + + /* comment */; + + (async () => {})() + `, + output: unIndent` + let foo + + /* comment */; + + (async () => {})() + `, + errors: expectedErrors([3, 4, 0, "Block"]) + }, + { + code: unIndent` + // comment + + ;(async () => {})() + `, + output: unIndent` + // comment + + ;(async () => {})() + `, + errors: expectedErrors([1, 0, 4, "Line"]) + }, + { + code: unIndent` + // comment + ;(async () => {})() + `, + output: unIndent` + // comment + ;(async () => {})() + `, + errors: expectedErrors([1, 0, 4, "Line"]) + }, + { + code: unIndent` + { + let foo + + // comment + + ;(async () => {})() + + } + `, + output: unIndent` + { + let foo + + // comment + + ;(async () => {})() + + } + `, + errors: expectedErrors([4, 4, 8, "Line"]) + }, + { + code: unIndent` + { + let foo + // comment + ;(async () => {})() + + } + `, + output: unIndent` + { + let foo + // comment + ;(async () => {})() + + } + `, + errors: expectedErrors([3, 4, 8, "Line"]) + }, + { + code: unIndent` + { + let foo + + /* comment */; + + (async () => {})() + + } + `, + output: unIndent` + { + let foo + + /* comment */; + + (async () => {})() + + } + `, + errors: expectedErrors([4, 8, 4, "Block"]) + }, + { + code: unIndent` + const foo = 1 + const bar = foo + + /* comment */ + + ;[1, 2, 3].forEach(() => {}) + `, + output: unIndent` + const foo = 1 + const bar = foo + + /* comment */ + + ;[1, 2, 3].forEach(() => {}) + `, + errors: expectedErrors([4, 0, 4, "Block"]) + }, + { + code: unIndent` + const foo = 1 + const bar = foo + /* comment */ + ;[1, 2, 3].forEach(() => {}) + `, + output: unIndent` + const foo = 1 + const bar = foo + /* comment */ + ;[1, 2, 3].forEach(() => {}) + `, + errors: expectedErrors([3, 0, 4, "Block"]) + }, + { + code: unIndent` + const foo = 1 + const bar = foo + + /* comment */; + + [1, 2, 3].forEach(() => {}) + `, + output: unIndent` + const foo = 1 + const bar = foo + + /* comment */; + + [1, 2, 3].forEach(() => {}) + `, + errors: expectedErrors([4, 4, 0, "Block"]) + }, + { + code: unIndent` + /* comment */ + + ;[1, 2, 3].forEach(() => {}) + `, + output: unIndent` + /* comment */ + + ;[1, 2, 3].forEach(() => {}) + `, + errors: expectedErrors([1, 0, 4, "Block"]) + }, + { + code: unIndent` + /* comment */ + ;[1, 2, 3].forEach(() => {}) + `, + output: unIndent` + /* comment */ + ;[1, 2, 3].forEach(() => {}) + `, + errors: expectedErrors([1, 0, 4, "Block"]) + }, + { + code: unIndent` + { + const foo = 1 + const bar = foo + + /* comment */ + + ;[1, 2, 3].forEach(() => {}) + + } + `, + output: unIndent` + { + const foo = 1 + const bar = foo + + /* comment */ + + ;[1, 2, 3].forEach(() => {}) + + } + `, + errors: expectedErrors([5, 4, 8, "Block"]) + }, + { + code: unIndent` + { + const foo = 1 + const bar = foo + /* comment */ + ;[1, 2, 3].forEach(() => {}) + + } + `, + output: unIndent` + { + const foo = 1 + const bar = foo + /* comment */ + ;[1, 2, 3].forEach(() => {}) + + } + `, + errors: expectedErrors([4, 4, 8, "Block"]) + }, + { + code: unIndent` + { + const foo = 1 + const bar = foo + + /* comment */; + + [1, 2, 3].forEach(() => {}) + + } + `, + output: unIndent` + { + const foo = 1 + const bar = foo + + /* comment */; + + [1, 2, 3].forEach(() => {}) + + } + `, + errors: expectedErrors([5, 8, 4, "Block"]) + }, // import expressions {