Skip to content

[Bug]: Weird AST for as in ArrayPattern #14498

@sosukesuzuki

Description

@sosukesuzuki

💻

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

[a as any] = x;

Configuration file name

No response

Configuration

const { parse } = require("@babel/parser");

const input = `[a as any] = x;`

const result = parse(input, { plugins: ["typescript"] });

console.log(result):

Current and expected behavior

current:

{
  "type": "AssignmentExpression",
  "operator": "=",
  "left": {
    "type": "ArrayPattern",
    "elements": [
      {
        "type": "Identifier",
        "name": "a",
        "typeAnnotation": {
          "type": "TSAnyKeyword"
        }
      }
    ]
  },
  "right": {
    "type": "Identifier",
    "name": "x"
  }
}

expected:

{
  "type": "AssignmentExpression",
  "operator": "=",
  "left": {
    "type": "ArrayPattern",
    "elements": [
      {
        "type": "TSAsExpression",
        "expression": {
          "type": "Identifier",
          "name": "a"
        },
        "typeAnnotation": {
          "type": "TSAnyKeyword"
        }
      }
    ]
  },
  "right": {
    "type": "Identifier",
    "name": "x"
  }
}

Environment

System:
OS: macOS 10.15.7
Binaries:
Node: 14.18.1 - ~/.nvm/versions/node/v14.18.1/bin/node
npm: 6.14.15 - ~/.nvm/versions/node/v14.18.1/bin/npm
npmPackages:
@babel/cli: ^7.17.0 => 7.17.0
@babel/core: ^7.17.0 => 7.17.8
@babel/eslint-config-internal: workspace:^ => 7.16.1
@babel/eslint-parser: workspace:^ => 7.17.0
@babel/eslint-plugin-development: workspace:^ => 7.17.7
@babel/eslint-plugin-development-internal: workspace:^ => 7.17.7
@babel/plugin-proposal-dynamic-import: ^7.16.7 => 7.16.7
@babel/plugin-proposal-export-namespace-from: ^7.16.7 => 7.16.7
@babel/plugin-proposal-object-rest-spread: ^7.16.7 => 7.16.7
@babel/plugin-transform-modules-commonjs: ^7.16.8 => 7.16.8
@babel/plugin-transform-runtime: ^7.17.0 => 7.17.0
@babel/preset-env: ^7.16.11 => 7.16.11
@babel/preset-flow: ^7.16.7 => 7.16.7
@babel/preset-typescript: ^7.16.7 => 7.16.7
@babel/runtime: ^7.17.0 => 7.17.0
babel-plugin-transform-charcodes: ^0.2.0 => 0.2.0
eslint: ^8.9.0 => 8.9.0
jest: ^27.4.0 => 27.4.0

Possible solution

No response

Additional context

prettier/prettier#12706

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: typescriptoutdatedA closed issue/PR that is archived due to age. Recommended to make a new issuepkg: parser

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions