EricSmekens / jsep

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

error out when comment at the head of first line #191

Closed zcappp closed 3 years ago

zcappp commented 3 years ago

If I comment at the head of first line with the comment plugin registered:

// log(11)
log(22)

jsep will throw error when parsing:

Error: Unexpected "l" at character 11
    at Jsep.throwError (jsep.js:327)
    at Jsep.gobbleExpressions (jsep.js:424)
    at Jsep.parse (jsep.js:385)
    at jsep (jsep.js:1040)
    at evaluate.js:50
    at __WEBPACK_DEFAULT_EXPORT__ (evaluate.js:49)
    at exec (inityxy.js:263)
    at HTMLUnknownElement.callCallback (react-dom.development.js:188)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:237)
    at invokeGuardedCallback (react-dom.development.js:292)

but it works fine if I swap the lines:

log(22)
// log(11)

parsed as:

{
    "type": "CallExpression",
    "arguments": [
        {
            "type": "Literal",
            "value": 22,
            "raw": "22"
        }
    ],
    "callee": {
        "type": "Identifier",
        "name": "log"
    }
}
6utt3rfly commented 3 years ago

@zclc - I can't reproduce this (when using the comment plugin). This test passes:

test('should not throw', (assert) => {
  testParser(`log(22)
// log(11)
`,  {
  type: 'CallExpression',
  arguments: [
    {
      type: 'Literal',
      value: 22,
      raw: '22'
    }
  ],
  callee: {
    type: 'Identifier',
    name: 'log'
  }}, assert);
});
zcappp commented 3 years ago

@6utt3rfly yes, if comment out the second line in my second case:

log(22)
// log(11)

it works fine, as it is NOT at the head of first line. but if comment out the first line as the NG case:

// log(11)
log(22)

it errors out. another note: the case is the COMPOUND expression, not in one line, as log(22) // log(11)

6utt3rfly commented 3 years ago

Thanks @zclc - I guess my unit test was not written correctly, which is why I couldn't see it! 🤦🏻‍♀️

github-actions[bot] commented 3 years ago

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

The release is available on:

Your semantic-release bot :package::rocket: