ratel-rust / ratel-core

High performance JavaScript to JavaScript compiler with a Rust core
Apache License 2.0
435 stars 17 forks source link

ASI for `++` and `--` tokens #78

Closed maciejhirsz closed 6 years ago

maciejhirsz commented 6 years ago
x 
++ 
y

and

x 
-- 
y

Should be parsed as x; ++y; and x; --y; respectively. They are currently parsed as x++; y; and x--; y;

s-coimbra21 commented 6 years ago

there is also

x++ + ++y

which the codegen prints as

x+++++y

then, if we try to parse that it becomes a binary expression with the left hand side being a double postfix increment but it should actually be a syntax error

EDIT: I used the web version to test this, so please ignore if it's already fixed on master

cmtt commented 6 years ago

Hello @s-coimbra21, I cannot acknowledge the issue with the source x++ + ++y.

The syntax is also valid JavaScript with the following AST:

{
  "type": "Program",
  "body": [
    {
      "type": "ExpressionStatement",
      "expression": {
        "type": "BinaryExpression",
        "operator": "+",
        "left": {
          "type": "UpdateExpression",
          "operator": "++",
          "argument": {
            "type": "Identifier",
            "name": "x",
            "start": 0,
            "end": 1
          },
          "prefix": false,
          "start": 0,
          "end": 3
        },
        "right": {
          "type": "UpdateExpression",
          "operator": "++",
          "argument": {
            "type": "Identifier",
            "name": "y",
            "start": 8,
            "end": 9
          },
          "prefix": true,
          "start": 6,
          "end": 9
        },
        "start": 0,
        "end": 9
      },
      "start": 0,
      "end": 9
    }
  ],
  "start": 0,
  "end": 9
}

The currently deployed version on http://maciej.codes/ratel-wasm generates the same code again. However, the returned ranges in source differ currently from master.

s-coimbra21 commented 6 years ago

My apologies, the first snippet parses correctly. The error is the code that is generated from that snippet (x+++++y) which should be a syntax error but which the ratel parser accepts though it produces an invalid AST (basically it parses (x++)++ + y)

maciejhirsz commented 6 years ago

Ye, there are two issues here, one is not checking validity of left value ((x++) is an invalid expression for ++ or any assignment operators). Another is that codegen should wrap the middle + in spaces.

maciejhirsz commented 6 years ago

Fixed with #85