Skip to content

Commit

Permalink
Merge 91c38fa into 6bb1604
Browse files Browse the repository at this point in the history
  • Loading branch information
mdnsk committed Apr 3, 2019
2 parents 6bb1604 + 91c38fa commit 271aa7e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
18 changes: 16 additions & 2 deletions docs/rules/jsx-indent.md
Expand Up @@ -31,11 +31,11 @@ The following patterns are considered warnings:
## Rule Options

It takes an option as the second parameter which can be `"tab"` for tab-based indentation or a positive number for space indentations.
To enable checking the indentation of attributes, use the third parameter to turn on the `checkAttributes` option (default is false).
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.

```js
...
"react/jsx-indent": [<enabled>, 'tab'|<number>, {checkAttributes: <boolean>}]
"react/jsx-indent": [<enabled>, 'tab'|<number>, {checkAttributes: <boolean>, indentLogicalExpressions: <boolean>}]
...
```

Expand All @@ -61,6 +61,13 @@ The following patterns are considered warnings:
}
/>
</App>

// [2, 2, {indentLogicalExpressions: true}]
<App>
{condition && (
<Hello />
)}
</App>
```
The following patterns are **not** warnings:
Expand Down Expand Up @@ -92,6 +99,13 @@ The following patterns are **not** warnings:
}
/>
</App>

// [2, 2, {indentLogicalExpressions: true}]
<App>
{condition && (
<Hello />
)}
</App>
```
## When not to use
Expand Down
7 changes: 6 additions & 1 deletion lib/rules/jsx-indent.js
Expand Up @@ -55,6 +55,9 @@ module.exports = {
properties: {
checkAttributes: {
type: 'boolean'
},
indentLogicalExpressions: {
type: 'boolean'
}
},
additionalProperties: false
Expand Down Expand Up @@ -83,6 +86,7 @@ module.exports = {
const indentChar = indentType === 'space' ? ' ' : '\t';
const options = context.options[1] || {};
const checkAttributes = options.checkAttributes || false;
const indentLogicalExpressions = options.indentLogicalExpressions || false;

/**
* Responsible for fixing the indentation issue fix
Expand Down Expand Up @@ -176,7 +180,8 @@ module.exports = {
node.parent &&
node.parent.parent &&
node.parent.parent.type === 'LogicalExpression' &&
node.parent.parent.right === node.parent
node.parent.parent.right === node.parent &&
!indentLogicalExpressions
);
}

Expand Down
40 changes: 40 additions & 0 deletions tests/lib/rules/jsx-indent.js
Expand Up @@ -832,6 +832,19 @@ const Component = () => (
}
`,
options: [2, {checkAttributes: true}]
}, {
code: `
function Foo() {
return (
<div>
{condition && (
<p>Bar</p>
)}
</div>
);
}
`,
options: [2, {indentLogicalExpressions: true}]
}],

invalid: [{
Expand Down Expand Up @@ -1652,5 +1665,32 @@ const Component = () => (
errors: [
{message: 'Expected indentation of 2 tab characters but found 0.'}
]
}, {
code: `
function Foo() {
return (
<div>
{condition && (
<p>Bar</p>
)}
</div>
);
}
`,
output: `
function Foo() {
return (
<div>
{condition && (
<p>Bar</p>
)}
</div>
);
}
`,
options: [2, {indentLogicalExpressions: true}],
errors: [
{message: 'Expected indentation of 12 space characters but found 10.'}
]
}]
});

0 comments on commit 271aa7e

Please sign in to comment.