#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

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

Login or Signup to reply.