-
Notifications
You must be signed in to change notification settings - Fork 68
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
Add optional AST node type #1760
base: main
Are you sure you want to change the base?
Conversation
@@ -35,6 +35,11 @@ export interface GenericAstNode extends AstNode { | |||
[key: string]: unknown | |||
} | |||
|
|||
export type OptionalAstNode<T extends AstNode> = { | |||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | |||
[P in keyof T]: P extends keyof AstNode ? T[P] : T[P] extends any[] | boolean ? T[P] : T[P] | 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.
Thanks!
Would it make sense to make this recursive too? I believe a child node that we want to recur downward is either AstNode[]
or AstNode | undefined
or AstNode
. Do I miss anything else?
Here's my attempt to take a stab at it. (This is the first time in my life that I do type-level programming actually! Apologies if anything is off)
export type OptionalAstNode<T extends AstNode> = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[P in keyof T]:
P extends keyof AstNode ?
T[P] :
T[P] extends AstNode[] ?
OptionalAstNode<T[P][number]>[] :
T[P] extends any[] | boolean ?
T[P] :
T[P] extends AstNode | undefined ?
OptionalAstNode<Extract<T[P], AstNode>> | undefined :
(T[P] extends AstNode ? OptionalAstNode<T[P]> : T[P]) | 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.
Thanks!
Would it make sense to make this recursive too?
I think it does! 😀
@@ -35,6 +35,11 @@ export interface GenericAstNode extends AstNode { | |||
[key: string]: unknown | |||
} | |||
|
|||
export type OptionalAstNode<T extends AstNode> = { |
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.
Please add JSdoc documentation to this definition, could essentially be the content of the PR description.
Please also leave a hint on the example/test cases in the arithmetics language.
@@ -35,6 +35,11 @@ export interface GenericAstNode extends AstNode { | |||
[key: string]: unknown | |||
} | |||
|
|||
export type OptionalAstNode<T extends AstNode> = { | |||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | |||
[P in keyof T]: P extends keyof AstNode ? T[P] : T[P] extends any[] | boolean ? T[P] : T[P] | 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.
[P in keyof T]: P extends keyof AstNode ? T[P] : T[P] extends any[] | boolean ? T[P] : T[P] | undefined; | |
[P in keyof T]: P extends keyof AstNode ? T[P] : T[P] extends unknown[] | boolean ? T[P] : T[P] | undefined; |
... should do the trick, too, without any need for the linter exclusion.
@@ -35,6 +35,11 @@ export interface GenericAstNode extends AstNode { | |||
[key: string]: unknown | |||
} | |||
|
|||
export type OptionalAstNode<T extends AstNode> = { | |||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | |||
[P in keyof T]: P extends keyof AstNode ? T[P] : T[P] extends any[] | boolean ? T[P] : T[P] | 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.
Thanks!
Would it make sense to make this recursive too?
I think it does! 😀
@sailingKieler @sorawee Making AST nodes deep optional, makes them very difficult to deal with. While technically correct on a type level, you cannot pass them into any method expecting a "normal" I've just tried this out, and it would require a more involved ast reflection API that gives us the optionality of all properties of an AST node. That way we could have a generic |
The proposed
OptionalAstNode<T>
is a generic type that sets all properties, that could be potentially missing due to parser errors, to be optional. However, this excludes specificially:AstNode
(mainly the$type
property which is always set).boolean
properties. Those will be set tofalse
by the parser, even if the property wasn't parsed.[]
by the parser.This change adjusts the arithmetics language validator to use that new type to ensure that all properties are properly checked for truthyness before they are accessed. Inspired by the discussion in #1758.