Skip to content

Commit 91c38fa

Browse files
mdnskljharb
authored andcommitted
[New] jsx-indent: Add indentLogicalExpressions option
1 parent 6bb1604 commit 91c38fa

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

docs/rules/jsx-indent.md

+16-2
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ The following patterns are considered warnings:
3131
## Rule Options
3232

3333
It takes an option as the second parameter which can be `"tab"` for tab-based indentation or a positive number for space indentations.
34-
To enable checking the indentation of attributes, use the third parameter to turn on the `checkAttributes` option (default is false).
34+
To enable checking the indentation of attributes or add indentation to logical expressions, use the third parameter to turn on the `checkAttributes` (default is false) and `indentLogicalExpressions` (default is false) respectively.
3535

3636
```js
3737
...
38-
"react/jsx-indent": [<enabled>, 'tab'|<number>, {checkAttributes: <boolean>}]
38+
"react/jsx-indent": [<enabled>, 'tab'|<number>, {checkAttributes: <boolean>, indentLogicalExpressions: <boolean>}]
3939
...
4040
```
4141

@@ -61,6 +61,13 @@ The following patterns are considered warnings:
6161
}
6262
/>
6363
</App>
64+
65+
// [2, 2, {indentLogicalExpressions: true}]
66+
<App>
67+
{condition && (
68+
<Hello />
69+
)}
70+
</App>
6471
```
6572
6673
The following patterns are **not** warnings:
@@ -92,6 +99,13 @@ The following patterns are **not** warnings:
9299
}
93100
/>
94101
</App>
102+
103+
// [2, 2, {indentLogicalExpressions: true}]
104+
<App>
105+
{condition && (
106+
<Hello />
107+
)}
108+
</App>
95109
```
96110
97111
## When not to use

lib/rules/jsx-indent.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ module.exports = {
5555
properties: {
5656
checkAttributes: {
5757
type: 'boolean'
58+
},
59+
indentLogicalExpressions: {
60+
type: 'boolean'
5861
}
5962
},
6063
additionalProperties: false
@@ -83,6 +86,7 @@ module.exports = {
8386
const indentChar = indentType === 'space' ? ' ' : '\t';
8487
const options = context.options[1] || {};
8588
const checkAttributes = options.checkAttributes || false;
89+
const indentLogicalExpressions = options.indentLogicalExpressions || false;
8690

8791
/**
8892
* Responsible for fixing the indentation issue fix
@@ -176,7 +180,8 @@ module.exports = {
176180
node.parent &&
177181
node.parent.parent &&
178182
node.parent.parent.type === 'LogicalExpression' &&
179-
node.parent.parent.right === node.parent
183+
node.parent.parent.right === node.parent &&
184+
!indentLogicalExpressions
180185
);
181186
}
182187

tests/lib/rules/jsx-indent.js

+40
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,19 @@ const Component = () => (
832832
}
833833
`,
834834
options: [2, {checkAttributes: true}]
835+
}, {
836+
code: `
837+
function Foo() {
838+
return (
839+
<div>
840+
{condition && (
841+
<p>Bar</p>
842+
)}
843+
</div>
844+
);
845+
}
846+
`,
847+
options: [2, {indentLogicalExpressions: true}]
835848
}],
836849

837850
invalid: [{
@@ -1652,5 +1665,32 @@ const Component = () => (
16521665
errors: [
16531666
{message: 'Expected indentation of 2 tab characters but found 0.'}
16541667
]
1668+
}, {
1669+
code: `
1670+
function Foo() {
1671+
return (
1672+
<div>
1673+
{condition && (
1674+
<p>Bar</p>
1675+
)}
1676+
</div>
1677+
);
1678+
}
1679+
`,
1680+
output: `
1681+
function Foo() {
1682+
return (
1683+
<div>
1684+
{condition && (
1685+
<p>Bar</p>
1686+
)}
1687+
</div>
1688+
);
1689+
}
1690+
`,
1691+
options: [2, {indentLogicalExpressions: true}],
1692+
errors: [
1693+
{message: 'Expected indentation of 12 space characters but found 10.'}
1694+
]
16551695
}]
16561696
});

0 commit comments

Comments
 (0)