Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
1 task
sosukesuzuki opened this issue Apr 27, 2022 · 9 comments 路 Fixed by #14500
Closed
1 task

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

sosukesuzuki opened this issue Apr 27, 2022 · 9 comments 路 Fixed by #14500
Labels
area: typescript i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: parser

Comments

@sosukesuzuki
Copy link
Member

馃捇

  • 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

@thorn0
Copy link
Contributor

thorn0 commented Apr 28, 2022

One more test case: ({a: [x as any] } = { a: [2] }); is valid TS (playground), but Babel can't parse it: SyntaxError: Unexpected type cast in parameter position. (1:6)
(I wish I knew why TS needs this syntax...)

@nicolo-ribaudo
Copy link
Member

Thanks! An even simpler reproduction is ([a as T] = b).

@thorn0
Copy link
Contributor

thorn0 commented Apr 28, 2022

Turns out ({a: x as any } = { a: [2] }); is valid too (playground), but Babel yields SyntaxError: Invalid left-hand side in object destructuring pattern. (1:5). #14500 fixes this.

@thorn0
Copy link
Contributor

thorn0 commented Apr 28, 2022

More things that are accepted by TS, but not by Babel:

  • ({ ...x as T } = y);
  • [...x as T] = y;

@nicolo-ribaudo
Copy link
Member

nicolo-ribaudo commented Apr 28, 2022

Another one, that is accepted by Babel but not TS: (a! = c) => d

@nicolo-ribaudo
Copy link
Member

This is fun

@thorn0
Copy link
Contributor

thorn0 commented Apr 28, 2022

Speaking of !: ({...x!} = y); (playground) yields SyntaxError: Invalid rest operator's argument.

Also I've just realized that destructurings in ES can be used in the middle of assignment chains: let b = { a } = { a: 1 }, but Babel seems to have no problem with that.

@nicolo-ribaudo
Copy link
Member

Fun fact: ([...[]] = []) is valid, but ({...{}} = {}) isn't.

@JLHwung
Copy link
Contributor

JLHwung commented Apr 28, 2022

Speaking of !: ({...x!} = y); (playground) yields SyntaxError: Invalid rest operator's argument.

Also I've just realized that destructurings in ES can be used in the middle of assignment chains: let b = { a } = { a: 1 }, but Babel seems to have no problem with that.

Yeah an assignment expression is perfectly valid RHS. The transform spent some efforts preserving the completion.

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Aug 1, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: typescript i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: parser
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants