Skip to content

Commit f0ff971

Browse files
jzabalaljharb
authored andcommitted
[Fix] jsx-curly-brace-presence: disable disallowed JSX text chars check in props
Fixes #2694
1 parent 9630957 commit f0ff971

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

lib/rules/jsx-curly-brace-presence.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ module.exports = {
100100
return rawStringValue.replace(/\\/g, '\\\\');
101101
}
102102

103-
function needToEscapeCharacterForJSX(raw) {
103+
function needToEscapeCharacterForJSX(raw, node) {
104104
return (
105105
containsBackslash(raw)
106106
|| containsHTMLEntity(raw)
107-
|| containsDisallowedJSXTextChars(raw)
107+
|| (node.parent.type !== 'JSXAttribute' && containsDisallowedJSXTextChars(raw))
108108
);
109109
}
110110

@@ -238,7 +238,7 @@ module.exports = {
238238
(JSXExpressionNode.parent.type === 'JSXAttribute' && !isWhiteSpaceLiteral(expression))
239239
|| !isLiteralWithTrailingWhiteSpaces(expression)
240240
)
241-
&& !needToEscapeCharacterForJSX(expression.raw) && (
241+
&& !needToEscapeCharacterForJSX(expression.raw, JSXExpressionNode) && (
242242
jsxUtil.isJSX(JSXExpressionNode.parent)
243243
|| !containsQuoteCharacters(expression.value)
244244
)
@@ -249,7 +249,7 @@ module.exports = {
249249
&& expression.expressions.length === 0
250250
&& expression.quasis[0].value.raw.indexOf('\n') === -1
251251
&& !isStringWithTrailingWhiteSpaces(expression.quasis[0].value.raw)
252-
&& !needToEscapeCharacterForJSX(expression.quasis[0].value.raw) && (
252+
&& !needToEscapeCharacterForJSX(expression.quasis[0].value.raw, JSXExpressionNode) && (
253253
jsxUtil.isJSX(JSXExpressionNode.parent)
254254
|| !containsQuoteCharacters(expression.quasis[0].value.cooked)
255255
)

tests/lib/rules/jsx-curly-brace-presence.js

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -244,14 +244,6 @@ ruleTester.run('jsx-curly-brace-presence', rule, {
244244
code: '<MyComponent>{"<Foo />"}</MyComponent>',
245245
options: ['never']
246246
},
247-
{
248-
code: '<MyComponent prop={"{ style: true }"}>bar</MyComponent>',
249-
options: ['never']
250-
},
251-
{
252-
code: '<MyComponent prop={"< style: true >"}>foo</MyComponent>',
253-
options: ['never']
254-
},
255247
{
256248
code: '<MyComponent prop={"Hello \\u1026 world"}>bar</MyComponent>',
257249
options: ['never']
@@ -800,6 +792,40 @@ ruleTester.run('jsx-curly-brace-presence', rule, {
800792
{message: missingCurlyMessage}
801793
],
802794
options: [{children: 'always'}]
795+
},
796+
{
797+
code: `
798+
<Box mb={'1rem'} />
799+
`,
800+
output: `
801+
<Box mb="1rem" />
802+
`,
803+
errors: [
804+
{message: unnecessaryCurlyMessage}
805+
],
806+
options: [{props: 'never'}]
807+
},
808+
{
809+
code: `
810+
<Box mb={'1rem {}'} />
811+
`,
812+
output: `
813+
<Box mb="1rem {}" />
814+
`,
815+
errors: [{message: unnecessaryCurlyMessage}],
816+
options: ['never']
817+
},
818+
{
819+
code: '<MyComponent prop={"{ style: true }"}>bar</MyComponent>',
820+
output: '<MyComponent prop="{ style: true }">bar</MyComponent>',
821+
errors: [{message: unnecessaryCurlyMessage}],
822+
options: ['never']
823+
},
824+
{
825+
code: '<MyComponent prop={"< style: true >"}>foo</MyComponent>',
826+
output: '<MyComponent prop="< style: true >">foo</MyComponent>',
827+
errors: [{message: unnecessaryCurlyMessage}],
828+
options: ['never']
803829
}
804830
]
805831
});

0 commit comments

Comments
 (0)