Repository navigation
ESLint plugin: Fix no-unused-vars-before-return false positive for JSX in nested functions - #80666
dhruvikpatel18 wants to merge 1 commit into
Conversation
…in local functions
|
Warning: Type of PR label mismatch To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.
Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task. |
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Thank you for this fix and the tests, @dhruvikpatel18! Your PR helped pinpoint the root cause: a scope-analysis bug in |
What?
Closes #55552
Fixes a false positive in the
@wordpress/no-unused-vars-before-returnESLint rule, which reported a variable as unused when its only reference was JSX nested inside a local function.Why?
The code from the issue is flagged incorrectly:
Loadis used, as a JSX component type insideLoadEdit, but the rule reports "Variables should not be assigned until just prior its first reference."Two notes on scope, since they affect how you'll want to verify this:
trunkas-is. Scope analysis ineslint-scopegained native JSX reference resolution in 8.3.0 (shipped with ESLint ~9.22), so on the ESLint version Gutenberg currently uses the rule's JSX workaround is bypassed entirely andLoadresolves as a genuine reference.@wordpress/eslint-plugindeclareseslint: ^9.0.0 || ^10.0.0as a peer dependency, and ESLint 9.0–9.21 bundleeslint-scope< 8.3, where the workaround is the only thing running and the false positive is real.So this fixes the rule for the ESLint versions we still support, and removes a latent correctness bug from the workaround regardless.
Testing Instructions
This is a lint-rule change with no runtime or UI impact.
The two new
validcases cover the reported code and the arrow-function variant.Because current ESLint resolves JSX references natively (see "Why?" above), those tests pass with or without the rule change on
trunk. To exercise the code path this PR actually fixes, the rule has to be run against a scope manager that does not resolve JSX identifiers — i.e. the same analyzer with thejsxscope option off, which is what ESLint 9.0–9.21 do. Under that setup:function)The last row matters most: the fix must not silence genuine findings, and it doesn't.
The equivalent real-world check is to install ESLint 9.21 or earlier alongside the plugin and lint the snippet from the issue.
Use of AI Tools
This PR was developed with the assistance of Claude Opus 4.8, used to diagnose the root cause, identify the
eslint-scopeversion boundary where JSX reference resolution was introduced, implement the fix, and write the regression tests.