oxc icon indicating copy to clipboard operation
oxc copied to clipboard

Parser/Codegen: should preserve parentheses around assignment target

Open kermanx opened this issue 1 year ago • 1 comments

Current Behavior

Playground link

var fn;
(fn) = function() {};

The AST doesn't preserve the parentheses in (fn), which causes the codegen generates:

var fn;
fn = function() {};

Which has different runtime behavior: in the original code, fn.name is an empty string, but in the generated code, fn.name is "fn". Run this playground.

Expected behavior

Parentheses around assignment target should be preserved and printed.

Background

I encountered the problem when trying to pass this test262 case: https://github.com/tc39/test262/blob/main/test/language/expressions/assignment/fn-name-lhs-cover.js

kermanx avatar Sep 29 '24 03:09 kermanx

https://oxc.rs/docs/learn/ecmascript/grammar.html#parenthesized-expression

Nobody uses this in the real world so I didn't spend the time fixing it. It'll be some ugly hacks in the parser and codegen ... but only to satisfy the spec?

This is a bug so I'll leave the issue open.

Boshen avatar Sep 29 '24 03:09 Boshen

As it turns out none of the tools (esbuild, swc, prettier, babel with prettify) preserve this semantic. I'm unwilling to implement this :-(

Boshen avatar Oct 10 '24 13:10 Boshen