-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
fix: use AST.Program type for Program node
#20244
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
Conversation
✅ Deploy Preview for docs-eslint ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
| /** Adds matching `:exit` selectors for all properties of a `RuleVisitor`. */ | ||
| type WithExit<RuleVisitorType extends RuleVisitor> = { | ||
| [Key in keyof RuleVisitorType as | ||
| | Key | ||
| | `${Key & string}:exit`]: RuleVisitorType[Key]; | ||
| }; |
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.
| // A `Program` visitor's node type has no `parent` property. | ||
| Program?: ((node: AST.Program) => 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.
The Program visitor type prevents access to the parent property of the node (which is null). This is covered by tests:
eslint/tests/lib/types/types.test.ts
Lines 817 to 820 in 1390a45
| Program(node) { | |
| // @ts-expect-error | |
| node.parent; | |
| }, |
This PR maintains the current behavior, but we could consider changing it, since parent is always set for the JS language.
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.
👏 nice! I really like the dynamic :exit type, that's a great use for a mapped type that adds properties.
typescript-eslint's done some work around parent types too that might be useful for reference:
- typescript-eslint/typescript-eslint#5252
- typescript-eslint/typescript-eslint#8347 -> typescript-eslint/typescript-eslint#8617
- typescript-eslint/typescript-eslint#10682 -> typescript-eslint/typescript-eslint#10685
Leaving open for >=1 other review as these are tricky API portions.
lumirlumir
left a comment
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.
LGTM, thanks!
Would like another review before merging.
nzakas
left a comment
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.
LGTM. Thanks!
Prerequisites checklist
What is the purpose of this pull request? (put an "X" next to an item)
[ ] Documentation update
[X] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:
What did you do? Please include the actual source code causing the issue.
Repro
What did you expect to happen?
The above TypeScript code should compile.
What actually happened? Please include the actual, raw output from ESLint.
A compiler error:
What changes did you make? (Give an overview)
This PR changes the type of the Program node in the
Rule.Nodeunion fromESTree.ProgramtoAST.Program & { parent: null; }. This add the propertiescommentsandtokensthat were previously missing and changes the type of theparentproperty fromRule.Nodetonull.I also updated and simplified
Rule.NodeListenerwith the updatedRule.Nodeunion using a mapped type. This change also fixes another unrelated bug in the type of theCallExpressionvisitor, exemplified in the code below:This currently fails with a compiler error:
The reason is that
ESTree.CallExpressionis confusingly defined as union of nodes wheretypecan be one ofCallExpressionorNewExpression(reference):Repro
The new definition avoids such bugs by ensuring that a visitor's key and node type are identical for all standard nodes.
Is there anything you'd like reviewers to focus on?