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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(class{f(a=1/2){}}.b=2) is syntax error on Safari <= 16.2 #3072

Closed
hyrious opened this issue Apr 19, 2023 · 2 comments
Closed

(class{f(a=1/2){}}.b=2) is syntax error on Safari <= 16.2 #3072

hyrious opened this issue Apr 19, 2023 · 2 comments

Comments

@hyrious
Copy link

hyrious commented Apr 19, 2023

see it live

// input
a=1,(class{f(b=1/2){}}).c=2

// output
a = 1, class {
  f(b = 1 / 2) {
  }
}.c = 2;
// Throws error before (<=) Safari 16.2
SyntaxError: Left hand side of operator '=' must be a reference
@hyrious hyrious changed the title Wrongly minify syntax 'a=0;(class{}).b=1' (class{f(a=1/2){}}.b=2) is syntax error on Safari Apr 20, 2023
@hyrious hyrious changed the title (class{f(a=1/2){}}.b=2) is syntax error on Safari (class{f(a=1/2){}}.b=2) is syntax error on Safari <= 16.2 Apr 20, 2023
@hyrious
Copy link
Author

hyrious commented Apr 21, 2023

For now, it's ok to leave this issue to "won't fix" since Safari 16.3+ (iOS 16.3+, which is iPhone 8+ since 2017) already fixed that and this is not a general JavaScript feature.

Others may change their build options to disable --minify-syntax to make that work on old Safari.

Feel free to close this issue.

@evanw
Copy link
Owner

evanw commented May 13, 2023

Thank you for the report. I looked into this and there are a bunch of these. Some examples (I'm testing Safari 12.1 since that's the only old version I have access to):

  1. x(class{f(a=-1){}}.b=2) is a syntax error in Safari but x((class{f(a=-1){}}).b=2) is not (nor is x(class{f(a=1){}}.b=2) so the - is part of the trigger)
  2. import(x+y)[0] = 0 is a syntax error in Safari but (import(x+y))[0] = 0 is not (nor is import(x)[0] = 0 so the + is part of the trigger)
  3. [+x,][0]=0 is a syntax error in Safari but ([+x,])[0]=0 is not (or is [+x][0]=0 so the , is part of the trigger)

It looks like Terser with safari10: true doesn't handle any of these. UglifyJS with webkit: true handles the first one by wrapping the class in parentheses (even when it's unnecessary because Safari would parse it fine). But UglifyJS doesn't handle the other ones.

The specific workaround from UglifyJS is here: mishoo/UglifyJS#2056. So the (overly conservative) rule is something like "wrap a function or class expression if the parent is a property access." I can do that in esbuild. And it looks like I can ignore other cases where this happens because they aren't handled by other minifiers in the ecosystem either.

@evanw evanw closed this as completed in 2f2b90a May 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants