Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: Correctly indent JSXText with trailing linebreaks (fixes #9878) #10055

Merged
merged 1 commit into from Mar 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion lib/rules/indent.js
Expand Up @@ -779,6 +779,19 @@ module.exports = {
return (statement.type === "ExpressionStatement" || statement.type === "VariableDeclaration") && statement.parent.type === "Program";
}

/**
* Counts the number of linebreaks that follow the last non-whitespace character in a string
* @param {string} string The string to check
* @returns {number} The number of JavaScript linebreaks that follow the last non-whitespace character,
* or the total number of linebreaks if the string is all whitespace.
*/
function countTrailingLinebreaks(string) {
const trailingWhitespace = string.match(/\s*$/)[0];
const linebreakMatches = trailingWhitespace.match(astUtils.createGlobalLinebreakMatcher());

return linebreakMatches === null ? 0 : linebreakMatches.length;
}

/**
* Check indentation for lists of elements (arrays, objects, function params)
* @param {ASTNode[]} elements List of elements that should be offset
Expand Down Expand Up @@ -836,8 +849,12 @@ module.exports = {
} else {
const previousElement = elements[index - 1];
const firstTokenOfPreviousElement = previousElement && getFirstToken(previousElement);
const previousElementLastToken = previousElement && sourceCode.getLastToken(previousElement);

if (previousElement && sourceCode.getLastToken(previousElement).loc.end.line > startToken.loc.end.line) {
if (
previousElement &&
previousElementLastToken.loc.end.line - countTrailingLinebreaks(previousElementLastToken.value) > startToken.loc.end.line
) {
offsets.setDesiredOffsets(element.range, firstTokenOfPreviousElement, 0);
}
}
Expand Down
36 changes: 36 additions & 0 deletions tests/lib/rules/indent.js
Expand Up @@ -4607,6 +4607,16 @@ ruleTester.run("indent", rule, {
);
}
`,
unIndent`
<div>foo
<div>bar</div>
</div>
`,
unIndent`
<small>Foo bar&nbsp;
<a>baz qux</a>.
</small>
`,
{
code: unIndent`
a(b
Expand Down Expand Up @@ -9136,6 +9146,32 @@ ruleTester.run("indent", rule, {
`,
errors: expectedErrors([3, 8, 6, "Block"])
},
{
code: unIndent`
<div>foo
<div>bar</div>
</div>
`,
output: unIndent`
<div>foo
<div>bar</div>
</div>
`,
errors: expectedErrors([2, 4, 0, "Punctuator"])
},
{
code: unIndent`
<small>Foo bar&nbsp;
<a>baz qux</a>.
</small>
`,
output: unIndent`
<small>Foo bar&nbsp;
<a>baz qux</a>.
</small>
`,
errors: expectedErrors([2, 4, 0, "Punctuator"])
},
{
code: unIndent`
({
Expand Down