Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of block comments nested in flow comments #15062

Merged
merged 2 commits into from Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 1 addition & 12 deletions packages/babel-parser/src/plugins/flow/index.ts
Expand Up @@ -3341,18 +3341,7 @@ export default (superClass: typeof Parser) =>
return;
}

if (this.state.hasFlowComment) {
const end = this.input.indexOf("*-/", this.state.pos + 2);
if (end === -1) {
throw this.raise(Errors.UnterminatedComment, {
at: this.state.curPosition(),
});
}
this.state.pos = end + 2 + 3;
return;
}

return super.skipBlockComment();
return super.skipBlockComment(this.state.hasFlowComment ? "*-/" : "*/");
}

skipFlowComment(): number | false {
Expand Down
13 changes: 8 additions & 5 deletions packages/babel-parser/src/tokenizer/index.ts
Expand Up @@ -259,11 +259,14 @@ export default abstract class Tokenizer extends CommentsParser {
this.getTokenFromCode(this.codePointAtPos(this.state.pos));
}

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

this.state.pos = end + 2;
this.state.pos = end + commentEnd.length;
lineBreakG.lastIndex = start + 2;
while (lineBreakG.test(this.input) && lineBreakG.lastIndex <= end) {
++this.state.curLine;
Expand All @@ -289,7 +292,7 @@ export default abstract class Tokenizer extends CommentsParser {
type: "CommentBlock",
value: this.input.slice(start + 2, end),
start,
end: end + 2,
end: end + commentEnd.length,
loc: new SourceLocation(startLoc, this.state.curPosition()),
};
if (this.options.tokens) this.pushToken(comment);
Expand Down Expand Up @@ -358,7 +361,7 @@ export default abstract class Tokenizer extends CommentsParser {
case charCodes.slash:
switch (this.input.charCodeAt(this.state.pos + 1)) {
case charCodes.asterisk: {
const comment = this.skipBlockComment();
const comment = this.skipBlockComment("*/");
if (comment !== undefined) {
this.addComment(comment);
if (this.options.attachComment) comments.push(comment);
Expand Down
@@ -0,0 +1,3 @@
let data /*: /* comment *-/T */;

const c = (data/*: /* this is an object *-/ Object */) => {};
@@ -0,0 +1,120 @@
{
"type": "File",
"start":0,"end":95,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":61,"index":95}},
"program": {
"type": "Program",
"start":0,"end":95,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":61,"index":95}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",
"start":0,"end":32,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":32,"index":32}},
"declarations": [
{
"type": "VariableDeclarator",
"start":4,"end":28,"loc":{"start":{"line":1,"column":4,"index":4},"end":{"line":1,"column":28,"index":28}},
"id": {
"type": "Identifier",
"start":4,"end":28,"loc":{"start":{"line":1,"column":4,"index":4},"end":{"line":1,"column":28,"index":28},"identifierName":"data"},
"name": "data",
"typeAnnotation": {
"type": "TypeAnnotation",
"start":11,"end":28,"loc":{"start":{"line":1,"column":11,"index":11},"end":{"line":1,"column":28,"index":28}},
"typeAnnotation": {
"type": "GenericTypeAnnotation",
"start":27,"end":28,"loc":{"start":{"line":1,"column":27,"index":27},"end":{"line":1,"column":28,"index":28}},
"typeParameters": null,
"id": {
"type": "Identifier",
"start":27,"end":28,"loc":{"start":{"line":1,"column":27,"index":27},"end":{"line":1,"column":28,"index":28},"identifierName":"T"},
"name": "T"
},
"leadingComments": [
{
"type": "CommentBlock",
"value": " comment ",
"start":13,"end":27,"loc":{"start":{"line":1,"column":13,"index":13},"end":{"line":1,"column":27,"index":27}}
}
]
}
}
},
"init": null
}
],
"kind": "let"
},
{
"type": "VariableDeclaration",
"start":34,"end":95,"loc":{"start":{"line":3,"column":0,"index":34},"end":{"line":3,"column":61,"index":95}},
"declarations": [
{
"type": "VariableDeclarator",
"start":40,"end":94,"loc":{"start":{"line":3,"column":6,"index":40},"end":{"line":3,"column":60,"index":94}},
"id": {
"type": "Identifier",
"start":40,"end":41,"loc":{"start":{"line":3,"column":6,"index":40},"end":{"line":3,"column":7,"index":41},"identifierName":"c"},
"name": "c"
},
"init": {
"type": "ArrowFunctionExpression",
"start":44,"end":94,"loc":{"start":{"line":3,"column":10,"index":44},"end":{"line":3,"column":60,"index":94}},
"id": null,
"generator": false,
"async": false,
"params": [
{
"type": "Identifier",
"start":45,"end":84,"loc":{"start":{"line":3,"column":11,"index":45},"end":{"line":3,"column":50,"index":84},"identifierName":"data"},
"name": "data",
"typeAnnotation": {
"type": "TypeAnnotation",
"start":51,"end":84,"loc":{"start":{"line":3,"column":17,"index":51},"end":{"line":3,"column":50,"index":84}},
"typeAnnotation": {
"type": "GenericTypeAnnotation",
"start":78,"end":84,"loc":{"start":{"line":3,"column":44,"index":78},"end":{"line":3,"column":50,"index":84}},
"typeParameters": null,
"id": {
"type": "Identifier",
"start":78,"end":84,"loc":{"start":{"line":3,"column":44,"index":78},"end":{"line":3,"column":50,"index":84},"identifierName":"Object"},
"name": "Object"
},
"leadingComments": [
{
"type": "CommentBlock",
"value": " this is an object ",
"start":53,"end":77,"loc":{"start":{"line":3,"column":19,"index":53},"end":{"line":3,"column":43,"index":77}}
}
]
}
}
}
],
"body": {
"type": "BlockStatement",
"start":92,"end":94,"loc":{"start":{"line":3,"column":58,"index":92},"end":{"line":3,"column":60,"index":94}},
"body": [],
"directives": []
}
}
}
],
"kind": "const"
}
],
"directives": []
},
"comments": [
{
"type": "CommentBlock",
"value": " comment ",
"start":13,"end":27,"loc":{"start":{"line":1,"column":13,"index":13},"end":{"line":1,"column":27,"index":27}}
},
{
"type": "CommentBlock",
"value": " this is an object ",
"start":53,"end":77,"loc":{"start":{"line":3,"column":19,"index":53},"end":{"line":3,"column":43,"index":77}}
}
]
}
nicolo-ribaudo marked this conversation as resolved.
Show resolved Hide resolved