-
-
Notifications
You must be signed in to change notification settings - Fork 15
fix: correct parent typing for rule visitors #172
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
base: main
Are you sure you want to change the base?
Conversation
src/types.ts
Outdated
| WithExit<{ | ||
| [Node in AnyNode as Node["type"]]?: | ||
| | ((node: Node) => void) | ||
| | undefined; | ||
| }> {} |
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 think it would be better to keep parent as a second parameter and adjust the ESLint logic where visitors are invoked. At the moment, visitors are always called with a single argument (the target node). See the relevant lines in the ESLint source:
- https://github.com/eslint/eslint/blob/v9.38.0/lib/linter/source-code-traverser.js#L291
- https://github.com/eslint/eslint/blob/v9.38.0/lib/linter/source-code-traverser.js#L303
But for non-JS languages the parent node is stored in step.args as a second element following the target node. So probably the invocation should look like this:
visitor.callSync(selector, ...(step.args ?? [step.target]));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.
Thanks for the clarification! I went ahead and opened a PR for this: eslint/eslint#20253
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.
For reference, passing parent as a second parameter to visitors is mentioned in the language plugin design, see https://github.com/eslint/rfcs/tree/main/designs/2022-languages#the-sourcecode-object.
JSONRuleVisitor| "String:exit": (...args) => testVisitor<StringNode>(...args), | ||
|
|
||
| // Unknown selectors allowed | ||
| "Identifier[name=foo]"(node: IdentifierNode, parent: MemberNode) {}, |
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.
Same as in the other PR, please add additional test cases for this.
| //------------------------------------------------------------------------------ | ||
|
|
||
| /** Adds matching `:exit` selectors for all properties of a `RuleVisitor`. */ | ||
| type WithExit<RuleVisitorType extends RuleVisitor> = { |
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 think this should be a shared helper from @eslint/core as all ESLint languages require it.
Prerequisites checklist
What is the purpose of this pull request?
To correct the
JSONRuleVisitortype definitions and improve maintainability by introducing aWithExithelper (copied from eslint/markdown). This ensures:exitselectors are automatically generated for all node types and simplifies visitor type definitions.What changes did you make? (Give an overview)
WithExit<RuleVisitorType>helper type to automatically add:exitvariants.JSONRuleVisitorinterface with a version usingWithExit.Note onparentparameter:The optionalparentparameters were removed from all visitor methods. At runtime, these parameters always appear to beundefined. Currently, only this plugin and theeslint/markdownplugin useparentparameters in their visitors — theeslint/cssplugin does not.I’m not entirely certain if removing them is safe for all current or future use cases, so I would like to hear opinions from @fasttime on this change.Related Issues
Is there anything you'd like reviewers to focus on?