swc-project / swc

Rust-based platform for the Web

Home Page:https://swc.rs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bug: ObjectPat Syntax Error for { a } = { a: 1 }

caoccao opened this issue · comments

Describe the bug

Problem

{ a } = { a: 1 } works in Node.js well, but fails to be parsed by SWC.

Result in Node.js

Welcome to Node.js v20.8.0.
Type ".help" for more information.
> let a
undefined
> { a } = { a: 1 }
{ a: 1 }
> a
1
>

Result in SWC

  x Expression expected
   ,----
 1 | { a } = { a: 1 }
   :       ^
   `----


Caused by:
    0: failed to parse code
    1: Syntax Error

Input code

{ a } = { a: 1 }

Config

{
  "jsc": {
    "parser": {
      "syntax": "ecmascript",
      "jsx": false
    },
    "target": "es2020",
    "loose": false,
    "minify": {
      "compress": false,
      "mangle": false
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": false,
  "isModule": false
}

Playground link (or link to the minimal reproduction)

https://play.swc.rs/?version=1.5.5&code=H4sIAAAAAAAAA6tWSFSoVbBVqFZItFIwVKgFABuN%2F0gQAAAA&config=H4sIAAAAAAAAA1VPOw6DMAzdOUXkuQNi6NA79BBWalBQfopTqRHi7g350LL5%2FfzsbRACVpbwEFseM%2FAYmMKJM8PJRvxkBkgaZBmUj3Dr6sqHNKNmKtReFYgYFoolxdM4jS0B2jmmnmicUVbN6b9TOuMDMV%2BNhxXtounaOLRWMO71LmL7JSZP9YI7%2FEy97FwMip89WdfuX4b2MUIYAQAA

SWC Info output

1.5.5

Expected behavior

{ a } = { a: 1 } should pass the syntax check.

Actual behavior

No response

Version

1.5.5

Additional context

No response

image

It's invalid syntax. nodeREPL parses it as an expression, but it's not a valid statement. The specification says it's a block statement if a line starts with {

Thank you for the quick response. You are right.

I just tried in d8 and got an syntax error.

V8 version 12.5.227.6
d8> let a
undefined
d8> { a } = { a: 1 }
(d8):1: SyntaxError: Unexpected token '='
{ a } = { a: 1 }
      ^
SyntaxError: Unexpected token '='