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

[Fix] jsx-indent-props: Reset line.isUsingOperator correctly after ternary (#3146) #3157

Merged
merged 1 commit into from Dec 22, 2021
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -8,12 +8,16 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
### Added
* [`function-component-definition`]: support namedComponents option being an array ([#3129][] @petersendidit)

### Fixed
* [`jsx-indent-props`]: Reset `line.isUsingOperator` correctly after ternary ([#3146][] @tobiaswaltl)

### Changed
* [Refactor] [`no-arrow-function-lifecycle`], [`no-unused-class-component-methods`]: use report/messages convention (@ljharb)
* [Tests] component detection: Add testing scaffolding ([#3149][] @duncanbeevers)
* [New] component detection: track React imports ([#3149][] @duncanbeevers)

[#3149]: https://github.com/yannickcr/eslint-plugin-react/pull/3149
[#3146]: https://github.com/yannickcr/eslint-plugin-react/pull/3146
[#3129]: https://github.com/yannickcr/eslint-plugin-react/pull/3129

## [7.27.1] - 2021.11.18
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/jsx-indent-props.js
Expand Up @@ -153,7 +153,7 @@ module.exports = {

const indent = regExp.exec(src);
const useOperator = /^([ ]|[\t])*[:]/.test(src) || /^([ ]|[\t])*[?]/.test(src);
const useBracket = /^([ ]|[\t])*[<]/.test(src);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add some test cases that use tabs, since that's what's being removed here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I've added them.

const useBracket = /[<]/.test(src);

line.currentOperator = false;
if (useOperator) {
Expand Down
264 changes: 264 additions & 0 deletions tests/lib/rules/jsx-indent-props.js
Expand Up @@ -195,6 +195,90 @@ ruleTester.run('jsx-indent-props', rule, {
},
],
},
{
code: `
const F = () => {
const foo = true
? <div id="id">test</div>
: false;

return <div
id="id"
>
test
</div>
ljharb marked this conversation as resolved.
Show resolved Hide resolved
}
`,
options: [
{
indentMode: 2,
ignoreTernaryOperator: false,
},
],
},
{
code: `
const F = () => {
const foo = true
? <div id="id">test</div>
: false;

return <div
id="id"
>
test
</div>
ljharb marked this conversation as resolved.
Show resolved Hide resolved
}
`,
options: [
{
indentMode: 2,
ignoreTernaryOperator: true,
},
],
},
{
code: `
\t\t\t\tconst F = () => {
\t\t\t\t\tconst foo = true
\t\t\t\t\t\t? <div id="id">test</div>
\t\t\t\t\t\t: false;

\t\t\t\t\treturn <div
\t\t\t\t\t\tid="id"
\t\t\t\t\t>
\t\t\t\t\t\ttest
\t\t\t\t\t</div>
\t\t\t\t}
`,
options: [
{
indentMode: 'tab',
ignoreTernaryOperator: false,
},
],
},
{
code: `
\t\t\t\tconst F = () => {
\t\t\t\t\tconst foo = true
\t\t\t\t\t\t? <div id="id">test</div>
\t\t\t\t\t\t: false;

\t\t\t\t\treturn <div
\t\t\t\t\t\tid="id"
\t\t\t\t\t>
\t\t\t\t\t\ttest
\t\t\t\t\t</div>
\t\t\t\t}
`,
options: [
{
indentMode: 'tab',
ignoreTernaryOperator: true,
},
],
},
{
code: `
{this.props.ignoreTernaryOperatorTrue
Expand Down Expand Up @@ -607,5 +691,185 @@ ruleTester.run('jsx-indent-props', rule, {
},
],
},
{
code: `
const F = () => {
const foo = true
? <div id="id">test</div>
: false;

return <div
id="id"
>
test
</div>
}
`,
output: `
const F = () => {
const foo = true
? <div id="id">test</div>
: false;

return <div
id="id"
>
test
</div>
}
`,
options: [
{
indentMode: 2,
ignoreTernaryOperator: false,
},
],
errors: [
{
messageId: 'wrongIndent',
data: {
needed: 12,
type: 'space',
characters: 'characters',
gotten: 14,
},
},
],
},
{
code: `
const F = () => {
const foo = true
? <div id="id">test</div>
: false;

return <div
id="id"
>
test
</div>
}
`,
output: `
const F = () => {
const foo = true
? <div id="id">test</div>
: false;

return <div
id="id"
>
test
</div>
}
`,
options: [
{
indentMode: 2,
ignoreTernaryOperator: true,
},
],
errors: [
{
messageId: 'wrongIndent',
data: {
needed: 12,
type: 'space',
characters: 'characters',
gotten: 14,
},
},
],
},
{
code: `
\t\t\t\tconst F = () => {
\t\t\t\t\tconst foo = true
\t\t\t\t\t\t? <div id="id">test</div>
\t\t\t\t\t\t: false;

\t\t\t\t\treturn <div
\t\t\t\t\t\t\tid="id"
\t\t\t\t\t>
\t\t\t\t\t\ttest
\t\t\t\t\t</div>
\t\t\t\t}
`,
output: `
\t\t\t\tconst F = () => {
\t\t\t\t\tconst foo = true
\t\t\t\t\t\t? <div id="id">test</div>
\t\t\t\t\t\t: false;

\t\t\t\t\treturn <div
\t\t\t\t\t\tid="id"
\t\t\t\t\t>
\t\t\t\t\t\ttest
\t\t\t\t\t</div>
\t\t\t\t}
`,
options: [
{
indentMode: 'tab',
ignoreTernaryOperator: false,
},
],
errors: [
{
messageId: 'wrongIndent',
data: {
needed: 6,
type: 'tab',
characters: 'characters',
gotten: 7,
},
},
],
},
{
code: `
\t\t\t\tconst F = () => {
\t\t\t\t\tconst foo = true
\t\t\t\t\t\t? <div id="id">test</div>
\t\t\t\t\t\t: false;

\t\t\t\t\treturn <div
\t\t\t\t\t\t\tid="id"
\t\t\t\t\t>
\t\t\t\t\t\ttest
\t\t\t\t\t</div>
\t\t\t\t}
`,
output: `
\t\t\t\tconst F = () => {
\t\t\t\t\tconst foo = true
\t\t\t\t\t\t? <div id="id">test</div>
\t\t\t\t\t\t: false;

\t\t\t\t\treturn <div
\t\t\t\t\t\tid="id"
\t\t\t\t\t>
\t\t\t\t\t\ttest
\t\t\t\t\t</div>
\t\t\t\t}
`,
options: [
{
indentMode: 'tab',
ignoreTernaryOperator: true,
},
],
errors: [
{
messageId: 'wrongIndent',
data: {
needed: 6,
type: 'tab',
characters: 'characters',
gotten: 7,
},
},
],
},
]),
});