Skip to content

Commit

Permalink
Update: fix indent bug on comments in ternary expressions (fixes #9729)…
Browse files Browse the repository at this point in the history
… (#9818)
  • Loading branch information
not-an-aardvark committed Jan 10, 2018
1 parent 6a5cd32 commit add1e70
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/rules/indent.js
Expand Up @@ -1094,8 +1094,8 @@ module.exports = {
const questionMarkToken = sourceCode.getFirstTokenBetween(node.test, node.consequent, token => token.type === "Punctuator" && token.value === "?");
const colonToken = sourceCode.getFirstTokenBetween(node.consequent, node.alternate, token => token.type === "Punctuator" && token.value === ":");

const firstConsequentToken = sourceCode.getTokenAfter(questionMarkToken, { includeComments: true });
const lastConsequentToken = sourceCode.getTokenBefore(colonToken, { includeComments: true });
const firstConsequentToken = sourceCode.getTokenAfter(questionMarkToken);
const lastConsequentToken = sourceCode.getTokenBefore(colonToken);
const firstAlternateToken = sourceCode.getTokenAfter(colonToken);

offsets.setDesiredOffset(questionMarkToken, firstToken, 1);
Expand Down
32 changes: 31 additions & 1 deletion tests/lib/rules/indent.js
Expand Up @@ -4796,7 +4796,16 @@ ruleTester.run("indent", rule, {
}
`,
options: [4, { ignoreComments: true }]
}
},
unIndent`
const obj = {
foo () {
return condition ? // comment
1 :
2
}
}
`
],

invalid: [
Expand Down Expand Up @@ -9261,6 +9270,27 @@ ruleTester.run("indent", rule, {
`,
options: [4, { ignoreComments: false }],
errors: expectedErrors([4, 4, 0, "Block"])
},
{
code: unIndent`
const obj = {
foo () {
return condition ? // comment
1 :
2
}
}
`,
output: unIndent`
const obj = {
foo () {
return condition ? // comment
1 :
2
}
}
`,
errors: expectedErrors([4, 12, 8, "Numeric"])
}
]
});

0 comments on commit add1e70

Please sign in to comment.