-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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 Rule] jsx-embed-condition #2888
Conversation
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'm still not convinced this is a rule worth adding. If it were added, it'd need autofix to avoid being too disruptive.
It is a convention recommended by The official React docs. A whole bunch of people have been stung by this bug, many didn't even know what caused it, see:
(and you'll find many more examples by searching for "Text strings must be rendered within a Text component" on Google) Apart from crashing React Native hard, the bug is known to add random
This Lint rule is opt-in only. I can commit to adding an autofix for it later next month. My work schedule is pretty tight before that.
Doing so fixed the check on tests for latest Node.js runners, but broke them on the rest. I looked around the repository and couldn't find any tests with |
You can pass |
Attempted in 8abb5dc No dice. Any other ideas? |
8abb5dc
to
dcfa20b
Compare
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've rebased and hopefully fixed the tests. I'm not sold on the name of the rule - "embeds" isn't a term I've ever heard before to describe jsx curly-brace expressions.
Specifically, this is a rule concerned with preventing accidental rendering of a zero. This can happen only in the "children" position in jsx, or in a React.createElement
call.
So:
- let's add support for
React.createElement
, both in thechildren
position and in a literalchildren
prop passed to that function - let's rename the rule. perhaps, since it's no longer just jsx,
no-conditional-children
?
230a228
to
ee232bb
Compare
Please mark the PR as ready for review once the next round of changes is done. Thanks! |
We have a similar rule in an internal codebase called Since this plugin's purview is a bit broader, perhaps a name like |
@steelbrain Here's the internal rule we use, which includes a simple fixer. module.exports = {
meta: {
docs: {
url: 'SECRET URL'
},
fixable: 'code'
},
create(context) {
const sourceCode = context.getSourceCode();
return {
JSXExpressionContainer(node) {
if (
node.expression.type === 'LogicalExpression' &&
node.expression.operator === '&&' &&
node.parent.type !== 'JSXAttribute'
) {
context.report({
node,
message: 'JSX expression should not use logical-and.',
fix: (fixer) => fixer.replaceText(
node,
`{${sourceCode.getText(node.expression.left)} ? ${sourceCode.getText(node.expression.right)} : null}`
)
});
}
}
};
}
}; |
I do not have the bandwidth to work on it this week, I'll continue this work on the weekend. Thank you for your continued support! |
We've also been bit by this bug and would like to see this rule enforced in our code base |
Are these changes still active? Would love to have this rule |
@steelbrain are you still planning to finish this? |
@ljharb Happy to open up a new PR with the lint rule + fix + tests if ya'll are interested in this. I was gonna release it as a separate package otherwise. |
I do not have the bandwidth in the near future and this needs more love than I can give right now :) |
Let's please keep this open so it can be reused. |
@holic please post a branch link here, and i'll update the PR |
Added a PR here: #3318 This ports over the code we used internally at my last company that I was intending to release as its own package. Let me know if you'd prefer I build off of this PR instead. |
@holic i thought i was pretty clear i did not want a second PR opened; that pollutes the repo forever. Now I have to keep both PRs open and in sync until both are landed together. |
59af733
to
865ed16
Compare
069314a
to
181c68f
Compare
Closed by #3203 |
This rule disallows use of
&&
inside JSX Embeds to avoid conditional numbers from being rendered.Fixes #1979