Skip to content

Commit a296eab

Browse files
authored
fix: πŸ› syntax error on multiline class constant with comment (#1152)
* fix: πŸ› syntax error on multiline class constant with comment βœ… Closes: #1151 * test: πŸ’ add test for multiline class constant * test: πŸ’ extract doc on test * test: πŸ’ fix test case
1 parent 81991b2 commit a296eab

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

β€Žsrc/parser/class.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ module.exports = {
358358
this.next();
359359
}
360360

361-
if (this.peek() === "=") {
361+
if (this.peekSkipComments() === "=") {
362362
return [false, null];
363363
}
364364

@@ -386,6 +386,21 @@ module.exports = {
386386
return [nullable, type];
387387
},
388388

389+
peekSkipComments: function () {
390+
const lexerState = this.lexer.getState();
391+
let nextToken;
392+
393+
do {
394+
nextToken = this.lexer.lex();
395+
} while (
396+
nextToken === this.tok.T_COMMENT ||
397+
nextToken === this.tok.T_WHITESPACE
398+
);
399+
400+
this.lexer.setState(lexerState);
401+
return nextToken;
402+
},
403+
389404
/*
390405
* reading an interface
391406
* ```ebnf

β€Žtest/snapshot/__snapshots__/classconstant.test.js.snap

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,71 @@ Program {
4949
}
5050
`;
5151

52+
exports[`classconstant multiline declaration with comment 1`] = `
53+
Program {
54+
"children": [
55+
Class {
56+
"attrGroups": [],
57+
"body": [
58+
ClassConstant {
59+
"attrGroups": [],
60+
"constants": [
61+
Constant {
62+
"kind": "constant",
63+
"name": Identifier {
64+
"kind": "identifier",
65+
"name": "CONSTANT",
66+
},
67+
"value": String {
68+
"isDoubleQuote": false,
69+
"kind": "string",
70+
"leadingComments": [
71+
CommentLine {
72+
"kind": "commentline",
73+
"offset": 75,
74+
"value": "// Comment
75+
",
76+
},
77+
],
78+
"raw": "'string'",
79+
"unicode": false,
80+
"value": "string",
81+
},
82+
},
83+
],
84+
"final": false,
85+
"kind": "classconstant",
86+
"nullable": false,
87+
"type": null,
88+
"visibility": "public",
89+
},
90+
],
91+
"extends": null,
92+
"implements": null,
93+
"isAbstract": false,
94+
"isAnonymous": false,
95+
"isFinal": false,
96+
"isReadonly": false,
97+
"kind": "class",
98+
"name": Identifier {
99+
"kind": "identifier",
100+
"name": "Foo",
101+
},
102+
},
103+
],
104+
"comments": [
105+
CommentLine {
106+
"kind": "commentline",
107+
"offset": 75,
108+
"value": "// Comment
109+
",
110+
},
111+
],
112+
"errors": [],
113+
"kind": "program",
114+
}
115+
`;
116+
52117
exports[`classconstant multiple 1`] = `
53118
Program {
54119
"children": [

β€Žtest/snapshot/classconstant.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,19 @@ describe("classconstant", () => {
7575
),
7676
).toThrowErrorMatchingSnapshot();
7777
});
78+
it("multiline declaration with comment", () => {
79+
expect(
80+
parser.parseEval(
81+
`class Foo {
82+
public
83+
const
84+
CONSTANT
85+
// Comment
86+
=
87+
'string';
88+
}`,
89+
{ parser: { version: 803, extractDoc: true } },
90+
),
91+
).toMatchSnapshot();
92+
});
7893
});

0 commit comments

Comments
Β (0)