From 05db44c56047130efe5e9ad7b4a32dca9c1e38bc Mon Sep 17 00:00:00 2001 From: Kai Cataldo Date: Sun, 8 Sep 2019 21:09:29 -0400 Subject: [PATCH] Align to next line instead of previous --- lib/rules/indent.js | 31 ++--- tests/lib/rules/indent.js | 240 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 256 insertions(+), 15 deletions(-) diff --git a/lib/rules/indent.js b/lib/rules/indent.js index a87e323c7d1..76bde3a1d86 100644 --- a/lib/rules/indent.js +++ b/lib/rules/indent.js @@ -1591,20 +1591,6 @@ module.exports = { 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 && astUtils.isSemicolonToken(tokenAfter) - ) { - 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); @@ -1615,9 +1601,24 @@ module.exports = { ) { return; } + + /* + * 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 (validateTokenIndent(firstTokenOfLine, offsets.getDesiredIndent(tokenAfter))) { + return; + } + } } - // If the token matches the expected expected indentation, don't report it. + // If the token matches the expected indentation, don't report it. if (validateTokenIndent(firstTokenOfLine, offsets.getDesiredIndent(firstTokenOfLine))) { return; } diff --git a/tests/lib/rules/indent.js b/tests/lib/rules/indent.js index 082c784e77f..e2d6202a998 100644 --- a/tests/lib/rules/indent.js +++ b/tests/lib/rules/indent.js @@ -4974,6 +4974,41 @@ ruleTester.run("indent", rule, { // 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 @@ -4992,6 +5027,26 @@ ruleTester.run("indent", rule, { ;(async () => {})() } `, + unIndent` + { + let foo + // comment + ;(async () => {})() + } + `, + unIndent` + { + // comment + + ;(async () => {})() + } + `, + unIndent` + { + // comment + ;(async () => {})() + } + `, unIndent` const foo = 1 const bar = foo @@ -5020,6 +5075,47 @@ ruleTester.run("indent", rule, { /* 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 */ @@ -5037,7 +5133,27 @@ ruleTester.run("indent", rule, { /* 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(() => {}) } `, @@ -9726,6 +9842,23 @@ ruleTester.run("indent", rule, { `, 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 @@ -9773,6 +9906,48 @@ ruleTester.run("indent", rule, { `, 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 @@ -9807,6 +9982,25 @@ ruleTester.run("indent", rule, { `, 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 */ @@ -9856,6 +10050,52 @@ ruleTester.run("indent", rule, { `, 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 {