From 8f1e626a19c36fb2b74edd020d82b16430145716 Mon Sep 17 00:00:00 2001 From: Shuhei Hayashibara Date: Tue, 14 Jan 2025 04:27:06 +0900 Subject: [PATCH 1/4] =?UTF-8?q?fix:=20=F0=9F=90=9B=20syntax=20error=20on?= =?UTF-8?q?=20multiline=20class=20constant=20with=20comment?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ✅ Closes: #1151 --- src/parser/class.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/parser/class.js b/src/parser/class.js index cbc2c370..9a4acc53 100644 --- a/src/parser/class.js +++ b/src/parser/class.js @@ -358,7 +358,7 @@ module.exports = { this.next(); } - if (this.peek() === "=") { + if (this.peekSkipComments() === "=") { return [false, null]; } @@ -386,6 +386,21 @@ module.exports = { return [nullable, type]; }, + peekSkipComments: function () { + const lexerState = this.lexer.getState(); + let nextToken; + + do { + nextToken = this.lexer.lex(); + } while ( + nextToken === this.tok.T_COMMENT || + nextToken === this.tok.T_WHITESPACE + ); + + this.lexer.setState(lexerState); + return nextToken; + }, + /* * reading an interface * ```ebnf From 5d2271b811a774c25cdef499dbc816561e2c017d Mon Sep 17 00:00:00 2001 From: Shuhei Hayashibara Date: Tue, 14 Jan 2025 04:27:55 +0900 Subject: [PATCH 2/4] =?UTF-8?q?test:=20=F0=9F=92=8D=20add=20test=20for=20m?= =?UTF-8?q?ultiline=20class=20constant?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../__snapshots__/classconstant.test.js.snap | 53 +++++++++++++++++++ test/snapshot/classconstant.test.js | 20 +++++++ 2 files changed, 73 insertions(+) diff --git a/test/snapshot/__snapshots__/classconstant.test.js.snap b/test/snapshot/__snapshots__/classconstant.test.js.snap index 743c7138..13ed8ed8 100644 --- a/test/snapshot/__snapshots__/classconstant.test.js.snap +++ b/test/snapshot/__snapshots__/classconstant.test.js.snap @@ -49,6 +49,59 @@ Program { } `; +exports[`classconstant multiline declaration with comment 1`] = ` +Program { + "children": [ + Class { + "attrGroups": [], + "body": [ + ClassConstant { + "attrGroups": [], + "constants": [ + Constant { + "kind": "constant", + "name": Identifier { + "kind": "identifier", + "name": "CONSTANT", + }, + "value": String { + "isDoubleQuote": true, + "kind": "string", + "raw": ""Hello world!"", + "unicode": false, + "value": "Hello world!", + }, + }, + ], + "final": false, + "kind": "classconstant", + "nullable": false, + "type": TypeReference { + "kind": "typereference", + "name": "string", + "raw": "string", + }, + "visibility": "public", + }, + ], + "extends": null, + "implements": null, + "isAbstract": false, + "isAnonymous": false, + "isFinal": false, + "isReadonly": false, + "kind": "class", + "name": Identifier { + "kind": "identifier", + "name": "Foo", + }, + }, + ], + "errors": [], + "kind": "program", +} +`; + exports[`classconstant multiple 1`] = ` Program { "children": [ diff --git a/test/snapshot/classconstant.test.js b/test/snapshot/classconstant.test.js index 287845a7..de5eadb8 100644 --- a/test/snapshot/classconstant.test.js +++ b/test/snapshot/classconstant.test.js @@ -75,4 +75,24 @@ describe("classconstant", () => { ), ).toThrowErrorMatchingSnapshot(); }); + it("multiline declaration with comment", () => { + expect( + parser.parseEval( + `class Foo { + public + // comment 1 + const + // comment 2 + string + // comment 3 + CONSTANT + // comment 4 + = + // comment 5 + "Hello world!"; + }`, + { parser: { version: 803 } }, + ), + ).toMatchSnapshot(); + }); }); From 8cf6b8bbd682d673ddce7acafda5045e548c0557 Mon Sep 17 00:00:00 2001 From: Shuhei Hayashibara Date: Wed, 15 Jan 2025 01:07:38 +0900 Subject: [PATCH 3/4] =?UTF-8?q?test:=20=F0=9F=92=8D=20extract=20doc=20on?= =?UTF-8?q?=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../__snapshots__/classconstant.test.js.snap | 68 +++++++++++++++++++ test/snapshot/classconstant.test.js | 4 +- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/test/snapshot/__snapshots__/classconstant.test.js.snap b/test/snapshot/__snapshots__/classconstant.test.js.snap index 13ed8ed8..267167db 100644 --- a/test/snapshot/__snapshots__/classconstant.test.js.snap +++ b/test/snapshot/__snapshots__/classconstant.test.js.snap @@ -67,6 +67,20 @@ Program { "value": String { "isDoubleQuote": true, "kind": "string", + "leadingComments": [ + CommentLine { + "kind": "commentline", + "offset": 165, + "value": "// comment 4 +", + }, + CommentLine { + "kind": "commentline", + "offset": 200, + "value": "// comment 5 +", + }, + ], "raw": ""Hello world!"", "unicode": false, "value": "Hello world!", @@ -75,9 +89,31 @@ Program { ], "final": false, "kind": "classconstant", + "leadingComments": [ + CommentLine { + "kind": "commentline", + "offset": 122, + "value": "// comment 3 +", + }, + ], "nullable": false, "type": TypeReference { "kind": "typereference", + "leadingComments": [ + CommentLine { + "kind": "commentline", + "offset": 41, + "value": "// comment 1 +", + }, + CommentLine { + "kind": "commentline", + "offset": 81, + "value": "// comment 2 +", + }, + ], "name": "string", "raw": "string", }, @@ -97,6 +133,38 @@ Program { }, }, ], + "comments": [ + CommentLine { + "kind": "commentline", + "offset": 41, + "value": "// comment 1 +", + }, + CommentLine { + "kind": "commentline", + "offset": 81, + "value": "// comment 2 +", + }, + CommentLine { + "kind": "commentline", + "offset": 122, + "value": "// comment 3 +", + }, + CommentLine { + "kind": "commentline", + "offset": 165, + "value": "// comment 4 +", + }, + CommentLine { + "kind": "commentline", + "offset": 200, + "value": "// comment 5 +", + }, + ], "errors": [], "kind": "program", } diff --git a/test/snapshot/classconstant.test.js b/test/snapshot/classconstant.test.js index de5eadb8..acfcdb18 100644 --- a/test/snapshot/classconstant.test.js +++ b/test/snapshot/classconstant.test.js @@ -87,11 +87,11 @@ describe("classconstant", () => { // comment 3 CONSTANT // comment 4 - = + = // comment 5 "Hello world!"; }`, - { parser: { version: 803 } }, + { parser: { version: 803, extractDoc: true } }, ), ).toMatchSnapshot(); }); From b7ff1015b706da51b23b999517b520c454316788 Mon Sep 17 00:00:00 2001 From: Shuhei Hayashibara Date: Wed, 15 Jan 2025 01:52:04 +0900 Subject: [PATCH 4/4] =?UTF-8?q?test:=20=F0=9F=92=8D=20fix=20test=20case?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../__snapshots__/classconstant.test.js.snap | 72 +++---------------- test/snapshot/classconstant.test.js | 15 ++-- 2 files changed, 13 insertions(+), 74 deletions(-) diff --git a/test/snapshot/__snapshots__/classconstant.test.js.snap b/test/snapshot/__snapshots__/classconstant.test.js.snap index 267167db..12c8d5fa 100644 --- a/test/snapshot/__snapshots__/classconstant.test.js.snap +++ b/test/snapshot/__snapshots__/classconstant.test.js.snap @@ -65,58 +65,26 @@ Program { "name": "CONSTANT", }, "value": String { - "isDoubleQuote": true, + "isDoubleQuote": false, "kind": "string", "leadingComments": [ CommentLine { "kind": "commentline", - "offset": 165, - "value": "// comment 4 -", - }, - CommentLine { - "kind": "commentline", - "offset": 200, - "value": "// comment 5 + "offset": 75, + "value": "// Comment ", }, ], - "raw": ""Hello world!"", + "raw": "'string'", "unicode": false, - "value": "Hello world!", + "value": "string", }, }, ], "final": false, "kind": "classconstant", - "leadingComments": [ - CommentLine { - "kind": "commentline", - "offset": 122, - "value": "// comment 3 -", - }, - ], "nullable": false, - "type": TypeReference { - "kind": "typereference", - "leadingComments": [ - CommentLine { - "kind": "commentline", - "offset": 41, - "value": "// comment 1 -", - }, - CommentLine { - "kind": "commentline", - "offset": 81, - "value": "// comment 2 -", - }, - ], - "name": "string", - "raw": "string", - }, + "type": null, "visibility": "public", }, ], @@ -136,32 +104,8 @@ Program { "comments": [ CommentLine { "kind": "commentline", - "offset": 41, - "value": "// comment 1 -", - }, - CommentLine { - "kind": "commentline", - "offset": 81, - "value": "// comment 2 -", - }, - CommentLine { - "kind": "commentline", - "offset": 122, - "value": "// comment 3 -", - }, - CommentLine { - "kind": "commentline", - "offset": 165, - "value": "// comment 4 -", - }, - CommentLine { - "kind": "commentline", - "offset": 200, - "value": "// comment 5 + "offset": 75, + "value": "// Comment ", }, ], diff --git a/test/snapshot/classconstant.test.js b/test/snapshot/classconstant.test.js index acfcdb18..63cbf6b8 100644 --- a/test/snapshot/classconstant.test.js +++ b/test/snapshot/classconstant.test.js @@ -79,17 +79,12 @@ describe("classconstant", () => { expect( parser.parseEval( `class Foo { - public - // comment 1 - const - // comment 2 - string - // comment 3 - CONSTANT - // comment 4 + public + const + CONSTANT + // Comment = - // comment 5 - "Hello world!"; + 'string'; }`, { parser: { version: 803, extractDoc: true } }, ),