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

Function can have an expression as a body? #96

Closed
jamestalmage opened this issue Mar 19, 2015 · 3 comments
Closed

Function can have an expression as a body? #96

jamestalmage opened this issue Mar 19, 2015 · 3 comments

Comments

@jamestalmage
Copy link
Contributor

// core.js
def("Function")
    .bases("Node")
    .field("id", or(def("Identifier"), null), defaults["null"])
    .field("params", [def("Pattern")])
    .field("body", or(def("BlockStatement"), def("Expression")));

What would an ES5 function without a block statement look like? Is this just here to support arrow functions in ES6? If so, wouldn't it be better to override it in es6.js?

@benjamn
Copy link
Owner

benjamn commented Mar 19, 2015

Yep, that's left over from the original Mozilla Parser API (see the note). It's actually still possible (in SpiderMonkey) to write function increment(x) x + 1.

@jamestalmage
Copy link
Contributor Author

interesting.

This works:

var ast = b.functionDeclaration(
  b.identifier('increment'),
  [b.identifier('x')],
  b.binaryExpression(
    '+',
    b.identifier('x'),
    b.literal(1)
  )
);

But if you try to print it it throws.

@jamestalmage
Copy link
Contributor Author

Esprima seems to throw an error when parsing function increment(x) x + 1 anyways.

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