From 80eb9460679188f76166dd25a9a2666b8ff129aa Mon Sep 17 00:00:00 2001 From: jason lim <50891910+Sxxov@users.noreply.github.com> Date: Mon, 11 Sep 2023 13:49:31 +0800 Subject: [PATCH] Fix `?->` with succeeding PHP keyword (#1124) * Use same path in `?->` for eating properties as `->` * Add test for `?->` with reserved keyword --- src/lexer/tokens.js | 3 ++- .../snapshot/__snapshots__/lexer.test.js.snap | 24 +++++++++++++++++++ test/snapshot/lexer.test.js | 4 ++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/lexer/tokens.js b/src/lexer/tokens.js index c57c0b3ca..217524ca8 100644 --- a/src/lexer/tokens.js +++ b/src/lexer/tokens.js @@ -238,7 +238,8 @@ module.exports = { this._input[this.offset] === "-" && this._input[this.offset + 1] === ">" ) { - this.consume(2); + this.consume(1); + this.begin("ST_LOOKING_FOR_PROPERTY").input(); return this.tok.T_NULLSAFE_OBJECT_OPERATOR; } return "?"; diff --git a/test/snapshot/__snapshots__/lexer.test.js.snap b/test/snapshot/__snapshots__/lexer.test.js.snap index ced4c81eb..e71bb1665 100644 --- a/test/snapshot/__snapshots__/lexer.test.js.snap +++ b/test/snapshot/__snapshots__/lexer.test.js.snap @@ -125,6 +125,30 @@ exports[`Test lexer test #148 - sensitive lexer 1`] = ` ] `; +exports[`Test lexer test #1003 - null-safe operator with reserved keyword 1`] = ` +Program { + "children": [ + ExpressionStatement { + "expression": NullSafePropertyLookup { + "kind": "nullsafepropertylookup", + "offset": Identifier { + "kind": "identifier", + "name": "class", + }, + "what": Variable { + "curly": false, + "kind": "variable", + "name": "a", + }, + }, + "kind": "expressionstatement", + }, + ], + "errors": [], + "kind": "program", +} +`; + exports[`Test lexer test comments 1`] = ` Program { "children": [ diff --git a/test/snapshot/lexer.test.js b/test/snapshot/lexer.test.js index 2888fdef8..ab6b3155b 100644 --- a/test/snapshot/lexer.test.js +++ b/test/snapshot/lexer.test.js @@ -81,4 +81,8 @@ describe("Test lexer", function () { it("test #148 - sensitive lexer", function () { expect(parser.tokenGetAll(" list;")).toMatchSnapshot(); }); + + it("test #1003 - null-safe operator with reserved keyword", function () { + expect(parser.parseCode("class;")).toMatchSnapshot(); + }); });