diff --git a/lib/rules/indent.js b/lib/rules/indent.js index 79b1063137e0..def169c33a9d 100644 --- a/lib/rules/indent.js +++ b/lib/rules/indent.js @@ -1588,15 +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]; + // If the line after a comment begins with a semicolon, validate the indentation against the previous line. + if ( + tokenBefore && tokenAfter && tokenAfter.type === "Punctuator" && tokenAfter.value === ";" + ) { + const firstTokenOfPrevLine = tokenInfo.firstTokensByLineNumber.get(tokenBefore.loc.start.line); + + offsets.setDesiredOffset(firstTokenOfLine, firstTokenOfPrevLine, 0); + + if (validateTokenIndent(firstTokenOfLine, offsets.getDesiredIndent(firstTokenOfPrevLine))) { + return; + } + } + const mayAlignWithBefore = tokenBefore && !hasBlankLinesBetween(tokenBefore, firstTokenOfLine); const mayAlignWithAfter = tokenAfter && !hasBlankLinesBetween(firstTokenOfLine, tokenAfter); @@ -1609,6 +1617,11 @@ module.exports = { } } + // If the token matches the expected 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 b38c97e960b1..ed2d2a0fc4f8 100644 --- a/tests/lib/rules/indent.js +++ b/tests/lib/rules/indent.js @@ -4950,6 +4950,76 @@ 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` + // 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` + /* comment */ + + ;[1, 2, 3].forEach(() => {}) + `, + unIndent` + /* comment */ + ;[1, 2, 3].forEach(() => {}) + `, // import expressions { @@ -9606,6 +9676,118 @@ 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` + // 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` + 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` + /* 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"]) + }, // import expressions {