Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New] jsx-indent: Add indentLogicalExpressions option #2227

Merged
merged 1 commit into from Apr 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.'}
]
}]
});