Skip to content

Commit 231acd9

Browse files
author
Yannick Croissant
committed
Fix jsx-indent for arrays in jsx (fixes #947)
1 parent 40882e5 commit 231acd9

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

lib/rules/jsx-indent.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,13 +226,16 @@ module.exports = {
226226
}
227227
// Use the parent in a list or an array
228228
if (prevToken.type === 'JSXText' || prevToken.type === 'Punctuator' && prevToken.value === ',') {
229-
prevToken = sourceCode.getNodeByRangeIndex(prevToken.start).parent;
229+
prevToken = sourceCode.getNodeByRangeIndex(prevToken.start);
230+
prevToken = prevToken.type === 'Literal' ? prevToken.parent : prevToken;
230231
// Use the first non-punctuator token in a conditional expression
231232
} else if (prevToken.type === 'Punctuator' && prevToken.value === ':') {
232233
do {
233234
prevToken = sourceCode.getTokenBefore(prevToken);
234235
} while (prevToken.type === 'Punctuator');
235236
}
237+
prevToken = prevToken.type === 'JSXExpressionContainer' ? prevToken.expression : prevToken;
238+
236239
var parentElementIndent = getNodeIndent(prevToken);
237240
var indent = (
238241
prevToken.loc.start.line === node.loc.start.line ||

tests/lib/rules/jsx-indent.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,30 @@ ruleTester.run('jsx-indent', rule, {
187187
].join('\n'),
188188
parserOptions: parserOptions,
189189
options: [2]
190+
}, {
191+
code: [
192+
'<div>',
193+
' {',
194+
' [',
195+
' <Foo />,',
196+
' <Bar />',
197+
' ]',
198+
' }',
199+
'</div>'
200+
].join('\n'),
201+
parserOptions: parserOptions
202+
}, {
203+
code: [
204+
'<div>',
205+
' {foo &&',
206+
' [',
207+
' <Foo />,',
208+
' <Bar />',
209+
' ]',
210+
' }',
211+
'</div>'
212+
].join('\n'),
213+
parserOptions: parserOptions
190214
}, {
191215
// Literals indentation is not touched
192216
code: [
@@ -626,6 +650,56 @@ ruleTester.run('jsx-indent', rule, {
626650
errors: [
627651
{message: 'Expected indentation of 2 space characters but found 0.'}
628652
]
653+
}, {
654+
code: [
655+
'<div>',
656+
' {',
657+
' [',
658+
' <Foo />,',
659+
' <Bar />',
660+
' ]',
661+
' }',
662+
'</div>'
663+
].join('\n'),
664+
output: [
665+
'<div>',
666+
' {',
667+
' [',
668+
' <Foo />,',
669+
' <Bar />',
670+
' ]',
671+
' }',
672+
'</div>'
673+
].join('\n'),
674+
parserOptions: parserOptions,
675+
errors: [
676+
{message: 'Expected indentation of 12 space characters but found 8.'}
677+
]
678+
}, {
679+
code: [
680+
'<div>',
681+
' {foo &&',
682+
' [',
683+
' <Foo />,',
684+
' <Bar />',
685+
' ]',
686+
' }',
687+
'</div>'
688+
].join('\n'),
689+
output: [
690+
'<div>',
691+
' {foo &&',
692+
' [',
693+
' <Foo />,',
694+
' <Bar />',
695+
' ]',
696+
' }',
697+
'</div>'
698+
].join('\n'),
699+
parserOptions: parserOptions,
700+
errors: [
701+
{message: 'Expected indentation of 12 space characters but found 8.'}
702+
]
629703
}, {
630704
// Multiline ternary
631705
// (colon at the end of the first expression)

0 commit comments

Comments
 (0)