Skip to content

Conversation

@jaymarvelz
Copy link

@jaymarvelz jaymarvelz commented Oct 25, 2025

Prerequisites checklist

What is the purpose of this pull request?

To correct the JSONRuleVisitor type definitions and improve maintainability by introducing a WithExit helper (copied from eslint/markdown). This ensures :exit selectors are automatically generated for all node types and simplifies visitor type definitions.

What changes did you make? (Give an overview)

  • Added a WithExit<RuleVisitorType> helper type to automatically add :exit variants.
  • Replaced the manually defined JSONRuleVisitor interface with a version using WithExit.

Note on parent parameter:

The optional parent parameters were removed from all visitor methods. At runtime, these parameters always appear to be undefined. Currently, only this plugin and the eslint/markdown plugin use parent parameters in their visitors — the eslint/css plugin 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?

@eslintbot eslintbot added this to Triage Oct 25, 2025
@github-project-automation github-project-automation bot moved this to Needs Triage in Triage Oct 25, 2025
@fasttime fasttime moved this from Needs Triage to Triaging in Triage Oct 27, 2025
src/types.ts Outdated
Comment on lines 58 to 62
WithExit<{
[Node in AnyNode as Node["type"]]?:
| ((node: Node) => void)
| undefined;
}> {}
Copy link
Member

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:

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]));

Copy link
Author

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

Copy link
Member

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.

@fasttime fasttime moved this from Triaging to Evaluating in Triage Oct 27, 2025
@jaymarvelz jaymarvelz changed the title refactor: simplify JSONRuleVisitor fix: correct parent typing for rule visitors Oct 29, 2025
@eslint-github-bot eslint-github-bot bot added the bug Something isn't working label Oct 29, 2025
@fasttime fasttime moved this from Evaluating to Implementing in Triage Oct 29, 2025
"String:exit": (...args) => testVisitor<StringNode>(...args),

// Unknown selectors allowed
"Identifier[name=foo]"(node: IdentifierNode, parent: MemberNode) {},

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> = {

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working chore

Projects

Status: Implementing

Development

Successfully merging this pull request may close these issues.

3 participants