EricSmekens / jsep

JavaScript Expression Parser
http://ericsmekens.github.io/jsep/
MIT License
836 stars 136 forks source link

arrow notation parse failure #204

Closed trenttobler closed 2 years ago

trenttobler commented 2 years ago

Context / versions:

"@jsep-plugin/arrow": "^1.0.1",
"@jsep-plugin/assignment": "^1.1.0",
"@jsep-plugin/comment": "^1.0.2",
"@jsep-plugin/object": "^1.1.0",
"@jsep-plugin/regex": "^1.0.1",
"@jsep-plugin/ternary": "^1.0.2",
"jsep": "^1.2.0"

Code sample:

  jsep = require('jsep');

  jsep.plugins.register(
    require('@jsep-plugin/assignment'),
    require('@jsep-plugin/comment'),
    require('@jsep-plugin/object'),
    require('@jsep-plugin/regex'),
    require('@jsep-plugin/ternary'),
    require('@jsep-plugin/arrow'));

  expr = jsep('f = x => y => x + y');

Thrown error:

Thrown:
{ Error: Unexpected "=" at character 6
    at Jsep.throwError (.../node_modules/jsep/dist/cjs/jsep.cjs.js:337:17)
    at Jsep.gobbleExpressions (.../node_modules/jsep/dist/cjs/jsep.cjs.js:434:11)
    at Jsep.parse (.../node_modules/jsep/dist/cjs/jsep.cjs.js:395:22)

This same expression works in javascript:

node> f = x => y => x + y;
[Function: f]
node> f(1)(2)
3
node>

This package and plugin system has been very useful and easy to work with. Any thoughts or ability to fix the above?

trenttobler commented 2 years ago

There also appears to be a weird parsing discrepancy (possibly related) with these two expressions (same context as above):

const hasSequenceExpression = jsep('x => (() => x + 1)`);
const noSequenceExpression = jsep('() => x + 1');

I'm not exactly sure why SequenceExpression type is included in the first expression, but not the second, as I think they should be in a similar context (the body of the lambda, vs. starting the expression).

6utt3rfly commented 2 years ago

Thank you for the test case @trenttobler and I can reproduce the error when assigning an arrow function. I'll try to take a look sometime over the next week. 🙂

github-actions[bot] commented 2 years ago

:tada: This issue has been resolved in version @jsep-plugin/arrow-v1.0.2-beta.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] commented 2 years ago

:tada: This issue has been resolved in version @jsep-plugin/arrow-v1.0.2 :tada:

The release is available on:

Your semantic-release bot :package::rocket: