Skip to content

Commit f73bdab

Browse files
committed
[Fix] jsx-curly-brace-presence: make unwrapping single-expression template literals configurable
Fixes #3607
1 parent 1a3a17a commit f73bdab

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

docs/rules/jsx-curly-brace-presence.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ You can pass in options to enforce the presence of curly braces on JSX props, ch
2020

2121
```js
2222
...
23-
"react/jsx-curly-brace-presence": [<enabled>, { "props": <string>, "children": <string>, "propElementValues": <string> }]
23+
"react/jsx-curly-brace-presence": [<enabled>, { "props": <string>, "children": <string>, "propElementValues": <string>, "unwrapTemplateLiterals": <boolean> }]
2424
...
2525
```
2626

@@ -47,7 +47,13 @@ If passed in the option to fix, this is how a style violation will get fixed
4747

4848
- All fixing operations use double quotes.
4949

50-
For examples:
50+
### `unwrapTemplateLiterals`
51+
52+
- `true`: unwrap template literals that only have a single expression inside of them.
53+
Since template literals return strings, this may cause changes in semantics, or type errors.
54+
- `false` (default): do not unwrap template literals that only have a single expression inside of them.
55+
56+
## Examples
5157

5258
Examples of **incorrect** code for this rule, when configured with `{ props: "always", children: "always" }`:
5359

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ const OPTION_VALUES = [
2525
OPTION_NEVER,
2626
OPTION_IGNORE,
2727
];
28-
const DEFAULT_CONFIG = { props: OPTION_NEVER, children: OPTION_NEVER, propElementValues: OPTION_IGNORE };
28+
const DEFAULT_CONFIG = {
29+
props: OPTION_NEVER,
30+
children: OPTION_NEVER,
31+
propElementValues: OPTION_IGNORE,
32+
unwrapTemplateLiterals: false,
33+
};
2934

3035
// ------------------------------------------------------------------------------
3136
// Rule Definition
@@ -57,6 +62,7 @@ module.exports = {
5762
props: { enum: OPTION_VALUES },
5863
children: { enum: OPTION_VALUES },
5964
propElementValues: { enum: OPTION_VALUES },
65+
unwrapTemplateLiterals: { type: 'boolean' },
6066
},
6167
additionalProperties: false,
6268
},
@@ -287,7 +293,9 @@ module.exports = {
287293
) {
288294
reportUnnecessaryCurly(JSXExpressionNode);
289295
} else if (
290-
isSingleExpressionTemplateLiteral(expression)) {
296+
isSingleExpressionTemplateLiteral(expression)
297+
&& userConfig.unwrapTemplateLiterals
298+
) {
291299
reportUnnecessaryCurly(JSXExpressionNode);
292300
} else if (jsxUtil.isJSX(expression)) {
293301
reportUnnecessaryCurly(JSXExpressionNode);

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -944,13 +944,17 @@ ruleTester.run('jsx-curly-brace-presence', rule, {
944944
code: '<App label={`${label}`} />',
945945
output: '<App label={label} />',
946946
errors: [{ messageId: 'unnecessaryCurly' }],
947-
options: [{ props: 'never', children: 'never', propElementValues: 'never' }],
947+
options: [{
948+
props: 'never', children: 'never', propElementValues: 'never', unwrapTemplateLiterals: true,
949+
}],
948950
},
949951
{
950952
code: '<App>{`${label}`}</App>',
951953
output: '<App>{label}</App>',
952954
errors: [{ messageId: 'unnecessaryCurly' }],
953-
options: [{ props: 'never', children: 'never', propElementValues: 'never' }],
955+
options: [{
956+
props: 'never', children: 'never', propElementValues: 'never', unwrapTemplateLiterals: true,
957+
}],
954958
}
955959
)),
956960
});

0 commit comments

Comments
 (0)