Skip to content

ESLint plugin: Fix no-unused-vars-before-return false positive for JSX in nested functions - #80666

Closed
dhruvikpatel18 wants to merge 1 commit into
WordPress:trunkfrom
dhruvikpatel18:fix/eslint-no-unused-vars-before-returns
Closed

dhruvikpatel18 wants to merge 1 commit into
WordPress:trunkfrom
dhruvikpatel18:fix/eslint-no-unused-vars-before-returns

Conversation

@dhruvikpatel18

Copy link
Copy Markdown
Member

What?

Closes #55552

Fixes a false positive in the @wordpress/no-unused-vars-before-return ESLint 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:

function lazyEdit( load ) {
	const Load = lazy( load );
	return function LoadEdit( props ) {
		return (
			<Suspense fallback={ null }>
				<Load { ...props } />
			</Suspense>
		);
	};
}

Load is used, as a JSX component type inside LoadEdit, 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:

  1. The issue does not reproduce on trunk as-is. Scope analysis in eslint-scope gained 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 and Load resolves as a genuine reference.
  2. The bug is still live for plugin consumers. @wordpress/eslint-plugin declares eslint: ^9.0.0 || ^10.0.0 as a peer dependency, and ESLint 9.0–9.21 bundle eslint-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.

npm run test:unit packages/eslint-plugin/rules/__tests__/no-unused-vars-before-return.js

The two new valid cases 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 the jsx scope option off, which is what ESLint 9.0–9.21 do. Under that setup:

Case Before After
Issue #55552 (JSX in nested function) ❌ false positive ✅ clean
JSX in nested arrow function ❌ false positive ✅ clean
JSX in the same scope ✅ clean ✅ clean
Declared before an early return (true positive) ✅ reported ✅ reported

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-scope version boundary where JSX reference resolution was introduced, implement the fix, and write the regression tests.

@dhruvikpatel18
dhruvikpatel18 marked this pull request as ready for review July 24, 2026 06:33
@github-actions

Copy link
Copy Markdown

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.

  • Required label: Any label starting with [Type].
  • Labels found: .

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.

@github-actions

Copy link
Copy Markdown

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: dhruvikpatel18 <dhruvik18@git.wordpress.org>
Co-authored-by: jsnajdr <jsnajdr@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@youknowriad

Copy link
Copy Markdown
Contributor

Thank you for this fix and the tests, @dhruvikpatel18! Your PR helped pinpoint the root cause: a scope-analysis bug in @babel/eslint-parser. #82144 fixed it by dropping that parser from the config, which closed #55552. Closing this as superseded. Feel free to open a new issue if the false positive still shows up for you.

@youknowriad youknowriad closed this Oct 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The no-unused-vars-before-return ESLint rule reports false positive on JSX usage

2 participants