Skip to content

AST: provide better discriminated unions for ambiguous node types #6434

Open
@bradzacher

Description

@bradzacher

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:

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 be Identifier | ArrayPattern | ObjectPattern | AssignmentPattern, but an object literal property can be Expression.
        • An object pattern property can only ever be kind = 'init'
      • Literal
        • An object literal property cannot have .value = ObjectPattern | ArrayPattern | AssignmentPattern
      • For this case, I would envision that we would use the correct subtype for the specific nodes, i.e.:
        • ObjectExpression would use ObjectExpressionProperty, not Property.
        • ObjectPattern would use ObjectPatternProperty, not Property.
        • Property would be a union of ObjectExpressionProperty | ObjectPatternProperty.
    • .shorthand = true
      • .key and .value must always be Identifier
      • 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
    • .kind = 'init' && .method = true
      • Must have .value = FunctionExpression | TSEmptyBodyFunctionExpression
      • Cannot have .shorthand = true
  • 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
    • kind = 'get' | 'set'
      • Cannot have .optional = true
  • ArrowFunctionExpression
    • .expression = true
      • .value cannot be BlockStatement
  • YieldExpression
    • .delegate = true
      • .argument must be defined
  • TSTypePredicate
    • .asserts = false
      • .typeAnnotation must be non-null
  • UnaryExpression
    • .prefix must always be true

Metadata

Metadata

Assignees

Labels

ASTPRs and Issues about the AST structureaccepting prsGo ahead, send a pull request that resolves this issueenhancementNew feature or requestpackage: ast-specIssues related to @typescript-eslint/ast-spec

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    pFad - Phonifier reborn

    Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

    Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy