Skip to content

Commit 1436799

Browse files
doochikljharb
authored andcommitted
[fix] jsx-curly-brace-presence: allow trailing spaces in literal
`jsx-curly-brace-presence` with `never` reports invalid errors in following cases ``` <Foo>{' space before or after '}</Foo> ``` ``` <Foo> {'space after '} <a>link</a> {' space before'} </Foo> ```
1 parent e911882 commit 1436799

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ module.exports = {
174174
return node.type && node.type === 'Literal' && node.value && jsxUtil.isWhiteSpaces(node.value);
175175
}
176176

177+
function isLiteralWithTrailingWhiteSpaces(node) {
178+
return node.type && node.type === 'Literal' && node.value && /^\s|\s$/.test(node.value);
179+
}
180+
177181
// Bail out if there is any character that needs to be escaped in JSX
178182
// because escaping decreases readiblity and the original code may be more
179183
// readible anyway or intentional for other specific reasons
@@ -184,7 +188,10 @@ module.exports = {
184188
if (
185189
(expressionType === 'Literal' || expressionType === 'JSXText') &&
186190
typeof expression.value === 'string' &&
187-
!isWhiteSpaceLiteral(expression) &&
191+
(
192+
(JSXExpressionNode.parent.type === 'JSXAttribute' && !isWhiteSpaceLiteral(expression)) ||
193+
!isLiteralWithTrailingWhiteSpaces(expression)
194+
) &&
188195
!needToEscapeCharacterForJSX(expression.raw) && (
189196
jsxUtil.isJSX(JSXExpressionNode.parent) ||
190197
!containsQuoteCharacters(expression.value)

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,14 @@ ruleTester.run('jsx-curly-brace-presence', rule, {
269269
code: '<MyComponent>{"Hello \\n world"}</MyComponent>',
270270
options: ['never']
271271
},
272+
{
273+
code: '<MyComponent>{"space after "}</MyComponent>',
274+
options: ['never']
275+
},
276+
{
277+
code: '<MyComponent>{" space before"}</MyComponent>',
278+
options: ['never']
279+
},
272280
{
273281
code: ['<a a={"start\\', '\\', 'end"}/>'].join('/n'),
274282
options: ['never']
@@ -320,6 +328,17 @@ ruleTester.run('jsx-curly-brace-presence', rule, {
320328
parser: parsers.BABEL_ESLINT,
321329
options: [{children: 'never'}]
322330
},
331+
{
332+
code: `
333+
<MyComponent>
334+
{ 'space after ' }
335+
<b>foo</b>
336+
{ ' space before' }
337+
</MyComponent>
338+
`,
339+
parser: parsers.BABEL_ESLINT,
340+
options: [{children: 'never'}]
341+
},
323342
{
324343
code: `
325344
<MyComponent>

0 commit comments

Comments
 (0)