-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Reworking the parent
property on AST Nodes
#1417
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
Comments
Note: Setting up parent within ts-estree can be misleading for projects that do not use eslint. what do you think about using type augmentation inside eslint-utils/plugin that will extend nodes instead? import { Node } from '@typescript-eslint/typescript-estree/dist/ts-estree/ts-estree';
declare module '@typescript-eslint/typescript-estree/dist/ts-estree/ts-estree' {
export interface BaseNode {
parent: Node;
}
export interface Program {
parent: never;
}
} |
Hence point (1) - changing
Potentially, but the problem with that is that it doesn't help external users of If we can make the declaration merging work for the types exported from the |
i like idea of using generics for parent, it will be awesome if there will be way to do so from within |
fwiw it looks like type argumentation no longer works because
I can't find anyway around this, though would be good if I'm wrong 😅 |
Yeah! When I sit out the ast-spec package I intentionally did not add the parent property because it's not part of the spec! It's added here Thanks for reminding me though - we can make the breaking change in the next major! |
#5252 is merged into the |
Right now the
parent
property is optional on all AST nodes.However in reality, it's always defined on all AST nodes, except for
Program
, where it is missing.This leads to a bunch of unnecessary ifs/optional chains/"trust me it's right" assertions, all of which "harm" the type safety, or are impossible to get coverage on (leading to istanbul ignore statements):
There is an issue with this, however.
typescript-estree
does not assign the parent property; this is done by ESLint as it traverses the AST at lint time.So there are two solutions I can see:
typescript-estree
so that it sets theparent
appropriately.parent
property.As an aside, doing this would unlock a really awesome benefit wherein we could make all the types generic, allowing you to specify the expected parent type ahead of time:
This would be great for things like return values of functions which explicitly check parent types, and for eslint selectors that explicitly state the tree structure (
VariableDeclaration > VariableDeclarator
).The text was updated successfully, but these errors were encountered: