Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] jsx-curly-brace-presence: allow trailing spaces in literal #2448

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/rules/jsx-curly-brace-presence.js
Expand Up @@ -174,6 +174,10 @@ module.exports = {
return node.type && node.type === 'Literal' && node.value && jsxUtil.isWhiteSpaces(node.value);
}

function isLiteralWithTrailingWhiteSpaces(node) {
return node.type && node.type === 'Literal' && node.value && /^\s|\s$/.test(node.value);
}

// Bail out if there is any character that needs to be escaped in JSX
// because escaping decreases readiblity and the original code may be more
// readible anyway or intentional for other specific reasons
Expand All @@ -184,7 +188,10 @@ module.exports = {
if (
(expressionType === 'Literal' || expressionType === 'JSXText') &&
typeof expression.value === 'string' &&
!isWhiteSpaceLiteral(expression) &&
(
(JSXExpressionNode.parent.type === 'JSXAttribute' && !isWhiteSpaceLiteral(expression)) ||
!isLiteralWithTrailingWhiteSpaces(expression)
) &&
!needToEscapeCharacterForJSX(expression.raw) && (
jsxUtil.isJSX(JSXExpressionNode.parent) ||
!containsQuoteCharacters(expression.value)
Expand Down
19 changes: 19 additions & 0 deletions tests/lib/rules/jsx-curly-brace-presence.js
Expand Up @@ -269,6 +269,14 @@ ruleTester.run('jsx-curly-brace-presence', rule, {
code: '<MyComponent>{"Hello \\n world"}</MyComponent>',
options: ['never']
},
{
code: '<MyComponent>{"space after "}</MyComponent>',
options: ['never']
},
{
code: '<MyComponent>{" space before"}</MyComponent>',
options: ['never']
},
{
code: ['<a a={"start\\', '\\', 'end"}/>'].join('/n'),
options: ['never']
Expand Down Expand Up @@ -320,6 +328,17 @@ ruleTester.run('jsx-curly-brace-presence', rule, {
parser: parsers.BABEL_ESLINT,
options: [{children: 'never'}]
},
{
code: `
<MyComponent>
{ 'space after ' }
<b>foo</b>
{ ' space before' }
</MyComponent>
`,
parser: parsers.BABEL_ESLINT,
options: [{children: 'never'}]
},
{
code: `
<MyComponent>
Expand Down