Skip to content

Commit

Permalink
Fix jsx-indent for arrays in jsx (fixes #947)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yannick Croissant committed Nov 12, 2016
1 parent 40882e5 commit 231acd9
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/rules/jsx-indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,16 @@ module.exports = {
}
// Use the parent in a list or an array
if (prevToken.type === 'JSXText' || prevToken.type === 'Punctuator' && prevToken.value === ',') {
prevToken = sourceCode.getNodeByRangeIndex(prevToken.start).parent;
prevToken = sourceCode.getNodeByRangeIndex(prevToken.start);
prevToken = prevToken.type === 'Literal' ? prevToken.parent : prevToken;
// Use the first non-punctuator token in a conditional expression
} else if (prevToken.type === 'Punctuator' && prevToken.value === ':') {
do {
prevToken = sourceCode.getTokenBefore(prevToken);
} while (prevToken.type === 'Punctuator');
}
prevToken = prevToken.type === 'JSXExpressionContainer' ? prevToken.expression : prevToken;

var parentElementIndent = getNodeIndent(prevToken);
var indent = (
prevToken.loc.start.line === node.loc.start.line ||
Expand Down
74 changes: 74 additions & 0 deletions tests/lib/rules/jsx-indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,30 @@ ruleTester.run('jsx-indent', rule, {
].join('\n'),
parserOptions: parserOptions,
options: [2]
}, {
code: [
'<div>',
' {',
' [',
' <Foo />,',
' <Bar />',
' ]',
' }',
'</div>'
].join('\n'),
parserOptions: parserOptions
}, {
code: [
'<div>',
' {foo &&',
' [',
' <Foo />,',
' <Bar />',
' ]',
' }',
'</div>'
].join('\n'),
parserOptions: parserOptions
}, {
// Literals indentation is not touched
code: [
Expand Down Expand Up @@ -626,6 +650,56 @@ ruleTester.run('jsx-indent', rule, {
errors: [
{message: 'Expected indentation of 2 space characters but found 0.'}
]
}, {
code: [
'<div>',
' {',
' [',
' <Foo />,',
' <Bar />',
' ]',
' }',
'</div>'
].join('\n'),
output: [
'<div>',
' {',
' [',
' <Foo />,',
' <Bar />',
' ]',
' }',
'</div>'
].join('\n'),
parserOptions: parserOptions,
errors: [
{message: 'Expected indentation of 12 space characters but found 8.'}
]
}, {
code: [
'<div>',
' {foo &&',
' [',
' <Foo />,',
' <Bar />',
' ]',
' }',
'</div>'
].join('\n'),
output: [
'<div>',
' {foo &&',
' [',
' <Foo />,',
' <Bar />',
' ]',
' }',
'</div>'
].join('\n'),
parserOptions: parserOptions,
errors: [
{message: 'Expected indentation of 12 space characters but found 8.'}
]
}, {
// Multiline ternary
// (colon at the end of the first expression)
Expand Down

0 comments on commit 231acd9

Please sign in to comment.