fix(linter): detect missing displayName when TS interface shares name with createContext (#19607)#19639
Closed
cobyfrombrooklyn-bot wants to merge 1 commit intooxc-project:mainfrom
Closed
Conversation
… with createContext variable When a TypeScript interface/type alias has the same name as a variable assigned to createContext(), the display-name rule failed to detect the missing displayName. This happened because the interface gets the primary symbol declaration, and is_react_component_node returns None for interface nodes, falling through to context assignment reference checking which also misses the const initializer. The fix checks redeclarations when the primary declaration is not a component. If the symbol has a redeclaration that is a component (e.g. a VariableDeclarator with createContext()), it is detected correctly. Fixes oxc-project#19607
Contributor
|
duplicate of #19608 |
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
The
react/display-namerule withcheckContextObjects: truefails to detect a missingdisplayNamewhen a TypeScript interface or type alias shares the same name as acreateContextvariable.Root Cause
When
interface Fooandconst Foo = createContext()share the same name, oxc's semantic binder merges them into one symbol. The interface gets the primarysymbol_declaration(since it's declared first). Theis_react_component_nodefunction receives the interface's AST node, finds no matching arm (it handles VariableDeclarator, FunctionDeclaration, etc. but not TSInterfaceDeclaration), and returnsNone. The code then falls through tocheck_context_assignment_referenceswhich checks write references, but aconstinitializer isn't a write reference.Fix
When the primary declaration isn't a component, also check the symbol's redeclarations via
ctx.scoping().symbol_redeclarations(). If a redeclaration (e.g. the VariableDeclarator withcreateContext()) is a component, it's detected correctly.Tests
Added 3 test cases:
Updated snapshot.
cargo test -p oxc_linter --lib -- display_namepasses.Tested locally on macOS ARM (Apple Silicon).