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 diff --git a/test/snapshot/__snapshots__/classconstant.test.js.snap b/test/snapshot/__snapshots__/classconstant.test.js.snap index 743c7138..12c8d5fa 100644 --- a/test/snapshot/__snapshots__/classconstant.test.js.snap +++ b/test/snapshot/__snapshots__/classconstant.test.js.snap @@ -49,6 +49,71 @@ 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": false, + "kind": "string", + "leadingComments": [ + CommentLine { + "kind": "commentline", + "offset": 75, + "value": "// Comment +", + }, + ], + "raw": "'string'", + "unicode": false, + "value": "string", + }, + }, + ], + "final": false, + "kind": "classconstant", + "nullable": false, + "type": null, + "visibility": "public", + }, + ], + "extends": null, + "implements": null, + "isAbstract": false, + "isAnonymous": false, + "isFinal": false, + "isReadonly": false, + "kind": "class", + "name": Identifier { + "kind": "identifier", + "name": "Foo", + }, + }, + ], + "comments": [ + CommentLine { + "kind": "commentline", + "offset": 75, + "value": "// Comment +", + }, + ], + "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..63cbf6b8 100644 --- a/test/snapshot/classconstant.test.js +++ b/test/snapshot/classconstant.test.js @@ -75,4 +75,19 @@ describe("classconstant", () => { ), ).toThrowErrorMatchingSnapshot(); }); + it("multiline declaration with comment", () => { + expect( + parser.parseEval( + `class Foo { + public + const + CONSTANT + // Comment + = + 'string'; + }`, + { parser: { version: 803, extractDoc: true } }, + ), + ).toMatchSnapshot(); + }); });