From 5757b4a1a4e4c1495f669796270e76a0a0f82b57 Mon Sep 17 00:00:00 2001 From: Tobias Waltl Date: Tue, 14 Dec 2021 09:51:53 +0100 Subject: [PATCH] [Fix] `jsx-indent-props`: Reset `line.isUsingOperator` correctly after ternary (#3146) --- lib/rules/jsx-indent-props.js | 2 +- tests/lib/rules/jsx-indent-props.js | 264 ++++++++++++++++++++++++++++ 2 files changed, 265 insertions(+), 1 deletion(-) diff --git a/lib/rules/jsx-indent-props.js b/lib/rules/jsx-indent-props.js index 8935616988..5d33ded173 100644 --- a/lib/rules/jsx-indent-props.js +++ b/lib/rules/jsx-indent-props.js @@ -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) { diff --git a/tests/lib/rules/jsx-indent-props.js b/tests/lib/rules/jsx-indent-props.js index bfa5f20d9d..adf6fd1387 100644 --- a/tests/lib/rules/jsx-indent-props.js +++ b/tests/lib/rules/jsx-indent-props.js @@ -195,6 +195,90 @@ ruleTester.run('jsx-indent-props', rule, { }, ], }, + { + code: ` + const F = () => { + const foo = true + ?
test
+ : false; + + return
+ test +
+ } + `, + options: [ + { + indentMode: 2, + ignoreTernaryOperator: false, + }, + ], + }, + { + code: ` + const F = () => { + const foo = true + ?
test
+ : false; + + return
+ test +
+ } + `, + options: [ + { + indentMode: 2, + ignoreTernaryOperator: true, + }, + ], + }, + { + code: ` +\t\t\t\tconst F = () => { +\t\t\t\t\tconst foo = true +\t\t\t\t\t\t?
test
+\t\t\t\t\t\t: false; + +\t\t\t\t\treturn
+\t\t\t\t\t\ttest +\t\t\t\t\t
+\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?
test
+\t\t\t\t\t\t: false; + +\t\t\t\t\treturn
+\t\t\t\t\t\ttest +\t\t\t\t\t
+\t\t\t\t} +`, + options: [ + { + indentMode: 'tab', + ignoreTernaryOperator: true, + }, + ], + }, { code: ` {this.props.ignoreTernaryOperatorTrue @@ -607,5 +691,185 @@ ruleTester.run('jsx-indent-props', rule, { }, ], }, + { + code: ` + const F = () => { + const foo = true + ?
test
+ : false; + + return
+ test +
+ } + `, + output: ` + const F = () => { + const foo = true + ?
test
+ : false; + + return
+ test +
+ } + `, + options: [ + { + indentMode: 2, + ignoreTernaryOperator: false, + }, + ], + errors: [ + { + messageId: 'wrongIndent', + data: { + needed: 12, + type: 'space', + characters: 'characters', + gotten: 14, + }, + }, + ], + }, + { + code: ` + const F = () => { + const foo = true + ?
test
+ : false; + + return
+ test +
+ } + `, + output: ` + const F = () => { + const foo = true + ?
test
+ : false; + + return
+ test +
+ } + `, + 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?
test
+\t\t\t\t\t\t: false; + +\t\t\t\t\treturn
+\t\t\t\t\t\ttest +\t\t\t\t\t
+\t\t\t\t} +`, + output: ` +\t\t\t\tconst F = () => { +\t\t\t\t\tconst foo = true +\t\t\t\t\t\t?
test
+\t\t\t\t\t\t: false; + +\t\t\t\t\treturn
+\t\t\t\t\t\ttest +\t\t\t\t\t
+\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?
test
+\t\t\t\t\t\t: false; + +\t\t\t\t\treturn
+\t\t\t\t\t\ttest +\t\t\t\t\t
+\t\t\t\t} +`, + output: ` +\t\t\t\tconst F = () => { +\t\t\t\t\tconst foo = true +\t\t\t\t\t\t?
test
+\t\t\t\t\t\t: false; + +\t\t\t\t\treturn
+\t\t\t\t\t\ttest +\t\t\t\t\t
+\t\t\t\t} +`, + options: [ + { + indentMode: 'tab', + ignoreTernaryOperator: true, + }, + ], + errors: [ + { + messageId: 'wrongIndent', + data: { + needed: 6, + type: 'tab', + characters: 'characters', + gotten: 7, + }, + }, + ], + }, ]), });