Fix jsx-no-leaked-render autofix for expression alternates#4017
Open
morgan-coded wants to merge 1 commit into
Open
Fix jsx-no-leaked-render autofix for expression alternates#4017morgan-coded wants to merge 1 commit into
morgan-coded wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds safer handling for ternary alternates under the coerce strategy in jsx-no-leaked-render, ensuring alternates like undefined/void 0 are caught while preserving side effects and valid ternary branches during autofix.
Changes:
- Expand ternary alternate detection to treat
undefinedidentifiers andvoid 0as invalid alternates. - Improve
coercefixer output by parenthesizing alternates that need precedence protection. - Add regression tests covering nested ternaries,
void sideEffect(), andundefined/void 0alternates.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| tests/lib/rules/jsx-no-leaked-render.js | Adds regression tests for ternary alternates and coerce-strategy autofix behavior. |
| lib/rules/jsx-no-leaked-render.js | Updates detection logic for invalid ternary alternates and refines fixer output formatting/parenthesization. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+32
to
+40
| function isInvalidTernaryAlternate(node) { | ||
| if (node.type === 'Identifier') { | ||
| return node.name === 'undefined'; | ||
| } | ||
| if (node.type === 'UnaryExpression') { | ||
| return node.operator === 'void' && node.argument.type === 'Literal'; | ||
| } | ||
| return node.type === 'Literal' && TERNARY_INVALID_ALTERNATE_VALUES.indexOf(node.value) > -1; | ||
| } |
| if (node.type === 'UnaryExpression') { | ||
| return node.operator === 'void' && node.argument.type === 'Literal'; | ||
| } | ||
| return node.type === 'Literal' && TERNARY_INVALID_ALTERNATE_VALUES.indexOf(node.value) > -1; |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #4017 +/- ##
==========================================
- Coverage 97.71% 97.59% -0.13%
==========================================
Files 137 137
Lines 10188 10208 +20
Branches 3797 3808 +11
==========================================
+ Hits 9955 9962 +7
- Misses 233 246 +13 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
261952c to
a0dc6b9
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem I saw:
jsx-no-leaked-rendercould produce a destructive autofix for JSX attribute ternaries whenvalidStrategies: ["coerce"]was used.The old alternate check read
.valuefrom expression nodes that do not have one, so JSX, call, or nested conditional alternates could fall through asundefinedand get removed from the fix.What I changed:
I replaced that broad
.valuecheck with explicit checks for the alternates that should be treated as leaked values.The rule still reports
: null,: false,: undefined, and: void 0in coerce-only mode, and it keeps the existingcondition ? false : valueconversion. The autofix now preserves expression alternates such as JSX, calls, nested conditionals, and logical||expressions.How I checked it:
I confirmed the targeted rule test failed before the fix with 186 passing and 3 failing.
After the fix, the focused
jsx-no-leaked-rendersuite passed with 216 tests. The full unit suite passed with 17,899 tests, changed-file ESLint passed, andgit diff --checkwas clean.Closes #3927