Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Reworking theparent property on AST Nodes #1417

Closed
Labels
accepting prsGo ahead, send a pull request that resolves this issuebreaking changeThis change will require a new major version to be releasedenhancementNew feature or requestpackage: typescript-estreeIssues related to @typescript-eslint/typescript-estree
Milestone
@bradzacher

Description

@bradzacher

Right now theparent property is optional on all AST nodes.
However in reality, it's always defined on all AST nodes, except forProgram, 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):

// this shouldn't happen, checking just in case/* istanbul ignore if */if(node.parent){// ...}// selector asserts this is correctconstparent=node.parentasTSESTree.VariableDeclaration;if(node.parent?.type===AST_NODE_TYPES.VariableDeclaration){// ...}

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:

  1. changetypescript-estree so that it sets theparent appropriately.
  2. create a separate set of types for use within plugins, which correctly type theparent 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:

interfaceBaseNode<TParentextendsNode=Node>{// ...parent:TParent;}interfaceVariableDeclarator<TParentextendsNode=Node>extendsBaseNode<TParent>{}interfaceProgramextendsBaseNode{parent?:never;}

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issuebreaking changeThis change will require a new major version to be releasedenhancementNew feature or requestpackage: typescript-estreeIssues related to @typescript-eslint/typescript-estree

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp