Skip to content

Commit

Permalink
[Fix] jsx-indent-props: Reset line.isUsingOperator correctly afte…
Browse files Browse the repository at this point in the history
…r ternary (#3146)
  • Loading branch information
Tobias Waltl committed Dec 21, 2021
1 parent d56fdb8 commit 5757b4a
Show file tree
Hide file tree
Showing 2 changed files with 265 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rules/jsx-indent-props.js
Original file line number Diff line number Diff line change
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);
const useBracket = /[<]/.test(src);

line.currentOperator = false;
if (useOperator) {
Expand Down
264 changes: 264 additions & 0 deletions tests/lib/rules/jsx-indent-props.js
Original file line number Diff line number Diff line change
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>
}
`,
options: [
{
indentMode: 2,
ignoreTernaryOperator: false,
},
],
},
{
code: `
const F = () => {
const foo = true
? <div id="id">test</div>
: false;
return <div
id="id"
>
test
</div>
}
`,
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,
},
},
],
},
]),
});

0 comments on commit 5757b4a

Please sign in to comment.