Skip to content

Commit 959fb23

Browse files
Moong0122ljharb
authored andcommitted
[Fix] jsx-indent-props: Apply indentation when operator is used in front of the upper line
Fixes #647
1 parent f84dc8b commit 959fb23

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
77

88
### Fixed
99
* [`no-unused-prop-types`]: Silence false positive on `never` type in TS ([#2815][] @pcorpet)
10+
* [`jsx-indent-props`]: Apply indentation when operator is used in front of the upper line ([#2808][] @Moong0122)
1011

1112
[#2815]: https://github.com/yannickcr/eslint-plugin-react/pull/2815
13+
[#2808]: https://github.com/yannickcr/eslint-plugin-react/pull/2808
1214

1315
## [7.21.3] - 2020.10.02
1416

lib/rules/jsx-indent-props.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ module.exports = {
6262
let indentType = 'space';
6363
/** @type {number|'first'} */
6464
let indentSize = 4;
65+
const line = {
66+
isUsingOperator: false,
67+
currentOperator: false
68+
};
6569

6670
if (context.options.length) {
6771
if (context.options[0] === 'first') {
@@ -119,6 +123,13 @@ module.exports = {
119123
}
120124

121125
const indent = regExp.exec(src);
126+
const useOperator = /^([ ]|[\t])*[:]/.test(src) || /^([ ]|[\t])*[?]/.test(src);
127+
line.currentOperator = false;
128+
if (useOperator) {
129+
line.isUsingOperator = true;
130+
line.currentOperator = true;
131+
}
132+
122133
return indent ? indent[0].length : 0;
123134
}
124135

@@ -130,6 +141,10 @@ module.exports = {
130141
function checkNodesIndent(nodes, indent) {
131142
nodes.forEach((node) => {
132143
const nodeIndent = getNodeIndent(node);
144+
if (line.isUsingOperator && !line.currentOperator && indentSize !== 'first') {
145+
indent += indentSize;
146+
line.isUsingOperator = false;
147+
}
133148
if (
134149
node.type !== 'ArrayExpression' && node.type !== 'ObjectExpression'
135150
&& nodeIndent !== indent && astUtil.isNodeFirstInLine(context, node)

tests/lib/rules/jsx-indent-props.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,71 @@ ruleTester.run('jsx-indent-props', rule, {
163163
].join('\n'),
164164
options: [2],
165165
errors: [{message: 'Expected indentation of 2 space characters but found 4.'}]
166+
}, {
167+
code: [
168+
'const test = true',
169+
' ? <span',
170+
' attr="value"',
171+
' />',
172+
' : <span',
173+
' attr="otherValue"',
174+
' />'
175+
].join('\n'),
176+
output: [
177+
'const test = true',
178+
' ? <span',
179+
' attr="value"',
180+
' />',
181+
' : <span',
182+
' attr="otherValue"',
183+
' />'
184+
].join('\n'),
185+
options: [2],
186+
errors: [
187+
{message: 'Expected indentation of 6 space characters but found 4.'},
188+
{message: 'Expected indentation of 6 space characters but found 4.'}
189+
]
190+
}, {
191+
code: [
192+
'{test.isLoading',
193+
' ? <Value/>',
194+
' : <OtherValue',
195+
' some={aaa}/>',
196+
'}'
197+
].join('\n'),
198+
output: [
199+
'{test.isLoading',
200+
' ? <Value/>',
201+
' : <OtherValue',
202+
' some={aaa}/>',
203+
'}'
204+
].join('\n'),
205+
options: [2],
206+
errors: [
207+
{message: 'Expected indentation of 6 space characters but found 4.'}
208+
]
209+
}, {
210+
code: [
211+
'{test.isLoading',
212+
' ? <Value/>',
213+
' : <OtherValue',
214+
' some={aaa}',
215+
' other={bbb}/>',
216+
'}'
217+
].join('\n'),
218+
output: [
219+
'{test.isLoading',
220+
' ? <Value/>',
221+
' : <OtherValue',
222+
' some={aaa}',
223+
' other={bbb}/>',
224+
'}'
225+
].join('\n'),
226+
options: [2],
227+
errors: [
228+
{message: 'Expected indentation of 6 space characters but found 4.'},
229+
{message: 'Expected indentation of 6 space characters but found 4.'}
230+
]
166231
}, {
167232
code: [
168233
'<App',

0 commit comments

Comments
 (0)