From 40f3d52c5525aadf4b6ea0b9a8c7dafb2a55ab3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Wed, 19 Oct 2022 19:38:26 +0200 Subject: [PATCH 1/2] Fix parsing of comments inside flow comments --- .../babel-parser/src/plugins/flow/index.ts | 2 +- .../14-code-after-nested-comment/input.js | 3 + .../14-code-after-nested-comment/output.json | 94 +++++++++++++++++++ 3 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 packages/babel-parser/test/fixtures/flow/comment/14-code-after-nested-comment/input.js create mode 100644 packages/babel-parser/test/fixtures/flow/comment/14-code-after-nested-comment/output.json diff --git a/packages/babel-parser/src/plugins/flow/index.ts b/packages/babel-parser/src/plugins/flow/index.ts index 4fb506daa9b9..3645e90d3571 100644 --- a/packages/babel-parser/src/plugins/flow/index.ts +++ b/packages/babel-parser/src/plugins/flow/index.ts @@ -3348,7 +3348,7 @@ export default (superClass: typeof Parser) => at: this.state.curPosition(), }); } - this.state.pos = end + 2 + 3; + this.state.pos = end + 3; return; } diff --git a/packages/babel-parser/test/fixtures/flow/comment/14-code-after-nested-comment/input.js b/packages/babel-parser/test/fixtures/flow/comment/14-code-after-nested-comment/input.js new file mode 100644 index 000000000000..de1f4e642526 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/comment/14-code-after-nested-comment/input.js @@ -0,0 +1,3 @@ +let data /*: /* comment *-/T */; + +const c = (data/*: /* this is an object *-/ Object */) => {}; diff --git a/packages/babel-parser/test/fixtures/flow/comment/14-code-after-nested-comment/output.json b/packages/babel-parser/test/fixtures/flow/comment/14-code-after-nested-comment/output.json new file mode 100644 index 000000000000..b07f69891446 --- /dev/null +++ b/packages/babel-parser/test/fixtures/flow/comment/14-code-after-nested-comment/output.json @@ -0,0 +1,94 @@ +{ + "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" + } + } + } + }, + "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" + } + } + } + } + ], + "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": [] + } +} From 8f737c252268731f8172898cc3af8f013ded74cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Wed, 19 Oct 2022 21:45:23 +0200 Subject: [PATCH 2/2] Fix parsing of block comments nested in flow comments --- .../babel-parser/src/plugins/flow/index.ts | 13 +------- packages/babel-parser/src/tokenizer/index.ts | 13 +++++--- .../14-code-after-nested-comment/output.json | 32 +++++++++++++++++-- 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/packages/babel-parser/src/plugins/flow/index.ts b/packages/babel-parser/src/plugins/flow/index.ts index 3645e90d3571..62fd136c83ef 100644 --- a/packages/babel-parser/src/plugins/flow/index.ts +++ b/packages/babel-parser/src/plugins/flow/index.ts @@ -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 + 3; - return; - } - - return super.skipBlockComment(); + return super.skipBlockComment(this.state.hasFlowComment ? "*-/" : "*/"); } skipFlowComment(): number | false { diff --git a/packages/babel-parser/src/tokenizer/index.ts b/packages/babel-parser/src/tokenizer/index.ts index 891a63b78679..5ed7e02f7457 100644 --- a/packages/babel-parser/src/tokenizer/index.ts +++ b/packages/babel-parser/src/tokenizer/index.ts @@ -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: @@ -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; @@ -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); @@ -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); diff --git a/packages/babel-parser/test/fixtures/flow/comment/14-code-after-nested-comment/output.json b/packages/babel-parser/test/fixtures/flow/comment/14-code-after-nested-comment/output.json index b07f69891446..b4b9db14f9e3 100644 --- a/packages/babel-parser/test/fixtures/flow/comment/14-code-after-nested-comment/output.json +++ b/packages/babel-parser/test/fixtures/flow/comment/14-code-after-nested-comment/output.json @@ -29,7 +29,14 @@ "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}} + } + ] } } }, @@ -72,7 +79,14 @@ "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}} + } + ] } } } @@ -90,5 +104,17 @@ } ], "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}} + } + ] }