#2445 Js: Invalid left-hand side in assignment

Jeremy Criquet Tue 25 Aug 2015

Compiling something like this in JavaScript:

b := false
a := b || ( b = true )

Compiles into this:

var b = false;
var a = (b || b = true);

And produces this error:

Uncaught ReferenceError: Invalid left-hand side in assignment

Based on operator presidence, this will not compile in Fantom (makes sense):

a := b || b = true

The reason for that is the same reason why JavaScript is complaining.

It should instead compile to:

var a = (b || (b = true));

Jeremy Criquet Tue 25 Aug 2015

If you're wondering why I'm doing this, here's a better example:

trimStr := "A quick brown"
textStr := "A quick brown fox jumps over the lazy dog"
trim := trimStr.split
stop := false
textStr = text.split.findAll |word, i| { stop || ( stop = trim.getSafe( i ) != word ) }.join( " " )

There's a way around this, but I just thought I'd point the bug out.

andy Tue 25 Aug 2015

Ticket promoted to #2445 and assigned to andy

SlimerDude Sun 11 Aug 2019

I found another example today, taken from Zip.read():

zip := Zip.read(in)
File? entry
while ((entry = zip.readNext()) != null) {
    ...
    ...
}

generates the following Javascript:

var zip = fan.sys.Zip.read(in);
var entry = null;
while ((entry = zip.readNext() != null)) {
    ...
    ...
}

You can see how the enclosing brackets are wrong in the Javascript code - which may give a clue on how to fix the compiler.

The annoying thing thing about this bug is that it causes a Javascript compilation error that invalidates the entire pod's javascript:

Uncaught ReferenceError: Invalid left-hand side in assignment

SlimerDude Wed 30 Aug 2023

I bumped in to this problem again today, and given Fantom JS is being re-looked at, I thought I'd bump the thread!

andy Wed 30 Aug 2023

Ticket reassigned from andy to matthew

matthew Fri 1 Sep 2023

Ticket resolved in 1.0.79

This should be resolved in 1.0.79. I can verify with a build from the tip that both the old and new js code are emitting correctly.

SlimerDude Sat 2 Sep 2023

Fantastic - thanks Matthew!

Login or Signup to reply.