Skip to content

Bug: DefinitionBase shouldn't allow its Node type to be TSESTree.Program #11334

Open
@JoshuaKGoldberg

Description

@JoshuaKGoldberg

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Relevant Package

scope-manager

Playground Link

No response

Repro Code

See https://github.com/typescript-eslint/typescript-eslint/pull/11322/files#r2158282689

ESLint Config

module.exports = {
  parser: "@typescript-eslint/parser",
  rules: {
    "@typescript-eslint/<rule-name>": ["error", ...<options>],
  },
};

tsconfig

{
  "compilerOptions": {
    // ...
  }
}

Expected Result

The types of nodes on scope-manager definitions are, to my knowledge, always nodes inside a source file. They're never the TSESTree.Program themselves. Thus, they should always have a defined parent property set to some parent node.

Actual Result

DefinitionBase's node property's type is from a type parameter constrained to TSESTree.Node:

TSESTree.Node includes TSESTree.Program as a type constituent. Which is problematic because that's the only type of node that doesn't have a required parent property.

We see in https://github.com/typescript-eslint/typescript-eslint/pull/11322/files#r2158282689 that some areas of code have to use a ! after node.parent.

Additional Info

My initial instinct for solving this is to:

  1. Make a TSESTree.NodeWithParent type union with all the constituents of TSESTree.Node except TSESTree.Program
  2. Use that TSESTree.NodeWithParent in DefinitionBase and other scope-manager types that can only ever be a node inside a file (i.e. with a parent)

Versions

package version
@typescript-eslint/scope-manager 8.34.1
@typescript-eslint/types 8.34.1

💖

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtriageWaiting for team members to take a look

    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