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

Parse error with arrow function returning object literal #38

Closed
swissspidy opened this issue Jul 5, 2021 · 4 comments
Closed

Parse error with arrow function returning object literal #38

swissspidy opened this issue Jul 5, 2021 · 4 comments

Comments

@swissspidy
Copy link

swissspidy commented Jul 5, 2021

Peast currently fails to parse this code:

const getTermsInfo = terms => ({
  terms,
  ...(terms === null || terms === void 0 ? void 0 : terms.reduce((accumulator, term) => { // This is line 28847
    const {
      mapById,
      mapByName,
      names
    } = accumulator;
    mapById[term.id] = term;
    mapByName[term.name] = term;
    names.push(term.name);
    return accumulator;
  }, {
    mapById: {},
    mapByName: {},
    names: []
  }))
});

I'm getting:

Peast\Syntax\Exception: Unexpected: (

Whereas this works:

const getTermsInfo = terms => {
    return {
        terms,
        ...(terms === null || terms === void 0 ? void 0 : terms.reduce((accumulator, term) => { // This is line 28847
            const {
            mapById,
            mapByName,
            names
            } = accumulator;
            mapById[term.id] = term;
            mapByName[term.name] = term;
            names.push(term.name);
            return accumulator;
        }, {
            mapById: {},
            mapByName: {},
            names: []
        }))
    };
}

This fails as well

const foo = () => ({ ...(void 0) }); // should return an empty object

This passes:

const foo = () => {
    return { ...(void 0) };
};
@ocean90
Copy link

ocean90 commented Jul 10, 2021

Some additional context:

Source which fails:

$source = 'const foo = () => ({ ...(void 0) });';
$ast    = Peast\Peast::latest( $source )->parse();
Fatal error: Uncaught Peast\Syntax\Exception: Unexpected: ( in /private/tmp/peast/vendor/mck89/peast/lib/Peast/Syntax/ParserAbstract.php:305
Stack trace:
#0 /private/tmp/peast/vendor/mck89/peast/lib/Peast/Syntax/Parser.php(2917): Peast\Syntax\ParserAbstract->error()
#1 /private/tmp/peast/vendor/mck89/peast/lib/Peast/Syntax/Parser.php(2885): Peast\Syntax\Parser->parseRestProperty()
#2 /private/tmp/peast/vendor/mck89/peast/lib/Peast/Syntax/Parser.php(2326): Peast\Syntax\Parser->parseObjectBindingPattern()
#3 /private/tmp/peast/vendor/mck89/peast/lib/Peast/Syntax/Parser.php(2417): Peast\Syntax\Parser->parseBindingPattern()
#4 /private/tmp/peast/vendor/mck89/peast/lib/Peast/Syntax/Parser.php(1604): Peast\Syntax\Parser->parseBindingElement()
#5 /private/tmp/peast/vendor/mck89/peast/lib/Peast/Syntax/Parser.php(2660): Peast\Syntax\Parser->parseFormalParameterList()
#6 /private/tmp/peast/vendor/mck89/peast/lib/Peast/Syntax/Parser.php(2720): Peast\Syntax\Parser->parseArrowParameters()
#7 /private/tmp/peast/vendor/mck89/peast/lib/P in /private/tmp/peast/vendor/mck89/peast/lib/Peast/Syntax/ParserAbstract.php on line 305
AST based on https://astexplorer.net/
{
  "type": "Program",
  "start": 0,
  "end": 36,
  "body": [
    {
      "type": "VariableDeclaration",
      "start": 0,
      "end": 36,
      "declarations": [
        {
          "type": "VariableDeclarator",
          "start": 6,
          "end": 35,
          "id": {
            "type": "Identifier",
            "start": 6,
            "end": 9,
            "name": "foo"
          },
          "init": {
            "type": "ArrowFunctionExpression",
            "start": 12,
            "end": 35,
            "id": null,
            "expression": true,
            "generator": false,
            "async": false,
            "params": [],
            "body": {
              "type": "ObjectExpression",
              "start": 19,
              "end": 34,
              "properties": [
                {
                  "type": "SpreadElement",
                  "start": 21,
                  "end": 32,
                  "argument": {
                    "type": "UnaryExpression",
                    "start": 25,
                    "end": 31,
                    "operator": "void",
                    "prefix": true,
                    "argument": {
                      "type": "Literal",
                      "start": 30,
                      "end": 31,
                      "value": 0,
                      "raw": "0"
                    }
                  }
                }
              ]
            }
          }
        }
      ],
      "kind": "const"
    }
  ],
  "sourceType": "module"
}

Source which passes:

$source = 'const foo = () => {
    return { ...(void 0) };
};';
$ast    = Peast\Peast::latest( $source )->parse();
Value of $ast
object(Peast\Syntax\Node\Program)#10 (6) {
  ["propertiesMap":protected]=>
  array(2) {
    ["body"]=>
    bool(true)
    ["sourceType"]=>
    bool(false)
  }
  ["sourceType":protected]=>
  string(6) "script"
  ["body":protected]=>
  array(1) {
    [0]=>
    object(Peast\Syntax\Node\VariableDeclaration)#44 (6) {
      ["propertiesMap":protected]=>
      array(2) {
        ["declarations"]=>
        bool(true)
        ["kind"]=>
        bool(false)
      }
      ["declarations":protected]=>
      array(1) {
        [0]=>
        object(Peast\Syntax\Node\VariableDeclarator)#20 (6) {
          ["propertiesMap":protected]=>
          array(2) {
            ["id"]=>
            bool(true)
            ["init"]=>
            bool(true)
          }
          ["id":protected]=>
          object(Peast\Syntax\Node\Identifier)#18 (5) {
            ["propertiesMap":protected]=>
            array(1) {
              ["name"]=>
              bool(false)
            }
            ["name":protected]=>
            string(3) "foo"
            ["location"]=>
            object(Peast\Syntax\SourceLocation)#19 (2) {
              ["start"]=>
              object(Peast\Syntax\Position)#14 (3) {
                ["line":protected]=>
                int(1)
                ["column":protected]=>
                int(6)
                ["index":protected]=>
                int(6)
              }
              ["end"]=>
              object(Peast\Syntax\Position)#17 (3) {
                ["line":protected]=>
                int(1)
                ["column":protected]=>
                int(9)
                ["index":protected]=>
                int(9)
              }
            }
            ["leadingComments":protected]=>
            array(0) {
            }
            ["trailingComments":protected]=>
            array(0) {
            }
          }
          ["init":protected]=>
          object(Peast\Syntax\Node\ArrowFunctionExpression)#33 (10) {
            ["propertiesMap":protected]=>
            array(1) {
              ["expression"]=>
              bool(false)
            }
            ["expression":protected]=>
            bool(false)
            ["id":protected]=>
            NULL
            ["params":protected]=>
            array(0) {
            }
            ["body":protected]=>
            object(Peast\Syntax\Node\BlockStatement)#50 (5) {
              ["propertiesMap":protected]=>
              array(1) {
                ["body"]=>
                bool(true)
              }
              ["body":protected]=>
              array(1) {
                [0]=>
                object(Peast\Syntax\Node\ReturnStatement)#41 (5) {
                  ["propertiesMap":protected]=>
                  array(1) {
                    ["argument"]=>
                    bool(true)
                  }
                  ["argument":protected]=>
                  object(Peast\Syntax\Node\ObjectExpression)#55 (5) {
                    ["propertiesMap":protected]=>
                    array(1) {
                      ["properties"]=>
                      bool(true)
                    }
                    ["properties":protected]=>
                    array(1) {
                      [0]=>
                      object(Peast\Syntax\Node\SpreadElement)#62 (5) {
                        ["propertiesMap":protected]=>
                        array(1) {
                          ["argument"]=>
                          bool(true)
                        }
                        ["argument":protected]=>
                        object(Peast\Syntax\Node\ParenthesizedExpression)#61 (5) {
                          ["propertiesMap":protected]=>
                          array(1) {
                            ["expression"]=>
                            bool(true)
                          }
                          ["expression":protected]=>
                          object(Peast\Syntax\Node\UnaryExpression)#70 (7) {
                            ["propertiesMap":protected]=>
                            array(3) {
                              ["argument"]=>
                              bool(true)
                              ["operator"]=>
                              bool(false)
                              ["prefix"]=>
                              bool(false)
                            }
                            ["operator":protected]=>
                            string(4) "void"
                            ["prefix":protected]=>
                            bool(true)
                            ["argument":protected]=>
                            object(Peast\Syntax\Node\NumericLiteral)#66 (8) {
                              ["propertiesMap":protected]=>
                              array(1) {
                                ["format"]=>
                                bool(false)
                              }
                              ["format":protected]=>
                              string(7) "decimal"
                              ["forms":protected]=>
                              array(3) {
                                ["b"]=>
                                array(3) {
                                  ["check"]=>
                                  string(18) "/^0b[01]+[01_]*$/i"
                                  ["conv"]=>
                                  string(6) "bindec"
                                  ["format"]=>
                                  string(6) "binary"
                                }
                                ["o"]=>
                                array(3) {
                                  ["check"]=>
                                  string(20) "/^0o[0-7]+[0-7_]*$/i"
                                  ["conv"]=>
                                  string(6) "octdec"
                                  ["format"]=>
                                  string(5) "octal"
                                }
                                ["x"]=>
                                array(3) {
                                  ["check"]=>
                                  string(26) "/^0x[0-9a-f]+[0-9a-f_]*$/i"
                                  ["conv"]=>
                                  string(6) "hexdec"
                                  ["format"]=>
                                  string(11) "hexadecimal"
                                }
                              }
                              ["value":protected]=>
                              int(0)
                              ["raw":protected]=>
                              string(1) "0"
                              ["location"]=>
                              object(Peast\Syntax\SourceLocation)#67 (2) {
                                ["start"]=>
                                object(Peast\Syntax\Position)#63 (3) {
                                  ["line":protected]=>
                                  int(2)
                                  ["column":protected]=>
                                  int(22)
                                  ["index":protected]=>
                                  int(42)
                                }
                                ["end"]=>
                                object(Peast\Syntax\Position)#65 (3) {
                                  ["line":protected]=>
                                  int(2)
                                  ["column":protected]=>
                                  int(23)
                                  ["index":protected]=>
                                  int(43)
                                }
                              }
                              ["leadingComments":protected]=>
                              array(0) {
                              }
                              ["trailingComments":protected]=>
                              array(0) {
                              }
                            }
                            ["location"]=>
                            object(Peast\Syntax\SourceLocation)#71 (2) {
                              ["start"]=>
                              object(Peast\Syntax\Position)#57 (3) {
                                ["line":protected]=>
                                int(2)
                                ["column":protected]=>
                                int(17)
                                ["index":protected]=>
                                int(37)
                              }
                              ["end"]=>
                              object(Peast\Syntax\Position)#65 (3) {
                                ["line":protected]=>
                                int(2)
                                ["column":protected]=>
                                int(23)
                                ["index":protected]=>
                                int(43)
                              }
                            }
                            ["leadingComments":protected]=>
                            array(0) {
                            }
                            ["trailingComments":protected]=>
                            array(0) {
                            }
                          }
                          ["location"]=>
                          object(Peast\Syntax\SourceLocation)#68 (2) {
                            ["start"]=>
                            object(Peast\Syntax\Position)#53 (3) {
                              ["line":protected]=>
                              int(2)
                              ["column":protected]=>
                              int(16)
                              ["index":protected]=>
                              int(36)
                            }
                            ["end"]=>
                            object(Peast\Syntax\Position)#69 (3) {
                              ["line":protected]=>
                              int(2)
                              ["column":protected]=>
                              int(24)
                              ["index":protected]=>
                              int(44)
                            }
                          }
                          ["leadingComments":protected]=>
                          array(0) {
                          }
                          ["trailingComments":protected]=>
                          array(0) {
                          }
                        }
                        ["location"]=>
                        object(Peast\Syntax\SourceLocation)#36 (2) {
                          ["start"]=>
                          object(Peast\Syntax\Position)#48 (3) {
                            ["line":protected]=>
                            int(2)
                            ["column":protected]=>
                            int(13)
                            ["index":protected]=>
                            int(33)
                          }
                          ["end"]=>
                          object(Peast\Syntax\Position)#69 (3) {
                            ["line":protected]=>
                            int(2)
                            ["column":protected]=>
                            int(24)
                            ["index":protected]=>
                            int(44)
                          }
                        }
                        ["leadingComments":protected]=>
                        array(0) {
                        }
                        ["trailingComments":protected]=>
                        array(0) {
                        }
                      }
                    }
                    ["location"]=>
                    object(Peast\Syntax\SourceLocation)#56 (2) {
                      ["start"]=>
                      object(Peast\Syntax\Position)#43 (3) {
                        ["line":protected]=>
                        int(2)
                        ["column":protected]=>
                        int(11)
                        ["index":protected]=>
                        int(31)
                      }
                      ["end"]=>
                      object(Peast\Syntax\Position)#64 (3) {
                        ["line":protected]=>
                        int(2)
                        ["column":protected]=>
                        int(26)
                        ["index":protected]=>
                        int(46)
                      }
                    }
                    ["leadingComments":protected]=>
                    array(0) {
                    }
                    ["trailingComments":protected]=>
                    array(0) {
                    }
                  }
                  ["location"]=>
                  object(Peast\Syntax\SourceLocation)#42 (2) {
                    ["start"]=>
                    object(Peast\Syntax\Position)#37 (3) {
                      ["line":protected]=>
                      int(2)
                      ["column":protected]=>
                      int(4)
                      ["index":protected]=>
                      int(24)
                    }
                    ["end"]=>
                    object(Peast\Syntax\Position)#54 (3) {
                      ["line":protected]=>
                      int(2)
                      ["column":protected]=>
                      int(27)
                      ["index":protected]=>
                      int(47)
                    }
                  }
                  ["leadingComments":protected]=>
                  array(0) {
                  }
                  ["trailingComments":protected]=>
                  array(0) {
                  }
                }
              }
              ["location"]=>
              object(Peast\Syntax\SourceLocation)#47 (2) {
                ["start"]=>
                object(Peast\Syntax\Position)#32 (3) {
                  ["line":protected]=>
                  int(1)
                  ["column":protected]=>
                  int(18)
                  ["index":protected]=>
                  int(18)
                }
                ["end"]=>
                object(Peast\Syntax\Position)#45 (3) {
                  ["line":protected]=>
                  int(3)
                  ["column":protected]=>
                  int(1)
                  ["index":protected]=>
                  int(49)
                }
              }
              ["leadingComments":protected]=>
              array(0) {
              }
              ["trailingComments":protected]=>
              array(0) {
              }
            }
            ["generator":protected]=>
            bool(false)
            ["async":protected]=>
            bool(false)
            ["location"]=>
            object(Peast\Syntax\SourceLocation)#31 (2) {
              ["start"]=>
              object(Peast\Syntax\Position)#23 (3) {
                ["line":protected]=>
                int(1)
                ["column":protected]=>
                int(12)
                ["index":protected]=>
                int(12)
              }
              ["end"]=>
              object(Peast\Syntax\Position)#45 (3) {
                ["line":protected]=>
                int(3)
                ["column":protected]=>
                int(1)
                ["index":protected]=>
                int(49)
              }
            }
            ["leadingComments":protected]=>
            array(0) {
            }
            ["trailingComments":protected]=>
            array(0) {
            }
          }
          ["location"]=>
          object(Peast\Syntax\SourceLocation)#21 (2) {
            ["start"]=>
            object(Peast\Syntax\Position)#14 (3) {
              ["line":protected]=>
              int(1)
              ["column":protected]=>
              int(6)
              ["index":protected]=>
              int(6)
            }
            ["end"]=>
            object(Peast\Syntax\Position)#45 (3) {
              ["line":protected]=>
              int(3)
              ["column":protected]=>
              int(1)
              ["index":protected]=>
              int(49)
            }
          }
          ["leadingComments":protected]=>
          array(0) {
          }
          ["trailingComments":protected]=>
          array(0) {
          }
        }
      }
      ["kind":protected]=>
      string(5) "const"
      ["location"]=>
      object(Peast\Syntax\SourceLocation)#52 (2) {
        ["start"]=>
        object(Peast\Syntax\Position)#9 (3) {
          ["line":protected]=>
          int(1)
          ["column":protected]=>
          int(0)
          ["index":protected]=>
          int(0)
        }
        ["end"]=>
        object(Peast\Syntax\Position)#49 (3) {
          ["line":protected]=>
          int(3)
          ["column":protected]=>
          int(2)
          ["index":protected]=>
          int(50)
        }
      }
      ["leadingComments":protected]=>
      array(0) {
      }
      ["trailingComments":protected]=>
      array(0) {
      }
    }
  }
  ["location"]=>
  object(Peast\Syntax\SourceLocation)#11 (2) {
    ["start"]=>
    object(Peast\Syntax\Position)#9 (3) {
      ["line":protected]=>
      int(1)
      ["column":protected]=>
      int(0)
      ["index":protected]=>
      int(0)
    }
    ["end"]=>
    object(Peast\Syntax\Position)#49 (3) {
      ["line":protected]=>
      int(3)
      ["column":protected]=>
      int(2)
      ["index":protected]=>
      int(50)
    }
  }
  ["leadingComments":protected]=>
  array(0) {
  }
  ["trailingComments":protected]=>
  array(0) {
  }
}

@mck89
Copy link
Owner

mck89 commented Jul 11, 2021

I've just fixed this error, the problem wasn't the object itself but the spread operator inside it, the parser was failing too early when consuming it. Thank you for reporting!

@ocean90
Copy link

ocean90 commented Jul 14, 2021

Hi @mck89 thanks for the quick fix. Confirming that it's now working as expected. Are you planning to publish the fix in a patch release?

@mck89
Copy link
Owner

mck89 commented Jul 14, 2021

I've released version 1.13.2 with this bugfix

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

3 participants