Skip to content

[ts]No syntax error is thrown for invalid type annotation #12180

@sosukesuzuki

Description

@sosukesuzuki

Bug Report

  • I would like to work on a fix!

Current behavior

Successful parsing:

AST
{
  "type": "File",
  "start": 0,
  "end": 5,
  "loc": {
    "start": {
      "line": 1,
      "column": 0
    },
    "end": {
      "line": 1,
      "column": 5
    }
  },
  "errors": [],
  "program": {
    "type": "Program",
    "start": 0,
    "end": 5,
    "loc": {
      "start": {
        "line": 1,
        "column": 0
      },
      "end": {
        "line": 1,
        "column": 5
      }
    },
    "sourceType": "module",
    "interpreter": null,
    "body": [
      {
        "type": "ExpressionStatement",
        "start": 0,
        "end": 5,
        "loc": {
          "start": {
            "line": 1,
            "column": 0
          },
          "end": {
            "line": 1,
            "column": 5
          }
        },
        "expression": {
          "type": "TSTypeCastExpression",
          "start": 1,
          "end": 4,
          "loc": {
            "start": {
              "line": 1,
              "column": 1
            },
            "end": {
              "line": 1,
              "column": 4
            }
          },
          "extra": {
            "parenthesized": true,
            "parenStart": 0
          },
          "expression": {
            "type": "Identifier",
            "start": 1,
            "end": 2,
            "loc": {
              "start": {
                "line": 1,
                "column": 1
              },
              "end": {
                "line": 1,
                "column": 2
              },
              "identifierName": "a"
            },
            "name": "a"
          },
          "typeAnnotation": {
            "type": "TSTypeAnnotation",
            "start": 2,
            "end": 4,
            "loc": {
              "start": {
                "line": 1,
                "column": 2
              },
              "end": {
                "line": 1,
                "column": 4
              }
            },
            "typeAnnotation": {
              "type": "TSTypeReference",
              "start": 3,
              "end": 4,
              "loc": {
                "start": {
                  "line": 1,
                  "column": 3
                },
                "end": {
                  "line": 1,
                  "column": 4
                }
              },
              "typeName": {
                "type": "Identifier",
                "start": 3,
                "end": 4,
                "loc": {
                  "start": {
                    "line": 1,
                    "column": 3
                  },
                  "end": {
                    "line": 1,
                    "column": 4
                  },
                  "identifierName": "b"
                },
                "name": "b"
              }
            }
          }
        }
      }
    ],
    "directives": []
  },
  "comments": []
}

Input Code

(a:b);

Expected behavior

Throw syntax error Did not expect a type annotation here..

Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)

Environment

  • @babel/parser: 7.12.0

Possible Solution

Additional context

Found from working on prettier/prettier#9408
This is a 7.12.0 regression

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions