-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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 a MemberExpression
has .computed = false
, then the .property
must only be Identifier | 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:
typescript-eslint/packages/ast-spec/src/expression/MemberExpression/spec.ts
Lines 7 to 28 in a9cb860
interface MemberExpressionBase extends BaseNode { | |
object: Expression; | |
property: Expression | Identifier | PrivateIdentifier; | |
computed: boolean; | |
optional: boolean; | |
} | |
export interface MemberExpressionComputedName extends MemberExpressionBase { | |
type: AST_NODE_TYPES.MemberExpression; | |
property: Expression; | |
computed: true; | |
} | |
export interface MemberExpressionNonComputedName extends MemberExpressionBase { | |
type: AST_NODE_TYPES.MemberExpression; | |
property: Identifier | PrivateIdentifier; | |
computed: false; | |
} | |
export type MemberExpression = | |
| 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
must always 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