Skip to content

Commit

Permalink
Align to next line instead of previous
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicataldo committed Sep 9, 2019
1 parent 1cfc491 commit d5c72b2
Show file tree
Hide file tree
Showing 2 changed files with 269 additions and 15 deletions.
31 changes: 16 additions & 15 deletions lib/rules/indent.js
Expand Up @@ -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);

Expand All @@ -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;
}
Expand Down
253 changes: 253 additions & 0 deletions tests/lib/rules/indent.js
Expand Up @@ -4974,6 +4974,47 @@ 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`
let foo
/* comment */;
(async () => {})()
`,
unIndent`
// comment
Expand All @@ -4992,6 +5033,26 @@ ruleTester.run("indent", rule, {
;(async () => {})()
}
`,
unIndent`
{
let foo
// comment
;(async () => {})()
}
`,
unIndent`
{
// comment
;(async () => {})()
}
`,
unIndent`
{
// comment
;(async () => {})()
}
`,
unIndent`
const foo = 1
const bar = foo
Expand Down Expand Up @@ -5020,6 +5081,54 @@ 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`
const foo = 1
const bar = foo
/* comment */;
[1, 2, 3].forEach(() => {})
`,
unIndent`
/* comment */
Expand All @@ -5037,7 +5146,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(() => {})
}
`,

Expand Down Expand Up @@ -9726,6 +9855,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
Expand Down Expand Up @@ -9773,6 +9919,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
Expand Down Expand Up @@ -9807,6 +9995,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 */
Expand Down Expand Up @@ -9856,6 +10063,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
{
Expand Down

0 comments on commit d5c72b2

Please sign in to comment.