Skip to content

Commit

Permalink
[Fix] jsx-curly-brace-presence: allow trailing spaces in TemplateLi…
Browse files Browse the repository at this point in the history
…teral
  • Loading branch information
doochik authored and ljharb committed Nov 29, 2019
1 parent 027ebd9 commit 221164a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/rules/jsx-curly-brace-presence.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,12 @@ module.exports = {
return node.type && node.type === 'Literal' && node.value && jsxUtil.isWhiteSpaces(node.value);
}

function isStringWithTrailingWhiteSpaces(value) {
return /^\s|\s$/.test(value);
}

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

// Bail out if there is any character that needs to be escaped in JSX
Expand All @@ -202,6 +206,7 @@ module.exports = {
expressionType === 'TemplateLiteral' &&
expression.expressions.length === 0 &&
expression.quasis[0].value.raw.indexOf('\n') === -1 &&
!isStringWithTrailingWhiteSpaces(expression.quasis[0].value.raw) &&
!needToEscapeCharacterForJSX(expression.quasis[0].value.raw) && (
jsxUtil.isJSX(JSXExpressionNode.parent) ||
!containsQuoteCharacters(expression.quasis[0].value.cooked)
Expand Down
19 changes: 19 additions & 0 deletions tests/lib/rules/jsx-curly-brace-presence.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,14 @@ ruleTester.run('jsx-curly-brace-presence', rule, {
code: '<MyComponent>{" space before"}</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 @@ -342,6 +350,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

0 comments on commit 221164a

Please sign in to comment.