Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
Open
Description
Some of the nodes in the AST have certain constraints to which they must adhere which are discriminated by some property on the node.
As an example - if aMemberExpression
has.computed = false
, then the.property
must only beIdentifier | PrivateIdentifier
. For this specific case we clearly include this discriminated union in the AST types to make it easier for users to consume the AST without performing unnecessary checks to appease the types:
interfaceMemberExpressionBaseextendsBaseNode{ | |
object:Expression; | |
property:Expression|Identifier|PrivateIdentifier; | |
computed:boolean; | |
optional:boolean; | |
} | |
exportinterfaceMemberExpressionComputedNameextendsMemberExpressionBase{ | |
type:AST_NODE_TYPES.MemberExpression; | |
property:Expression; | |
computed:true; | |
} | |
exportinterfaceMemberExpressionNonComputedNameextendsMemberExpressionBase{ | |
type:AST_NODE_TYPES.MemberExpression; | |
property:Identifier|PrivateIdentifier; | |
computed:false; | |
} | |
exporttypeMemberExpression= | |
|MemberExpressionComputedName | |
|MemberExpressionNonComputedName; |
We have been pretty conservative (read: lazy 😅) with this work though and there are a lot more cases we could discriminate things further.
Specifically we could further discriminate the following cases:
Property
- Object literal property vs Object pattern property.
- Pattern
- An object pattern property
.value
can only ever beIdentifier | ArrayPattern | ObjectPattern | AssignmentPattern
, but an object literal property can beExpression
. - An object pattern property can only ever be
kind = 'init'
- An object pattern property
- Literal
- An object literal property cannot have
.value = ObjectPattern | ArrayPattern | AssignmentPattern
- An object literal property cannot have
- For this case, I would envision that we would use the correct subtype for the specific nodes, i.e.:
ObjectExpression
would useObjectExpressionProperty
, notProperty
.ObjectPattern
would useObjectPatternProperty
, notProperty
.Property
would be a union ofObjectExpressionProperty | ObjectPatternProperty
.
- Pattern
.shorthand = true
.key
and.value
mustalways beIdentifier
- Cannot have
.computed = true
- Cannot have
.kind = 'get' | 'set'
- Cannot have
.method = true
.kind = 'get' | 'set'
- Must have
.value = FunctionExpression | TSEmptyBodyFunctionExpression
- Cannot have
.shorthand = true
- Must have
.kind = 'init' && .method = true
- Must have
.value = FunctionExpression | TSEmptyBodyFunctionExpression
- Cannot have
.shorthand = true
- Must have
- Object literal property vs Object pattern property.
BinaryExpression
in
allows private brand checks like#priv in expr
.
All other operators do not allow a lone private identifier in the.left
MethodDefinition
kind = 'constructor'
- Cannot have
.computed = true
- Cannot have
.static = true
- Cannot have
.optional = true
- Cannot have
kind = 'get' | 'set'
- Cannot have
.optional = true
- Cannot have
ArrowFunctionExpression
.expression = true
.value
cannot beBlockStatement
YieldExpression
.delegate = true
.argument
must be defined
TSTypePredicate
.asserts = false
.typeAnnotation
must be non-null
UnaryExpression
.prefix
must always betrue