Skip to content

Commit 73c9aef

Browse files
Fix parsing of block comments nested in flow comments (#15062)
* Fix parsing of comments inside flow comments * Fix parsing of block comments nested in flow comments
1 parent cbef14b commit 73c9aef

4 files changed

Lines changed: 132 additions & 17 deletions

File tree

packages/babel-parser/src/plugins/flow/index.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3341,18 +3341,7 @@ export default (superClass: typeof Parser) =>
33413341
return;
33423342
}
33433343

3344-
if (this.state.hasFlowComment) {
3345-
const end = this.input.indexOf("*-/", this.state.pos + 2);
3346-
if (end === -1) {
3347-
throw this.raise(Errors.UnterminatedComment, {
3348-
at: this.state.curPosition(),
3349-
});
3350-
}
3351-
this.state.pos = end + 2 + 3;
3352-
return;
3353-
}
3354-
3355-
return super.skipBlockComment();
3344+
return super.skipBlockComment(this.state.hasFlowComment ? "*-/" : "*/");
33563345
}
33573346

33583347
skipFlowComment(): number | false {

packages/babel-parser/src/tokenizer/index.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,14 @@ export default abstract class Tokenizer extends CommentsParser {
259259
this.getTokenFromCode(this.codePointAtPos(this.state.pos));
260260
}
261261

262-
skipBlockComment(): N.CommentBlock | undefined {
262+
// Skips a block comment, whose end is marked by commentEnd.
263+
// *-/ is used by the Flow plugin, when parsing block comments nested
264+
// inside Flow comments.
265+
skipBlockComment(commentEnd: "*/" | "*-/"): N.CommentBlock | undefined {
263266
let startLoc;
264267
if (!this.isLookahead) startLoc = this.state.curPosition();
265268
const start = this.state.pos;
266-
const end = this.input.indexOf("*/", start + 2);
269+
const end = this.input.indexOf(commentEnd, start + 2);
267270
if (end === -1) {
268271
// We have to call this again here because startLoc may not be set...
269272
// This seems to be for performance reasons:
@@ -273,7 +276,7 @@ export default abstract class Tokenizer extends CommentsParser {
273276
});
274277
}
275278

276-
this.state.pos = end + 2;
279+
this.state.pos = end + commentEnd.length;
277280
lineBreakG.lastIndex = start + 2;
278281
while (lineBreakG.test(this.input) && lineBreakG.lastIndex <= end) {
279282
++this.state.curLine;
@@ -289,7 +292,7 @@ export default abstract class Tokenizer extends CommentsParser {
289292
type: "CommentBlock",
290293
value: this.input.slice(start + 2, end),
291294
start,
292-
end: end + 2,
295+
end: end + commentEnd.length,
293296
loc: new SourceLocation(startLoc, this.state.curPosition()),
294297
};
295298
if (this.options.tokens) this.pushToken(comment);
@@ -358,7 +361,7 @@ export default abstract class Tokenizer extends CommentsParser {
358361
case charCodes.slash:
359362
switch (this.input.charCodeAt(this.state.pos + 1)) {
360363
case charCodes.asterisk: {
361-
const comment = this.skipBlockComment();
364+
const comment = this.skipBlockComment("*/");
362365
if (comment !== undefined) {
363366
this.addComment(comment);
364367
if (this.options.attachComment) comments.push(comment);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
let data /*: /* comment *-/T */;
2+
3+
const c = (data/*: /* this is an object *-/ Object */) => {};
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
{
2+
"type": "File",
3+
"start":0,"end":95,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":61,"index":95}},
4+
"program": {
5+
"type": "Program",
6+
"start":0,"end":95,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":61,"index":95}},
7+
"sourceType": "module",
8+
"interpreter": null,
9+
"body": [
10+
{
11+
"type": "VariableDeclaration",
12+
"start":0,"end":32,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":32,"index":32}},
13+
"declarations": [
14+
{
15+
"type": "VariableDeclarator",
16+
"start":4,"end":28,"loc":{"start":{"line":1,"column":4,"index":4},"end":{"line":1,"column":28,"index":28}},
17+
"id": {
18+
"type": "Identifier",
19+
"start":4,"end":28,"loc":{"start":{"line":1,"column":4,"index":4},"end":{"line":1,"column":28,"index":28},"identifierName":"data"},
20+
"name": "data",
21+
"typeAnnotation": {
22+
"type": "TypeAnnotation",
23+
"start":11,"end":28,"loc":{"start":{"line":1,"column":11,"index":11},"end":{"line":1,"column":28,"index":28}},
24+
"typeAnnotation": {
25+
"type": "GenericTypeAnnotation",
26+
"start":27,"end":28,"loc":{"start":{"line":1,"column":27,"index":27},"end":{"line":1,"column":28,"index":28}},
27+
"typeParameters": null,
28+
"id": {
29+
"type": "Identifier",
30+
"start":27,"end":28,"loc":{"start":{"line":1,"column":27,"index":27},"end":{"line":1,"column":28,"index":28},"identifierName":"T"},
31+
"name": "T"
32+
},
33+
"leadingComments": [
34+
{
35+
"type": "CommentBlock",
36+
"value": " comment ",
37+
"start":13,"end":27,"loc":{"start":{"line":1,"column":13,"index":13},"end":{"line":1,"column":27,"index":27}}
38+
}
39+
]
40+
}
41+
}
42+
},
43+
"init": null
44+
}
45+
],
46+
"kind": "let"
47+
},
48+
{
49+
"type": "VariableDeclaration",
50+
"start":34,"end":95,"loc":{"start":{"line":3,"column":0,"index":34},"end":{"line":3,"column":61,"index":95}},
51+
"declarations": [
52+
{
53+
"type": "VariableDeclarator",
54+
"start":40,"end":94,"loc":{"start":{"line":3,"column":6,"index":40},"end":{"line":3,"column":60,"index":94}},
55+
"id": {
56+
"type": "Identifier",
57+
"start":40,"end":41,"loc":{"start":{"line":3,"column":6,"index":40},"end":{"line":3,"column":7,"index":41},"identifierName":"c"},
58+
"name": "c"
59+
},
60+
"init": {
61+
"type": "ArrowFunctionExpression",
62+
"start":44,"end":94,"loc":{"start":{"line":3,"column":10,"index":44},"end":{"line":3,"column":60,"index":94}},
63+
"id": null,
64+
"generator": false,
65+
"async": false,
66+
"params": [
67+
{
68+
"type": "Identifier",
69+
"start":45,"end":84,"loc":{"start":{"line":3,"column":11,"index":45},"end":{"line":3,"column":50,"index":84},"identifierName":"data"},
70+
"name": "data",
71+
"typeAnnotation": {
72+
"type": "TypeAnnotation",
73+
"start":51,"end":84,"loc":{"start":{"line":3,"column":17,"index":51},"end":{"line":3,"column":50,"index":84}},
74+
"typeAnnotation": {
75+
"type": "GenericTypeAnnotation",
76+
"start":78,"end":84,"loc":{"start":{"line":3,"column":44,"index":78},"end":{"line":3,"column":50,"index":84}},
77+
"typeParameters": null,
78+
"id": {
79+
"type": "Identifier",
80+
"start":78,"end":84,"loc":{"start":{"line":3,"column":44,"index":78},"end":{"line":3,"column":50,"index":84},"identifierName":"Object"},
81+
"name": "Object"
82+
},
83+
"leadingComments": [
84+
{
85+
"type": "CommentBlock",
86+
"value": " this is an object ",
87+
"start":53,"end":77,"loc":{"start":{"line":3,"column":19,"index":53},"end":{"line":3,"column":43,"index":77}}
88+
}
89+
]
90+
}
91+
}
92+
}
93+
],
94+
"body": {
95+
"type": "BlockStatement",
96+
"start":92,"end":94,"loc":{"start":{"line":3,"column":58,"index":92},"end":{"line":3,"column":60,"index":94}},
97+
"body": [],
98+
"directives": []
99+
}
100+
}
101+
}
102+
],
103+
"kind": "const"
104+
}
105+
],
106+
"directives": []
107+
},
108+
"comments": [
109+
{
110+
"type": "CommentBlock",
111+
"value": " comment ",
112+
"start":13,"end":27,"loc":{"start":{"line":1,"column":13,"index":13},"end":{"line":1,"column":27,"index":27}}
113+
},
114+
{
115+
"type": "CommentBlock",
116+
"value": " this is an object ",
117+
"start":53,"end":77,"loc":{"start":{"line":3,"column":19,"index":53},"end":{"line":3,"column":43,"index":77}}
118+
}
119+
]
120+
}

0 commit comments

Comments
 (0)