Skip to content

Bug: JSDocNonNullableType has extra nested typeAnnotation with invalid position #11064

Closed as not planned
@leaysgur

Description

@leaysgur

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

parser

Playground Link

https://typescript-eslint.io/play/#ts=5.4.3&showAST=es&fileType=.tsx&code=GYVwdgxgLglg9mABMAjACgIYC5EbATwEocNEYBnRcqAJxjAHMBCAbwChFEaBTKEGpLRDcA3GwC%2BbIA&eslintrc=N4KABGBEBOCuA2BTAzpAXGYBfEWg&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA&tokens=false

Repro Code

function f1(a: any): a is string!{
  return true;
}

ESLint Config

tsconfig

Expected Result

{
  "type": "Program",
  "start": 0,
  "end": 51,
  "body": [
    {
      "type": "FunctionDeclaration",
      "start": 0,
      "end": 51,
      "id": {
        "type": "Identifier",
        "start": 9,
        "end": 11,
        "name": "f1",
        "decorators": [],
        "optional": false,
        "typeAnnotation": null
      },
      "expression": false,
      "generator": false,
      "async": false,
      "params": [
        {
          "type": "Identifier",
          "start": 12,
          "end": 18,
          "name": "a",
          "decorators": [],
          "optional": false,
          "typeAnnotation": {
            "type": "TSTypeAnnotation",
            "start": 13,
            "end": 18,
            "typeAnnotation": {
              "type": "TSAnyKeyword",
              "start": 15,
              "end": 18
            }
          }
        }
      ],
      "body": {
        "type": "BlockStatement",
        "start": 33,
        "end": 51,
        "body": [
          {
            "type": "ReturnStatement",
            "start": 37,
            "end": 49,
            "argument": {
              "type": "Literal",
              "start": 44,
              "end": 48,
              "value": true,
              "raw": "true"
            }
          }
        ]
      },
      "declare": false,
      "typeParameters": null,
      "returnType": {
        "type": "TSTypeAnnotation",
        "start": 19,
        "end": 33,
        "typeAnnotation": {
          "type": "TSTypePredicate",
          "start": 21,
          "end": 33,
          "parameterName": {
            "type": "Identifier",
            "start": 21,
            "end": 22,
            "name": "a",
            "decorators": [],
            "optional": false,
            "typeAnnotation": null
          },
          "asserts": false,
          "typeAnnotation": { // ============================== 👀
            "type": "TSTypeAnnotation",
            "start": 26,
            "end": 33,
            "typeAnnotation": {
              "type": "TSJSDocNonNullableType",
              "start": 26,
              "end": 33,
              "typeAnnotation": {
                "type": "TSStringKeyword",
                "start": 26,
                "end": 32
              },
              "postfix": true
            } // ============================================= 👀
          }
        }
      }
    }
  ],
  "sourceType": "module"
}

Actual Result

{
  "type": "Program",
  "start": 0,
  "end": 51,
  "body": [
    {
      "type": "FunctionDeclaration",
      "start": 0,
      "end": 51,
      "async": false,
      "body": {
        "type": "BlockStatement",
        "start": 33,
        "end": 51,
        "body": [
          {
            "type": "ReturnStatement",
            "start": 37,
            "end": 49,
            "argument": {
              "type": "Literal",
              "start": 44,
              "end": 48,
              "raw": "true",
              "value": true
            }
          }
        ]
      },
      "declare": false,
      "expression": false,
      "generator": false,
      "id": {
        "type": "Identifier",
        "start": 9,
        "end": 11,
        "decorators": [],
        "name": "f1",
        "optional": false,
        "typeAnnotation": null
      },
      "params": [
        {
          "type": "Identifier",
          "start": 12,
          "end": 18,
          "decorators": [],
          "name": "a",
          "optional": false,
          "typeAnnotation": {
            "type": "TSTypeAnnotation",
            "start": 13,
            "end": 18,
            "typeAnnotation": {
              "type": "TSAnyKeyword",
              "start": 15,
              "end": 18
            }
          }
        }
      ],
      "returnType": {
        "type": "TSTypeAnnotation",
        "start": 19,
        "end": 33,
        "typeAnnotation": {
          "type": "TSTypePredicate",
          "start": 21,
          "end": 33,
          "asserts": false,
          "parameterName": {
            "type": "Identifier",
            "start": 21,
            "end": 22,
            "decorators": [],
            "name": "a",
            "optional": false,
            "typeAnnotation": null
          },
          "typeAnnotation": { // ============================== 👀
            "type": "TSTypeAnnotation",
            "start": 26,
            "end": 33,
            "typeAnnotation": {
              "type": "TSJSDocNonNullableType",
              "start": 26,
              "end": 33,
              "typeAnnotation": {
                "type": "TSTypeAnnotation",
                "start": 24, // ❗️
                "end": 32,
                "typeAnnotation": {
                  "type": "TSStringKeyword",
                  "start": 26,
                  "end": 32
                }
              },
              "id": 0,
              "original": null,
              "emitNode": null,
              "postfix": true
            }
          }
        } // ================================================== 👀
      },
      "typeParameters": null
    }
  ],
  "sourceType": "module"
}

Additional Info

I'm not sure if extra typeAnnotation nesting is intended or not, but at least its starting position should be fixed?

Versions

package version
@typescript-eslint/parser 8.29.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinglocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.wontfixThis will not be worked onworking as intendedIssues that are closed as they are working as intended

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions