espruino / EspruinoTools

JavaScript library of tools for Espruino - used for the Web IDE, CLI, etc.
Apache License 2.0
150 stars 89 forks source link

Espruino CLI: Arrow functions doesn't work in modules #112

Closed orryoracle closed 4 years ago

orryoracle commented 4 years ago

I'm using VSCode with the Espruino tools to develop using Espruino WiFi.

Function calls using arrow function such as: setTimeout(()=>{ console.log('foo')},1000);

Works in main file, but throws syntax error when used in modules and then used in main file:

>Uncaught SyntaxError: Got ';' expected ',' at line 1 col 34 setTimeout(()=>console.log('foo');,1000),exports.foo=10;

(with ^ pointing at semicolon before ,1000)

Am I missing anything in flags etc when calling espruino?

gfwilliams commented 4 years ago

Ahh, sorry - yes, I believe this is a bug in the Esprima minifier which minifies modules by default. A few months ago I updated but it looks like it's still buggy :(

The easy solution is just to add --config MODULE_MINIFICATION_LEVEL= to the command-line, which should disable module minification. --config MODULE_MINIFICATION_LEVEL=TERSER may also help.

I'll leave the issue open and will see if I can update the minifier, however it may be it makes more sense just to remove it and switch to Terser

orryoracle commented 4 years ago

--config MODULE_MINIFICATION_LEVEL=TERSER works - thanks!

orryoracle commented 4 years ago

Actually, --config MODULE_MINIFICATION_LEVEL=TERSER throws error and import modules unminified (which works I guess):

Terser errors in WifiManager - sending unminified code. DefaultsError:minifyFnis not a supported option

bartmichu commented 4 years ago

Looks like not only modules are affected. I'm getting unexpected results with this code:

const photoresistor = Pin(B1);

E.on('init', () => {
  photoresistor.mode('analog');
});

setInterval(() => {
  analogWrite(LED1, analogRead(photoresistor), { soft: true });
}, 300);

It doesn't produce any errors but LED keeps blinking. I was expecting LED to stay lit, just like it does when I upload the same code with Chrome App.

gfwilliams commented 4 years ago

So what command line are you using for the upload? and please can you post the output after adding -o out.js to the command line?

gfwilliams commented 4 years ago

Fixed with https://github.com/espruino/EspruinoTools/commit/a1493a2146455bc151da003113101a665bce0b50