-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[new] [jsx-curly-brace-presence] Disallow unnecessary JSX expressions or enforce them #1349
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
[new] [jsx-curly-brace-presence] Disallow unnecessary JSX expressions or enforce them #1349
Conversation
}, | ||
additionalProperties: false | ||
} | ||
props: {enum: optionValues}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should probably still specify the default?
additionalProperties: false | ||
} | ||
props: {enum: optionValues}, | ||
children: {enum: optionValues} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also here?
@@ -108,7 +109,7 @@ module.exports = { | |||
// -------------------------------------------------------------------------- | |||
|
|||
return { | |||
JSXExpressionContainer: node => { | |||
JSXExpressionContainer: function(node) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any particular reason to change these from arrows?
@@ -119,7 +120,7 @@ module.exports = { | |||
} | |||
}, | |||
|
|||
Literal: node => { | |||
Literal: function(node) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also here?
03e5fcf
to
2d695c4
Compare
@@ -27,8 +27,8 @@ module.exports = { | |||
{ | |||
type: 'object', | |||
properties: { | |||
props: {enum: optionValues}, | |||
children: {enum: optionValues} | |||
props: {enum: optionValues, default: optionNever}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed the default by mistake. They are back now.
@@ -116,15 +116,15 @@ module.exports = { | |||
// -------------------------------------------------------------------------- | |||
|
|||
return { | |||
JSXExpressionContainer: function(node) { | |||
JSXExpressionContainer: node => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made the change to maintain consistencies across files, but I guess it doesn't matter anyway. I have changed it back to using the arrow function.
@ljharb |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be useful to also allow, in addition to the object, a single string of "always" or "never" that sets the defaults to that value.
Can you add tests that include spread props, as well as tests like
<div a={`foo`} />
<div a={`foo${bar}`} />
?
@ljharb Good suggestions! I will make the changes. |
@ljharb |
They are `always`, `always,single`, `always,double`, `always,orignal`, `never` and `ignore` for checking on JSX props and children. | ||
|
||
* `always`: always enforce curly braces inside JSX props or/and children and fix with double quotes inside the generated JSX expressions | ||
* `always,single`: always enforce curly braces and fix with single quotes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so, first - i think rather than squashing the quote style into the string value, this should be an object.
However, quote style is something that another rule should handle. This one should just pick a default - i'd go with double quotes - and let the jsx-quotes rule handle correcting it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this is looking much better.
|
||
```js | ||
... | ||
"react/forbid-elements": [<enabled>, { "props": <string>, "children": <string> }] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this might be the wrong rule name
|
||
```js | ||
... | ||
"react/forbid-elements": [<enabled>, <string>] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also here
It can fixed to: | ||
|
||
```jsx | ||
<App prop='foo'>Hello world</App>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's use multiple props in this example so we can show that it allows either single or double quotes (one of each)
Thanks for all your suggestions! They all make sense and give me an idea on how to better contribute to this repo for future PRs. Hopefully, everything looks good now! |
|
||
```jsx | ||
<App>Hello world</App>; | ||
<App prop='Hello world' attr="foo" />; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this example use double quotes consistently?
|
||
```jsx | ||
<App>{"Hello world"}</App>; | ||
<App prop={'Hello world'}>{'Hello world'}</App>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should both of these examples use the same quote style?
They can be fixed to: | ||
```jsx | ||
<App>{"Hello world"}</App>; | ||
<App prop={'Hello world'} attr={"foo"}>{"Hello world"}</App>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these should probably both use the same kind of quotes
It can fixed to: | ||
|
||
```jsx | ||
<App prop='foo' attr="bar">Hello world</App>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these should probably both use the same kind of quotes
|
||
## Edge cases | ||
|
||
The fix also deals with template literals, strings with quotes and strings with escapes characters. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use the Oxford comma - in this case, after "quotes"
will warned and fixed to: | ||
|
||
```jsx | ||
<App prop={'Hello "foo" world'}>{"Hello 'foo' \"bar\" world"}</App>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it expected that the quote style is unchanged for prop
?
Thanks again for all your suggestions. I was leaving original quotes in as much as I could previously when I was fixing the styles, but your suggestion makes sense for the purpose of consistency. I have changed the operations to fix with double quotes in all cases. Hope things look better now! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, this looks great!
@ljharb Thank you for the review! |
For issue #1310:
This rule checks for unnecessary JSX expressions when literals alone will do or enforce the presence of JSX expressions.
References:
String literals in JSX
JSX gotchas