Skip to content

Commit 6cf535a

Browse files
committed
jsx-curly-brace-presence: support shorthand fragments
1 parent e248365 commit 6cf535a

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
'use strict';
77

88
const docsUrl = require('../util/docsUrl');
9+
const jsxUtil = require('../util/jsx');
910

1011
// ------------------------------------------------------------------------------
1112
// Constants
@@ -168,13 +169,12 @@ module.exports = {
168169
function lintUnnecessaryCurly(JSXExpressionNode) {
169170
const expression = JSXExpressionNode.expression;
170171
const expressionType = expression.type;
171-
const parentType = JSXExpressionNode.parent.type;
172172

173173
if (
174174
(expressionType === 'Literal' || expressionType === 'JSXText') &&
175175
typeof expression.value === 'string' &&
176176
!needToEscapeCharacterForJSX(expression.raw) && (
177-
parentType === 'JSXElement' ||
177+
jsxUtil.isJSX(JSXExpressionNode.parent) ||
178178
!containsQuoteCharacters(expression.value)
179179
)
180180
) {
@@ -183,32 +183,30 @@ module.exports = {
183183
expressionType === 'TemplateLiteral' &&
184184
expression.expressions.length === 0 &&
185185
!needToEscapeCharacterForJSX(expression.quasis[0].value.raw) && (
186-
parentType === 'JSXElement' ||
186+
jsxUtil.isJSX(JSXExpressionNode.parent) ||
187187
!containsQuoteCharacters(expression.quasis[0].value.cooked)
188188
)
189189
) {
190190
reportUnnecessaryCurly(JSXExpressionNode);
191191
}
192192
}
193193

194-
function areRuleConditionsSatisfied(parentType, config, ruleCondition) {
194+
function areRuleConditionsSatisfied(parent, config, ruleCondition) {
195195
return (
196-
parentType === 'JSXAttribute' &&
196+
parent.type === 'JSXAttribute' &&
197197
typeof config.props === 'string' &&
198198
config.props === ruleCondition
199199
) || (
200-
parentType === 'JSXElement' &&
200+
jsxUtil.isJSX(parent) &&
201201
typeof config.children === 'string' &&
202202
config.children === ruleCondition
203203
);
204204
}
205205

206206
function shouldCheckForUnnecessaryCurly(parent, config) {
207-
const parentType = parent.type;
208-
209207
// If there are more than one JSX child, there is no need to check for
210208
// unnecessary curly braces.
211-
if (parentType === 'JSXElement' && parent.children.length !== 1) {
209+
if (jsxUtil.isJSX(parent) && parent.children.length !== 1) {
212210
return false;
213211
}
214212

@@ -220,7 +218,7 @@ module.exports = {
220218
return false;
221219
}
222220

223-
return areRuleConditionsSatisfied(parentType, config, OPTION_NEVER);
221+
return areRuleConditionsSatisfied(parent, config, OPTION_NEVER);
224222
}
225223

226224
function shouldCheckForMissingCurly(parent, config) {
@@ -232,7 +230,7 @@ module.exports = {
232230
return false;
233231
}
234232

235-
return areRuleConditionsSatisfied(parent.type, config, OPTION_ALWAYS);
233+
return areRuleConditionsSatisfied(parent, config, OPTION_ALWAYS);
236234
}
237235

238236
// --------------------------------------------------------------------------

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ ruleTester.run('jsx-curly-brace-presence', rule, {
3232
{
3333
code: '<App {...props}>foo</App>'
3434
},
35+
{
36+
code: '<>foo</>',
37+
parser: 'babel-eslint'
38+
},
3539
{
3640
code: '<App {...props}>foo</App>',
3741
options: [{props: 'never'}]
@@ -259,6 +263,13 @@ ruleTester.run('jsx-curly-brace-presence', rule, {
259263
options: [{children: 'never'}],
260264
errors: [{message: unnecessaryCurlyMessage}]
261265
},
266+
{
267+
code: '<>{`foo`}</>',
268+
output: '<>foo</>',
269+
parser: 'babel-eslint',
270+
options: [{children: 'never'}],
271+
errors: [{message: unnecessaryCurlyMessage}]
272+
},
262273
{
263274
code: `<MyComponent>{'foo'}</MyComponent>`,
264275
output: '<MyComponent>foo</MyComponent>',

0 commit comments

Comments
 (0)