From 39b527c65927db569ab29c4c522b90d2378ce749 Mon Sep 17 00:00:00 2001 From: Christian Zosel Date: Sat, 27 Mar 2021 15:10:32 +0100 Subject: [PATCH] refactor: reset state instead of try / catch --- src/parser/function.js | 26 +++++++++---------- test/snapshot/__snapshots__/call.test.js.snap | 14 ++++++++++ 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/parser/function.js b/src/parser/function.js index 528c95867..c05260e59 100644 --- a/src/parser/function.js +++ b/src/parser/function.js @@ -290,20 +290,20 @@ module.exports = { if (this.token === this.tok.T_ELLIPSIS) { return this.node("variadic")(this.next().read_expr()); } - const name = this.text(); - let res; - try { - res = this.read_expr(); - } catch (e) { - // happens if named argument name equals keyword name, e.g. - // foo(array: $a) - `array` is not a valid expr - this.expect(":") && this.next(); - return this.node("namedargument")(name, this.read_expr()); - } - if (this.token === ":") { - return this.node("namedargument")(name, this.next().read_expr()); + if ( + this.token === this.tok.T_STRING || + Object.values(this.lexer.keywords).includes(this.token) + ) { + const backup = [this.token, this.lexer.getState()]; + const name = this.text(); + this.next(); + if (this.token === ":") { + return this.node("namedargument")(name, this.next().read_expr()); + } + this.lexer.tokens.push(backup); + this.next(); } - return res; + return this.read_expr(); }, /** * read type hinting diff --git a/test/snapshot/__snapshots__/call.test.js.snap b/test/snapshot/__snapshots__/call.test.js.snap index 81669f7c1..94f4448d8 100644 --- a/test/snapshot/__snapshots__/call.test.js.snap +++ b/test/snapshot/__snapshots__/call.test.js.snap @@ -135,6 +135,14 @@ Program { }, ], "kind": "array", + "leadingComments": Array [ + CommentLine { + "kind": "commentline", + "offset": 10, + "value": "// comment +", + }, + ], "shortForm": false, }, ], @@ -153,6 +161,12 @@ Program { "kind": "commentline", "offset": 10, "value": "// comment +", + }, + CommentLine { + "kind": "commentline", + "offset": 10, + "value": "// comment ", }, ],