Skip to content

Commit

Permalink
Fix: indentation of comment followed by semicolon (fixes #12232)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicataldo committed Sep 8, 2019
1 parent 0313441 commit 4f32d17
Show file tree
Hide file tree
Showing 2 changed files with 200 additions and 5 deletions.
23 changes: 18 additions & 5 deletions lib/rules/indent.js
Expand Up @@ -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);

Expand All @@ -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));
});
Expand Down
182 changes: 182 additions & 0 deletions tests/lib/rules/indent.js
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
{
Expand Down

0 comments on commit 4f32d17

Please sign in to comment.