EricSmekens / jsep

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

New operator not wholly supported #243

Closed rossm6 closed 1 year ago

rossm6 commented 1 year ago

This does not work -

const p = parse("new Date(new Date().setDate(new Date().getDate()-5))");
evaluate(p, { Date }); // gives error: Error: 'setDate' is not a function

P.S. I have the "new" keyword plugin added (which is why it gets passed)

6utt3rfly commented 1 year ago

I checked the generated AST by jsep and confirmed the issue:

{
  "type": "NewExpression",
  "arguments": [
    {
      "type": "CallExpression", // not NewExpression
      "arguments": [
        {
          "type": "BinaryExpression",
          "operator": "-",
          "left": {
            "type": "CallExpression", // not NewExpression
            "arguments": [],
            "callee": {
              "type": "MemberExpression",
              "computed": false,
              "object": {
                "type": "NewExpression", // not CallExpression
                "arguments": [],
                "callee": {
                  "type": "Identifier",
                  "name": "Date"
                }
              },
              "property": {
                "type": "Identifier",
                "name": "getDate"
              }
            }
          },
          "right": {
            "type": "Literal",
            "value": 5,
            "raw": "5"
          }
        }
      ],
      "callee": {
        "type": "MemberExpression",
        "computed": false,
        "object": {
          "type": "NewExpression", // not CallExpression
          "arguments": [],
          "callee": {
            "type": "Identifier",
            "name": "Date"
          }
        },
        "property": {
          "type": "Identifier",
          "name": "setDate"
        }
      }
    }
  ],
  "callee": {
    "type": "Identifier",
    "name": "Date"
  }
}
rossm6 commented 1 year ago

Hi. Is there an update on this?

github-actions[bot] commented 1 year ago

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

The release is available on:

Your semantic-release bot :package::rocket:

rossm6 commented 1 year ago

Thanks for the quick fix