Skip to content

Commit

Permalink
[Fix] jsx-curly-brace-presence: disable disallowed JSX text chars c…
Browse files Browse the repository at this point in the history
…heck in props

Fixes #2694
  • Loading branch information
jzabala authored and ljharb committed Jul 12, 2020
1 parent 9630957 commit f0ff971
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
8 changes: 4 additions & 4 deletions lib/rules/jsx-curly-brace-presence.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ module.exports = {
return rawStringValue.replace(/\\/g, '\\\\');
}

function needToEscapeCharacterForJSX(raw) {
function needToEscapeCharacterForJSX(raw, node) {
return (
containsBackslash(raw)
|| containsHTMLEntity(raw)
|| containsDisallowedJSXTextChars(raw)
|| (node.parent.type !== 'JSXAttribute' && containsDisallowedJSXTextChars(raw))
);
}

Expand Down Expand Up @@ -238,7 +238,7 @@ module.exports = {
(JSXExpressionNode.parent.type === 'JSXAttribute' && !isWhiteSpaceLiteral(expression))
|| !isLiteralWithTrailingWhiteSpaces(expression)
)
&& !needToEscapeCharacterForJSX(expression.raw) && (
&& !needToEscapeCharacterForJSX(expression.raw, JSXExpressionNode) && (
jsxUtil.isJSX(JSXExpressionNode.parent)
|| !containsQuoteCharacters(expression.value)
)
Expand All @@ -249,7 +249,7 @@ module.exports = {
&& expression.expressions.length === 0
&& expression.quasis[0].value.raw.indexOf('\n') === -1
&& !isStringWithTrailingWhiteSpaces(expression.quasis[0].value.raw)
&& !needToEscapeCharacterForJSX(expression.quasis[0].value.raw) && (
&& !needToEscapeCharacterForJSX(expression.quasis[0].value.raw, JSXExpressionNode) && (
jsxUtil.isJSX(JSXExpressionNode.parent)
|| !containsQuoteCharacters(expression.quasis[0].value.cooked)
)
Expand Down
42 changes: 34 additions & 8 deletions tests/lib/rules/jsx-curly-brace-presence.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,14 +244,6 @@ ruleTester.run('jsx-curly-brace-presence', rule, {
code: '<MyComponent>{"<Foo />"}</MyComponent>',
options: ['never']
},
{
code: '<MyComponent prop={"{ style: true }"}>bar</MyComponent>',
options: ['never']
},
{
code: '<MyComponent prop={"< style: true >"}>foo</MyComponent>',
options: ['never']
},
{
code: '<MyComponent prop={"Hello \\u1026 world"}>bar</MyComponent>',
options: ['never']
Expand Down Expand Up @@ -800,6 +792,40 @@ ruleTester.run('jsx-curly-brace-presence', rule, {
{message: missingCurlyMessage}
],
options: [{children: 'always'}]
},
{
code: `
<Box mb={'1rem'} />
`,
output: `
<Box mb="1rem" />
`,
errors: [
{message: unnecessaryCurlyMessage}
],
options: [{props: 'never'}]
},
{
code: `
<Box mb={'1rem {}'} />
`,
output: `
<Box mb="1rem {}" />
`,
errors: [{message: unnecessaryCurlyMessage}],
options: ['never']
},
{
code: '<MyComponent prop={"{ style: true }"}>bar</MyComponent>',
output: '<MyComponent prop="{ style: true }">bar</MyComponent>',
errors: [{message: unnecessaryCurlyMessage}],
options: ['never']
},
{
code: '<MyComponent prop={"< style: true >"}>foo</MyComponent>',
output: '<MyComponent prop="< style: true >">foo</MyComponent>',
errors: [{message: unnecessaryCurlyMessage}],
options: ['never']
}
]
});

0 comments on commit f0ff971

Please sign in to comment.