sweet-js / sweet-core

Sweeten your JavaScript.
https://www.sweetjs.org
BSD 2-Clause "Simplified" License
4.59k stars 209 forks source link

TypeError: decl.init.body is undefined #543

Open jeandrek opened 8 years ago

jeandrek commented 8 years ago

When I enter

syntax fn = function (ctx) {
  let args = ctx.next().value;
  let body = ctx.next().value; 
  return #`function ${args} ${body}`;
}

(fn () {
  return 42;
})

into the online editor I get this error message:

TypeError: decl.init.body is undefined

I'm using Firefox Nightly version 49.0a1.

disnet commented 8 years ago

Your example is actually not a valid program because of ASI. It's actually getting parsed as:

syntax fn = function (ctx) {
  let args = ctx.next().value;
  let body = ctx.next().value; 
  return #`function ${args} ${body}`;
}(fn () {
  return 42;
})

Add the necessary ; and everything works out fine (yet another nail in the coffin of semicolon-less style).

That said I'm leaving this issue open so we can make a better error message. It should be able to say the { at (fn () { ... is invalid.

jeandrek commented 8 years ago

Oh, thanks.