From 64cc20e49958d9e35bdc83246c4e9cbc9fda974e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 28 Jun 2021 11:53:19 -0400 Subject: [PATCH 01/35] refactor: inline pushComment --- packages/babel-parser/src/parser/comments.js | 3 +- .../babel-parser/src/plugins/flow/index.js | 4 +- packages/babel-parser/src/tokenizer/index.js | 76 +++++++++---------- packages/babel-parser/src/types.js | 12 ++- 4 files changed, 48 insertions(+), 47 deletions(-) diff --git a/packages/babel-parser/src/parser/comments.js b/packages/babel-parser/src/parser/comments.js index c990c9e0e8a9..48c47cd6a2dc 100644 --- a/packages/babel-parser/src/parser/comments.js +++ b/packages/babel-parser/src/parser/comments.js @@ -34,6 +34,7 @@ function last(stack: $ReadOnlyArray): T { export default class CommentsParser extends BaseParser { addComment(comment: Comment): void { if (this.filename) comment.loc.filename = this.filename; + this.state.comments.push(comment); this.state.trailingComments.push(comment); this.state.leadingComments.push(comment); } @@ -101,8 +102,6 @@ export default class CommentsParser extends BaseParser { } processComment(node: Node): void { - if (node.type === "Program" && node.body.length > 0) return; - const stack = this.state.commentStack; let firstChild, lastChild, trailingComments, i, j; diff --git a/packages/babel-parser/src/plugins/flow/index.js b/packages/babel-parser/src/plugins/flow/index.js index 3bf6f4082d7e..d27aaaa0777c 100644 --- a/packages/babel-parser/src/plugins/flow/index.js +++ b/packages/babel-parser/src/plugins/flow/index.js @@ -3212,7 +3212,7 @@ export default (superClass: Class): Class => return fileNode; } - skipBlockComment(): void { + skipBlockComment(): N.CommentBlock | void { if (this.hasPlugin("flowComments") && this.skipFlowComment()) { if (this.state.hasFlowComment) { this.unexpected(null, FlowErrors.NestedFlowComment); @@ -3232,7 +3232,7 @@ export default (superClass: Class): Class => return; } - super.skipBlockComment(); + return super.skipBlockComment(); } skipFlowComment(): number | boolean { diff --git a/packages/babel-parser/src/tokenizer/index.js b/packages/babel-parser/src/tokenizer/index.js index c1c9848b424d..0e1da5a29298 100644 --- a/packages/babel-parser/src/tokenizer/index.js +++ b/packages/babel-parser/src/tokenizer/index.js @@ -304,28 +304,7 @@ export default class Tokenizer extends ParserErrors { } } - pushComment( - block: boolean, - text: string, - start: number, - end: number, - startLoc: Position, - endLoc: Position, - ): void { - const comment = { - type: block ? "CommentBlock" : "CommentLine", - value: text, - start: start, - end: end, - loc: new SourceLocation(startLoc, endLoc), - }; - - if (this.options.tokens) this.pushToken(comment); - this.state.comments.push(comment); - this.addComment(comment); - } - - skipBlockComment(): void { + skipBlockComment(): N.CommentBlock | void { let startLoc; if (!this.isLookahead) startLoc = this.state.curPosition(); const start = this.state.pos; @@ -348,17 +327,19 @@ export default class Tokenizer extends ParserErrors { if (this.isLookahead) return; /*:: invariant(startLoc) */ - this.pushComment( - true, - this.input.slice(start + 2, end), - start, - this.state.pos, - startLoc, - this.state.curPosition(), - ); + const value = this.input.slice(start + 2, end); + const comment = { + type: "CommentBlock", + value: value, + start: start, + end: end + 2, + loc: new SourceLocation(startLoc, this.state.curPosition()), + }; + if (this.options.tokens) this.pushToken(comment); + return comment; } - skipLineComment(startSkip: number): void { + skipLineComment(startSkip: number): N.CommentLine | void { const start = this.state.pos; let startLoc; if (!this.isLookahead) startLoc = this.state.curPosition(); @@ -374,20 +355,25 @@ export default class Tokenizer extends ParserErrors { if (this.isLookahead) return; /*:: invariant(startLoc) */ - this.pushComment( - false, - this.input.slice(start + startSkip, this.state.pos), + const end = this.state.pos; + const value = this.input.slice(start + startSkip, end); + + const comment = { + type: "CommentLine", + value, start, - this.state.pos, - startLoc, - this.state.curPosition(), - ); + end, + loc: new SourceLocation(startLoc, this.state.curPosition()), + }; + if (this.options.tokens) this.pushToken(comment); + return comment; } // Called at the start of the parse and after every token. Skips // whitespace and comments, and. skipSpace(): void { + const comments = []; loop: while (this.state.pos < this.length) { const ch = this.input.charCodeAt(this.state.pos); switch (ch) { @@ -414,11 +400,11 @@ export default class Tokenizer extends ParserErrors { case charCodes.slash: switch (this.input.charCodeAt(this.state.pos + 1)) { case charCodes.asterisk: - this.skipBlockComment(); + comments.push(this.skipBlockComment()); break; case charCodes.slash: - this.skipLineComment(2); + comments.push(this.skipLineComment(2)); break; default: @@ -434,6 +420,10 @@ export default class Tokenizer extends ParserErrors { } } } + for (let i = 0; i < comments.length; i++) { + const comment = comments[i]; + if (comment !== undefined) this.addComment(comment); + } } // Called at the end of every token. Sets `end`, `val`, and @@ -668,7 +658,8 @@ export default class Tokenizer extends ParserErrors { (this.state.lastTokEnd === 0 || this.hasPrecedingLineBreak()) ) { // A `-->` line comment - this.skipLineComment(3); + const comment = this.skipLineComment(3); + if (comment !== undefined) this.addComment(comment); this.skipSpace(); this.nextToken(); return; @@ -711,7 +702,8 @@ export default class Tokenizer extends ParserErrors { this.input.charCodeAt(this.state.pos + 3) === charCodes.dash ) { // `` line comment + const whitespaceStart = this.state.lastTokEnd; const comment = this.skipLineComment(3); - if (comment !== undefined) this.addComment(comment); - this.skipSpace(); + const comments = []; + if (comment !== undefined) { + this.addComment(comment); + comments.push(comment); + } + this.skipSpace(comments, whitespaceStart); this.nextToken(); return; } @@ -702,9 +725,14 @@ export default class Tokenizer extends ParserErrors { this.input.charCodeAt(this.state.pos + 3) === charCodes.dash ) { // `` line comment + const comment = this.skipLineComment(3); + if (comment !== undefined) { + this.addComment(comment); + comments.push(comment); + } + } else { + break loop; + } + } else if (ch === charCodes.lessThan && !this.inModule) { + const pos = this.state.pos; + if ( + this.input.charCodeAt(pos + 1) === charCodes.exclamationMark && + this.input.charCodeAt(pos + 2) === charCodes.dash && + this.input.charCodeAt(pos + 3) === charCodes.dash + ) { + // `` line comment - const whitespaceStart = this.state.lastTokEnd; - const comment = this.skipLineComment(3); - const comments = []; - if (comment !== undefined) { - this.addComment(comment); - comments.push(comment); - } - this.skipSpace(comments, whitespaceStart); - this.nextToken(); - return; - } this.finishOp(tt.incDec, 2); return; } @@ -717,26 +733,6 @@ export default class Tokenizer extends ParserErrors { return; } - if ( - next === charCodes.exclamationMark && - code === charCodes.lessThan && - !this.inModule && - this.input.charCodeAt(this.state.pos + 2) === charCodes.dash && - this.input.charCodeAt(this.state.pos + 3) === charCodes.dash - ) { - // `` HTML close comment. For example, the following snippet + +```js +a// 1 +/* 2 */ + + +2; +``` + +have two comment whitespaces + +```jsonc +// for `// 1\n/* 2 */ ` +{ + start: 1, // position of '/' + end: 15, // position of '+' + comments: [ + CommentLine { start: 1, end: 5}, + CommentBlock { start: 6, end: 13 } + ], + leadingNode: Identifier("a"), + trailingNode: null, + containerNode: BinaryExpression, +} +``` + +and + +```jsonc +// for ` \n` +{ + start: 16, // position of ' ' after '+' + end: 28, // position of '2' + comments: [ + CommentLine { start: 17, end: 23}, + CommentBlock { start: 24, end: 27 } + ], + leadingNode: null, + trailingNode: NumericLiteral(2), + containerNode: BinaryExpression, +} +``` + +Given a program source, the set of all the comment whitespaces has the following properties: + +**Nonemptiness** (P1): For every `w` of comment whitespaces, `w` satisifies + +``` +w.start < w.end +``` + +**Isolation** (P2): There must not exist any pair of comment whitespaces `w1` and `w2` such that + +``` +w1.start ≤ w2.start ≤ w1.end +``` + +**Completeness** (P3): For every comment AST node `c`, there must exist a comment whitespace `w`, such that + +``` +w.start ≤ c.start < c.end ≤ w.end +``` + +We can also say `w` encompasses `c`. + +**Monotonicity** (Corollary from P1 and P2): Given a list of comment whitespaces orderred by `start`, denoted by `{ w1, w2, ... w_n }`, they must satisify + +``` +w1.start < w1.end < w2.start < w2.end < ... < w_n.start < w_n.end +``` + +For any given comment whitespace `w` and an AST node `n`, we can define the following relationships: + +1. `n` is the _leading node_ of `w` iff `n.end = w.start` +2. `n` is the _trailing node_ of `w` iff `n.start = w.end` +3. `n` is the _containing node_ of `w` iff for all AST nodes `N` satisfying `N.start < w.start < w.end < N.end`, the following proposition is true: + +``` +N.start ≤ n.start < w.start < w.end < n.end ≤ N.end +``` + +Note that the relationship from `w` to `n` is _not_ injective. In other words, a comment whitespace can have multiple leading nodes, trailing nodes, and/or containing nodes. To address this issue we can define the extrema of the set of related ast nodes. + +1. Outermost leading/trailing node: `n` is the _outermost leading/trailing node_ of `w` iff for every other leading/trailing node `N` of `w`, `N` is a descendant of `n` +2. Innermost containing node: `n` is the _innermost containing node_ of `w` iff for every other containing node `N` of `w`, `n` is a descendant of `N` + +For any given comment `c` and AST node `n`, now we can (in)formally define leading comments, trailing comments and inner comments: + +**Leading Comment**: `c` is one of leading comments of `n` iff there exist a comment whitespace `w`, such that `n` is the outermost trailing node of `w` and `w` encompasses `c` + +**Trailing Comment**: `c` is one of trailing comments of `n` iff there exist a comment whitespace `w`, such that `n` is the outermost leading node of `w` and `w` encompasses `c` + +**Inner Comment**: `c` is one of inner comments of `n` iff + +1. there exist a comment whitespace `w`, such that `n` is the innermost containing node of `w` and `w` encompasses `c`. +2. there does not exist a comment whitespace `w`, such that `n` is the outermost leading or trailing node of `w and `w`encompasses`c`. + +The Isolation (P2) of a comment whitespace gaurantees that if two comments `c1`, `c2` belongs to the leading/trailing comments of `n`, `c1` and `c2` must be encompassed by the same comment whitespace `w`. This property simplifies classification of leading/trailing because we can now mark a group of comments instead of checking every comments under the same comment whitespace. + +Note that Babel parser marks certain inner comments after a trailing comma of a list structures to be the trailing comments of the last element in that list. (https://github.com/babel/babel/pull/10369) This behaviour can be considered as conpensation due to lack of a `TrailingCommaElement` AST structure to which a comment can be attached. Although this PR implements such behaviour, we will not be discussing it in the design section. + +### Construct Comment Whitespace + +We construct the comment whitespace in `Tokenizer#skipSpace` of `packages/babel-parser/src/tokenizer/index.js`, after we exit from the skip loop, we collect the `comments`, mark the location info and push to `parser.state.commentStack`. In this PR we also merge the parsing of `HTMLOpenComment` and `HTMLCloseComment` to `skipSpace`. + +### Attaching Nodes to Comment Whitespace + +For every finished AST node invoked from `parser#finishNode`. Before an AST node is finished, the whitespace token have been read from `tokenizer#next()`, so if this node has trailing comments, it must be the `leadingNode` of the last element in `commentStack`. + +Note that the `leadingNode` will be updated by subsequent `finishNode()` calls invoked at the same position. The last `finishNode()` call is the winner, which is exactly the _outermost_ leading node that we are interested. Likewise for `trailingNode`. + +Then we iterate `state.commentStack` reversely. we mark `trailingNode` when `comment.end = node.start`, mark `containingNode` when it is not defined, so here the first `finishNode()` is the winner, which is exactly the _innermost_ containing node. + +After we set the containing node, we can assign comments to related node, since the nature of a recursive descending parser requires that when `containingNode` is finished, its `leadingNode` and `trailingNode` must have been parsed\*, so the related node stops being updated by `processComment`. + +\* Technically this is not always true because we have `estree` plugins invokes `finishNodeAt` at a different tokenizer location. However, since most `estree` users are using `@babel/eslint-parser`, which removes the attached comment. So we are good here. + +### Finalize comment whitespaces + +In this step we attach the comments and do the trailing comma adjustments. Note that an extra routine `finalizeRemainingComments` is provided for `parseExpression`, which may not have opportunity to finalize comments which is added to the leading/trailing of the top level Expression node. From c3b2feaf4cdc2ee5d3955ad0a94d51a6a18fa672 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Fri, 2 Jul 2021 10:59:34 -0400 Subject: [PATCH 26/35] misc fix --- packages/babel-parser/ast/comment-attachment.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/babel-parser/ast/comment-attachment.md b/packages/babel-parser/ast/comment-attachment.md index a08ac8745bf5..f5aafdf12355 100644 --- a/packages/babel-parser/ast/comment-attachment.md +++ b/packages/babel-parser/ast/comment-attachment.md @@ -42,7 +42,7 @@ and end: 28, // position of '2' comments: [ CommentLine { start: 17, end: 23}, - CommentBlock { start: 24, end: 27 } + CommentLine { start: 24, end: 27 } ], leadingNode: null, trailingNode: NumericLiteral(2), @@ -72,7 +72,7 @@ w.start ≤ c.start < c.end ≤ w.end We can also say `w` encompasses `c`. -**Monotonicity** (Corollary from P1 and P2): Given a list of comment whitespaces orderred by `start`, denoted by `{ w1, w2, ... w_n }`, they must satisify +**Monotonicity** (Corollary from P1 and P2): Given a non-empty list of comment whitespaces orderred by `start`, denoted by `{ w1, w2, ... w_n }`, they must satisify ``` w1.start < w1.end < w2.start < w2.end < ... < w_n.start < w_n.end From 7ae3f42811e9a0afaa850a50ace44cb65f76e1f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 5 Jul 2021 15:26:03 -0400 Subject: [PATCH 27/35] fix: reset previous trailing comments when parsing async method/accessor --- packages/babel-parser/src/parser/comments.js | 31 ++ .../babel-parser/src/parser/expression.js | 2 + packages/babel-parser/src/parser/statement.js | 2 + .../basic/class-accessor-computed/input.js | 3 + .../basic/class-accessor-computed/output.json | 343 ++++++++++++++++++ .../class-method-async-generator/input.js | 2 + .../class-method-async-generator/output.json | 207 +++++++++++ .../basic/object-accessor-computed/input.js | 3 + .../object-accessor-computed/output.json | 325 +++++++++++++++++ .../object-method-async-generator/input.js | 2 + .../object-method-async-generator/output.json | 197 ++++++++++ 11 files changed, 1117 insertions(+) create mode 100644 packages/babel-parser/test/fixtures/comments/basic/class-accessor-computed/input.js create mode 100644 packages/babel-parser/test/fixtures/comments/basic/class-accessor-computed/output.json create mode 100644 packages/babel-parser/test/fixtures/comments/basic/class-method-async-generator/input.js create mode 100644 packages/babel-parser/test/fixtures/comments/basic/class-method-async-generator/output.json create mode 100644 packages/babel-parser/test/fixtures/comments/basic/object-accessor-computed/input.js create mode 100644 packages/babel-parser/test/fixtures/comments/basic/object-accessor-computed/output.json create mode 100644 packages/babel-parser/test/fixtures/comments/basic/object-method-async-generator/input.js create mode 100644 packages/babel-parser/test/fixtures/comments/basic/object-method-async-generator/output.json diff --git a/packages/babel-parser/src/parser/comments.js b/packages/babel-parser/src/parser/comments.js index 3ec4c14cef70..bab22ad79fa0 100644 --- a/packages/babel-parser/src/parser/comments.js +++ b/packages/babel-parser/src/parser/comments.js @@ -210,4 +210,35 @@ export default class CommentsParser extends BaseParser { } this.state.commentStack = []; } + + /** + * Reset previous node trailing comments. Used in object / class + * property parsing. We parse `async`, `set` and `get` as an identifier + * but may reinterepret it into an async/accessor method later. In this case + * the identifier is not part of the AST and we should sync the knowledge + * to commentStacks + * + * For performance we do not check trailing node and we assume `node` is the + * last finished node before current token. + * + * @param {N.Node} node the last finished AST node _before_ current token + * @returns + * @memberof CommentsParser + */ + resetPreviousNodeTrailingComments(node: N.Node) { + const { commentStack } = this.state; + let i = commentStack.length - 1; + if (i < 0) return; + let commentWS = commentStack[i]; + if (commentWS.leadingNode === node) { + commentWS.leadingNode = null; + return; + } + i--; + if (i < 0) return; + commentWS = commentStack[i]; + if (commentWS.leadingNode === node) { + commentWS.leadingNode = null; + } + } } diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 4300a69d0e35..befb8e533ba6 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -1788,6 +1788,7 @@ export default class ExpressionParser extends LValParser { // https://tc39.es/ecma262/#prod-AsyncGeneratorMethod if (keyName === "async" && !this.hasPrecedingLineBreak()) { isAsync = true; + this.resetPreviousNodeTrailingComments(key); isGenerator = this.eat(tt.star); this.parsePropertyName(prop, /* isPrivateNameAllowed */ false); } @@ -1795,6 +1796,7 @@ export default class ExpressionParser extends LValParser { // set PropertyName[?Yield, ?Await] ( PropertySetParameterList ) { FunctionBody[~Yield, ~Await] } if (keyName === "get" || keyName === "set") { isAccessor = true; + this.resetPreviousNodeTrailingComments(key); prop.kind = keyName; if (this.match(tt.star)) { isGenerator = true; diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index cff855036f19..1571dd1d3a1a 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -1495,6 +1495,7 @@ export default class StatementParser extends ExpressionParser { !this.isLineTerminator() ) { // an async method + this.resetPreviousNodeTrailingComments(key); const isGenerator = this.eat(tt.star); if (publicMember.optional) { @@ -1536,6 +1537,7 @@ export default class StatementParser extends ExpressionParser { ) { // `get\n*` is an uninitialized property named 'get' followed by a generator. // a getter or setter + this.resetPreviousNodeTrailingComments(key); method.kind = key.name; // The so-called parsed name would have been "get/set": get the real name. const isPrivate = this.match(tt.privateName); diff --git a/packages/babel-parser/test/fixtures/comments/basic/class-accessor-computed/input.js b/packages/babel-parser/test/fixtures/comments/basic/class-accessor-computed/input.js new file mode 100644 index 000000000000..54d2e8c4768d --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/class-accessor-computed/input.js @@ -0,0 +1,3 @@ +(class { /* 1 */ set /* 2 */ [ /* 3 */f/* 4 */] /* 5 */ (/* 6 */ a /* 7 */, /* 8 */) /* 9 */ {} }); +(class { /* 1 */ get /* 2 */ [ /* 3 */f/* 4 */] /* 5 */ (/* 6 */) /* 7 */ {} }); +(class { get /* 1 */ [f] () {} }); diff --git a/packages/babel-parser/test/fixtures/comments/basic/class-accessor-computed/output.json b/packages/babel-parser/test/fixtures/comments/basic/class-accessor-computed/output.json new file mode 100644 index 000000000000..fdd022eeb802 --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/class-accessor-computed/output.json @@ -0,0 +1,343 @@ +{ + "type": "File", + "start":0,"end":215,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":34}}, + "program": { + "type": "Program", + "start":0,"end":215,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":34}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":99,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":99}}, + "expression": { + "type": "ClassExpression", + "start":1,"end":97,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":97}}, + "extra": { + "parenthesized": true, + "parenStart": 0 + }, + "id": null, + "superClass": null, + "body": { + "type": "ClassBody", + "start":7,"end":97,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":97}}, + "body": [ + { + "type": "ClassMethod", + "start":17,"end":95,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":95}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 1 ", + "start":9,"end":16,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":16}} + } + ], + "innerComments": [ + { + "type": "CommentBlock", + "value": " 2 ", + "start":21,"end":28,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":28}} + }, + { + "type": "CommentBlock", + "value": " 5 ", + "start":48,"end":55,"loc":{"start":{"line":1,"column":48},"end":{"line":1,"column":55}} + } + ], + "static": false, + "key": { + "type": "Identifier", + "start":38,"end":39,"loc":{"start":{"line":1,"column":38},"end":{"line":1,"column":39},"identifierName":"f"}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 3 ", + "start":31,"end":38,"loc":{"start":{"line":1,"column":31},"end":{"line":1,"column":38}} + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": " 4 ", + "start":39,"end":46,"loc":{"start":{"line":1,"column":39},"end":{"line":1,"column":46}} + } + ], + "name": "f" + }, + "computed": true, + "kind": "set", + "id": null, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start":65,"end":66,"loc":{"start":{"line":1,"column":65},"end":{"line":1,"column":66},"identifierName":"a"}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 6 ", + "start":57,"end":64,"loc":{"start":{"line":1,"column":57},"end":{"line":1,"column":64}} + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": " 7 ", + "start":67,"end":74,"loc":{"start":{"line":1,"column":67},"end":{"line":1,"column":74}} + }, + { + "type": "CommentBlock", + "value": " 8 ", + "start":76,"end":83,"loc":{"start":{"line":1,"column":76},"end":{"line":1,"column":83}} + } + ], + "name": "a" + } + ], + "body": { + "type": "BlockStatement", + "start":93,"end":95,"loc":{"start":{"line":1,"column":93},"end":{"line":1,"column":95}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 9 ", + "start":85,"end":92,"loc":{"start":{"line":1,"column":85},"end":{"line":1,"column":92}} + } + ], + "body": [], + "directives": [] + } + } + ] + } + } + }, + { + "type": "ExpressionStatement", + "start":100,"end":180,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":80}}, + "expression": { + "type": "ClassExpression", + "start":101,"end":178,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":78}}, + "extra": { + "parenthesized": true, + "parenStart": 100 + }, + "id": null, + "superClass": null, + "body": { + "type": "ClassBody", + "start":107,"end":178,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":78}}, + "body": [ + { + "type": "ClassMethod", + "start":117,"end":176,"loc":{"start":{"line":2,"column":17},"end":{"line":2,"column":76}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 1 ", + "start":109,"end":116,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":16}} + } + ], + "innerComments": [ + { + "type": "CommentBlock", + "value": " 2 ", + "start":121,"end":128,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":28}} + }, + { + "type": "CommentBlock", + "value": " 5 ", + "start":148,"end":155,"loc":{"start":{"line":2,"column":48},"end":{"line":2,"column":55}} + }, + { + "type": "CommentBlock", + "value": " 6 ", + "start":157,"end":164,"loc":{"start":{"line":2,"column":57},"end":{"line":2,"column":64}} + } + ], + "static": false, + "key": { + "type": "Identifier", + "start":138,"end":139,"loc":{"start":{"line":2,"column":38},"end":{"line":2,"column":39},"identifierName":"f"}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 3 ", + "start":131,"end":138,"loc":{"start":{"line":2,"column":31},"end":{"line":2,"column":38}} + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": " 4 ", + "start":139,"end":146,"loc":{"start":{"line":2,"column":39},"end":{"line":2,"column":46}} + } + ], + "name": "f" + }, + "computed": true, + "kind": "get", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":174,"end":176,"loc":{"start":{"line":2,"column":74},"end":{"line":2,"column":76}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 7 ", + "start":166,"end":173,"loc":{"start":{"line":2,"column":66},"end":{"line":2,"column":73}} + } + ], + "body": [], + "directives": [] + } + } + ] + } + } + }, + { + "type": "ExpressionStatement", + "start":181,"end":215,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":34}}, + "expression": { + "type": "ClassExpression", + "start":182,"end":213,"loc":{"start":{"line":3,"column":1},"end":{"line":3,"column":32}}, + "extra": { + "parenthesized": true, + "parenStart": 181 + }, + "id": null, + "superClass": null, + "body": { + "type": "ClassBody", + "start":188,"end":213,"loc":{"start":{"line":3,"column":7},"end":{"line":3,"column":32}}, + "body": [ + { + "type": "ClassMethod", + "start":190,"end":211,"loc":{"start":{"line":3,"column":9},"end":{"line":3,"column":30}}, + "innerComments": [ + { + "type": "CommentBlock", + "value": " 1 ", + "start":194,"end":201,"loc":{"start":{"line":3,"column":13},"end":{"line":3,"column":20}} + } + ], + "static": false, + "key": { + "type": "Identifier", + "start":203,"end":204,"loc":{"start":{"line":3,"column":22},"end":{"line":3,"column":23},"identifierName":"f"}, + "name": "f" + }, + "computed": true, + "kind": "get", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":209,"end":211,"loc":{"start":{"line":3,"column":28},"end":{"line":3,"column":30}}, + "body": [], + "directives": [] + } + } + ] + } + } + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentBlock", + "value": " 1 ", + "start":9,"end":16,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":16}} + }, + { + "type": "CommentBlock", + "value": " 2 ", + "start":21,"end":28,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":28}} + }, + { + "type": "CommentBlock", + "value": " 3 ", + "start":31,"end":38,"loc":{"start":{"line":1,"column":31},"end":{"line":1,"column":38}} + }, + { + "type": "CommentBlock", + "value": " 4 ", + "start":39,"end":46,"loc":{"start":{"line":1,"column":39},"end":{"line":1,"column":46}} + }, + { + "type": "CommentBlock", + "value": " 5 ", + "start":48,"end":55,"loc":{"start":{"line":1,"column":48},"end":{"line":1,"column":55}} + }, + { + "type": "CommentBlock", + "value": " 6 ", + "start":57,"end":64,"loc":{"start":{"line":1,"column":57},"end":{"line":1,"column":64}} + }, + { + "type": "CommentBlock", + "value": " 7 ", + "start":67,"end":74,"loc":{"start":{"line":1,"column":67},"end":{"line":1,"column":74}} + }, + { + "type": "CommentBlock", + "value": " 8 ", + "start":76,"end":83,"loc":{"start":{"line":1,"column":76},"end":{"line":1,"column":83}} + }, + { + "type": "CommentBlock", + "value": " 9 ", + "start":85,"end":92,"loc":{"start":{"line":1,"column":85},"end":{"line":1,"column":92}} + }, + { + "type": "CommentBlock", + "value": " 1 ", + "start":109,"end":116,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":16}} + }, + { + "type": "CommentBlock", + "value": " 2 ", + "start":121,"end":128,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":28}} + }, + { + "type": "CommentBlock", + "value": " 3 ", + "start":131,"end":138,"loc":{"start":{"line":2,"column":31},"end":{"line":2,"column":38}} + }, + { + "type": "CommentBlock", + "value": " 4 ", + "start":139,"end":146,"loc":{"start":{"line":2,"column":39},"end":{"line":2,"column":46}} + }, + { + "type": "CommentBlock", + "value": " 5 ", + "start":148,"end":155,"loc":{"start":{"line":2,"column":48},"end":{"line":2,"column":55}} + }, + { + "type": "CommentBlock", + "value": " 6 ", + "start":157,"end":164,"loc":{"start":{"line":2,"column":57},"end":{"line":2,"column":64}} + }, + { + "type": "CommentBlock", + "value": " 7 ", + "start":166,"end":173,"loc":{"start":{"line":2,"column":66},"end":{"line":2,"column":73}} + }, + { + "type": "CommentBlock", + "value": " 1 ", + "start":194,"end":201,"loc":{"start":{"line":3,"column":13},"end":{"line":3,"column":20}} + } + ] +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/comments/basic/class-method-async-generator/input.js b/packages/babel-parser/test/fixtures/comments/basic/class-method-async-generator/input.js new file mode 100644 index 000000000000..4b9c1f582cab --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/class-method-async-generator/input.js @@ -0,0 +1,2 @@ +class C { /* 1 */ async /* 2 */ * /* 3 */ f /* 4 */ (/* 5 */ a /* 6 */, /* 7 */) /* 8 */ {} } +(class { async /* 1 */ *f () {} }); diff --git a/packages/babel-parser/test/fixtures/comments/basic/class-method-async-generator/output.json b/packages/babel-parser/test/fixtures/comments/basic/class-method-async-generator/output.json new file mode 100644 index 000000000000..b8b29273972e --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/class-method-async-generator/output.json @@ -0,0 +1,207 @@ +{ + "type": "File", + "start":0,"end":129,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":35}}, + "program": { + "type": "Program", + "start":0,"end":129,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":35}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ClassDeclaration", + "start":0,"end":93,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":93}}, + "id": { + "type": "Identifier", + "start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7},"identifierName":"C"}, + "name": "C" + }, + "superClass": null, + "body": { + "type": "ClassBody", + "start":8,"end":93,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":93}}, + "body": [ + { + "type": "ClassMethod", + "start":18,"end":91,"loc":{"start":{"line":1,"column":18},"end":{"line":1,"column":91}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 1 ", + "start":10,"end":17,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":17}} + } + ], + "innerComments": [ + { + "type": "CommentBlock", + "value": " 2 ", + "start":24,"end":31,"loc":{"start":{"line":1,"column":24},"end":{"line":1,"column":31}} + } + ], + "static": false, + "key": { + "type": "Identifier", + "start":42,"end":43,"loc":{"start":{"line":1,"column":42},"end":{"line":1,"column":43},"identifierName":"f"}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 3 ", + "start":34,"end":41,"loc":{"start":{"line":1,"column":34},"end":{"line":1,"column":41}} + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": " 4 ", + "start":44,"end":51,"loc":{"start":{"line":1,"column":44},"end":{"line":1,"column":51}} + } + ], + "name": "f" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": true, + "async": true, + "params": [ + { + "type": "Identifier", + "start":61,"end":62,"loc":{"start":{"line":1,"column":61},"end":{"line":1,"column":62},"identifierName":"a"}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 5 ", + "start":53,"end":60,"loc":{"start":{"line":1,"column":53},"end":{"line":1,"column":60}} + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": " 6 ", + "start":63,"end":70,"loc":{"start":{"line":1,"column":63},"end":{"line":1,"column":70}} + }, + { + "type": "CommentBlock", + "value": " 7 ", + "start":72,"end":79,"loc":{"start":{"line":1,"column":72},"end":{"line":1,"column":79}} + } + ], + "name": "a" + } + ], + "body": { + "type": "BlockStatement", + "start":89,"end":91,"loc":{"start":{"line":1,"column":89},"end":{"line":1,"column":91}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 8 ", + "start":81,"end":88,"loc":{"start":{"line":1,"column":81},"end":{"line":1,"column":88}} + } + ], + "body": [], + "directives": [] + } + } + ] + } + }, + { + "type": "ExpressionStatement", + "start":94,"end":129,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":35}}, + "expression": { + "type": "ClassExpression", + "start":95,"end":127,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":33}}, + "extra": { + "parenthesized": true, + "parenStart": 94 + }, + "id": null, + "superClass": null, + "body": { + "type": "ClassBody", + "start":101,"end":127,"loc":{"start":{"line":2,"column":7},"end":{"line":2,"column":33}}, + "body": [ + { + "type": "ClassMethod", + "start":103,"end":125,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":31}}, + "innerComments": [ + { + "type": "CommentBlock", + "value": " 1 ", + "start":109,"end":116,"loc":{"start":{"line":2,"column":15},"end":{"line":2,"column":22}} + } + ], + "static": false, + "key": { + "type": "Identifier", + "start":118,"end":119,"loc":{"start":{"line":2,"column":24},"end":{"line":2,"column":25},"identifierName":"f"}, + "name": "f" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": true, + "async": true, + "params": [], + "body": { + "type": "BlockStatement", + "start":123,"end":125,"loc":{"start":{"line":2,"column":29},"end":{"line":2,"column":31}}, + "body": [], + "directives": [] + } + } + ] + } + } + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentBlock", + "value": " 1 ", + "start":10,"end":17,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":17}} + }, + { + "type": "CommentBlock", + "value": " 2 ", + "start":24,"end":31,"loc":{"start":{"line":1,"column":24},"end":{"line":1,"column":31}} + }, + { + "type": "CommentBlock", + "value": " 3 ", + "start":34,"end":41,"loc":{"start":{"line":1,"column":34},"end":{"line":1,"column":41}} + }, + { + "type": "CommentBlock", + "value": " 4 ", + "start":44,"end":51,"loc":{"start":{"line":1,"column":44},"end":{"line":1,"column":51}} + }, + { + "type": "CommentBlock", + "value": " 5 ", + "start":53,"end":60,"loc":{"start":{"line":1,"column":53},"end":{"line":1,"column":60}} + }, + { + "type": "CommentBlock", + "value": " 6 ", + "start":63,"end":70,"loc":{"start":{"line":1,"column":63},"end":{"line":1,"column":70}} + }, + { + "type": "CommentBlock", + "value": " 7 ", + "start":72,"end":79,"loc":{"start":{"line":1,"column":72},"end":{"line":1,"column":79}} + }, + { + "type": "CommentBlock", + "value": " 8 ", + "start":81,"end":88,"loc":{"start":{"line":1,"column":81},"end":{"line":1,"column":88}} + }, + { + "type": "CommentBlock", + "value": " 1 ", + "start":109,"end":116,"loc":{"start":{"line":2,"column":15},"end":{"line":2,"column":22}} + } + ] +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/comments/basic/object-accessor-computed/input.js b/packages/babel-parser/test/fixtures/comments/basic/object-accessor-computed/input.js new file mode 100644 index 000000000000..b23bc2eb5c72 --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/object-accessor-computed/input.js @@ -0,0 +1,3 @@ +({ /* 1 */ set /* 2 */ [ /* 3 */f/* 4 */] /* 5 */ (/* 6 */ a /* 7 */, /* 8 */) /* 9 */ {} }); +({ /* 1 */ get /* 2 */ [ /* 3 */f/* 4 */] /* 5 */ (/* 6 */) /* 7 */ {} }); +({ get /* 1 */ [f] () {} }); diff --git a/packages/babel-parser/test/fixtures/comments/basic/object-accessor-computed/output.json b/packages/babel-parser/test/fixtures/comments/basic/object-accessor-computed/output.json new file mode 100644 index 000000000000..fe999b322c9e --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/object-accessor-computed/output.json @@ -0,0 +1,325 @@ +{ + "type": "File", + "start":0,"end":197,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":28}}, + "program": { + "type": "Program", + "start":0,"end":197,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":28}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":93,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":93}}, + "expression": { + "type": "ObjectExpression", + "start":1,"end":91,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":91}}, + "extra": { + "parenthesized": true, + "parenStart": 0 + }, + "properties": [ + { + "type": "ObjectMethod", + "start":11,"end":89,"loc":{"start":{"line":1,"column":11},"end":{"line":1,"column":89}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 1 ", + "start":3,"end":10,"loc":{"start":{"line":1,"column":3},"end":{"line":1,"column":10}} + } + ], + "innerComments": [ + { + "type": "CommentBlock", + "value": " 2 ", + "start":15,"end":22,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":22}} + }, + { + "type": "CommentBlock", + "value": " 5 ", + "start":42,"end":49,"loc":{"start":{"line":1,"column":42},"end":{"line":1,"column":49}} + } + ], + "method": false, + "key": { + "type": "Identifier", + "start":32,"end":33,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":33},"identifierName":"f"}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 3 ", + "start":25,"end":32,"loc":{"start":{"line":1,"column":25},"end":{"line":1,"column":32}} + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": " 4 ", + "start":33,"end":40,"loc":{"start":{"line":1,"column":33},"end":{"line":1,"column":40}} + } + ], + "name": "f" + }, + "computed": true, + "kind": "set", + "id": null, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start":59,"end":60,"loc":{"start":{"line":1,"column":59},"end":{"line":1,"column":60},"identifierName":"a"}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 6 ", + "start":51,"end":58,"loc":{"start":{"line":1,"column":51},"end":{"line":1,"column":58}} + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": " 7 ", + "start":61,"end":68,"loc":{"start":{"line":1,"column":61},"end":{"line":1,"column":68}} + }, + { + "type": "CommentBlock", + "value": " 8 ", + "start":70,"end":77,"loc":{"start":{"line":1,"column":70},"end":{"line":1,"column":77}} + } + ], + "name": "a" + } + ], + "body": { + "type": "BlockStatement", + "start":87,"end":89,"loc":{"start":{"line":1,"column":87},"end":{"line":1,"column":89}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 9 ", + "start":79,"end":86,"loc":{"start":{"line":1,"column":79},"end":{"line":1,"column":86}} + } + ], + "body": [], + "directives": [] + } + } + ] + } + }, + { + "type": "ExpressionStatement", + "start":94,"end":168,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":74}}, + "expression": { + "type": "ObjectExpression", + "start":95,"end":166,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":72}}, + "extra": { + "parenthesized": true, + "parenStart": 94 + }, + "properties": [ + { + "type": "ObjectMethod", + "start":105,"end":164,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":70}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 1 ", + "start":97,"end":104,"loc":{"start":{"line":2,"column":3},"end":{"line":2,"column":10}} + } + ], + "innerComments": [ + { + "type": "CommentBlock", + "value": " 2 ", + "start":109,"end":116,"loc":{"start":{"line":2,"column":15},"end":{"line":2,"column":22}} + }, + { + "type": "CommentBlock", + "value": " 5 ", + "start":136,"end":143,"loc":{"start":{"line":2,"column":42},"end":{"line":2,"column":49}} + }, + { + "type": "CommentBlock", + "value": " 6 ", + "start":145,"end":152,"loc":{"start":{"line":2,"column":51},"end":{"line":2,"column":58}} + } + ], + "method": false, + "key": { + "type": "Identifier", + "start":126,"end":127,"loc":{"start":{"line":2,"column":32},"end":{"line":2,"column":33},"identifierName":"f"}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 3 ", + "start":119,"end":126,"loc":{"start":{"line":2,"column":25},"end":{"line":2,"column":32}} + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": " 4 ", + "start":127,"end":134,"loc":{"start":{"line":2,"column":33},"end":{"line":2,"column":40}} + } + ], + "name": "f" + }, + "computed": true, + "kind": "get", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":162,"end":164,"loc":{"start":{"line":2,"column":68},"end":{"line":2,"column":70}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 7 ", + "start":154,"end":161,"loc":{"start":{"line":2,"column":60},"end":{"line":2,"column":67}} + } + ], + "body": [], + "directives": [] + } + } + ] + } + }, + { + "type": "ExpressionStatement", + "start":169,"end":197,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":28}}, + "expression": { + "type": "ObjectExpression", + "start":170,"end":195,"loc":{"start":{"line":3,"column":1},"end":{"line":3,"column":26}}, + "extra": { + "parenthesized": true, + "parenStart": 169 + }, + "properties": [ + { + "type": "ObjectMethod", + "start":172,"end":193,"loc":{"start":{"line":3,"column":3},"end":{"line":3,"column":24}}, + "innerComments": [ + { + "type": "CommentBlock", + "value": " 1 ", + "start":176,"end":183,"loc":{"start":{"line":3,"column":7},"end":{"line":3,"column":14}} + } + ], + "method": false, + "key": { + "type": "Identifier", + "start":185,"end":186,"loc":{"start":{"line":3,"column":16},"end":{"line":3,"column":17},"identifierName":"f"}, + "name": "f" + }, + "computed": true, + "kind": "get", + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":191,"end":193,"loc":{"start":{"line":3,"column":22},"end":{"line":3,"column":24}}, + "body": [], + "directives": [] + } + } + ] + } + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentBlock", + "value": " 1 ", + "start":3,"end":10,"loc":{"start":{"line":1,"column":3},"end":{"line":1,"column":10}} + }, + { + "type": "CommentBlock", + "value": " 2 ", + "start":15,"end":22,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":22}} + }, + { + "type": "CommentBlock", + "value": " 3 ", + "start":25,"end":32,"loc":{"start":{"line":1,"column":25},"end":{"line":1,"column":32}} + }, + { + "type": "CommentBlock", + "value": " 4 ", + "start":33,"end":40,"loc":{"start":{"line":1,"column":33},"end":{"line":1,"column":40}} + }, + { + "type": "CommentBlock", + "value": " 5 ", + "start":42,"end":49,"loc":{"start":{"line":1,"column":42},"end":{"line":1,"column":49}} + }, + { + "type": "CommentBlock", + "value": " 6 ", + "start":51,"end":58,"loc":{"start":{"line":1,"column":51},"end":{"line":1,"column":58}} + }, + { + "type": "CommentBlock", + "value": " 7 ", + "start":61,"end":68,"loc":{"start":{"line":1,"column":61},"end":{"line":1,"column":68}} + }, + { + "type": "CommentBlock", + "value": " 8 ", + "start":70,"end":77,"loc":{"start":{"line":1,"column":70},"end":{"line":1,"column":77}} + }, + { + "type": "CommentBlock", + "value": " 9 ", + "start":79,"end":86,"loc":{"start":{"line":1,"column":79},"end":{"line":1,"column":86}} + }, + { + "type": "CommentBlock", + "value": " 1 ", + "start":97,"end":104,"loc":{"start":{"line":2,"column":3},"end":{"line":2,"column":10}} + }, + { + "type": "CommentBlock", + "value": " 2 ", + "start":109,"end":116,"loc":{"start":{"line":2,"column":15},"end":{"line":2,"column":22}} + }, + { + "type": "CommentBlock", + "value": " 3 ", + "start":119,"end":126,"loc":{"start":{"line":2,"column":25},"end":{"line":2,"column":32}} + }, + { + "type": "CommentBlock", + "value": " 4 ", + "start":127,"end":134,"loc":{"start":{"line":2,"column":33},"end":{"line":2,"column":40}} + }, + { + "type": "CommentBlock", + "value": " 5 ", + "start":136,"end":143,"loc":{"start":{"line":2,"column":42},"end":{"line":2,"column":49}} + }, + { + "type": "CommentBlock", + "value": " 6 ", + "start":145,"end":152,"loc":{"start":{"line":2,"column":51},"end":{"line":2,"column":58}} + }, + { + "type": "CommentBlock", + "value": " 7 ", + "start":154,"end":161,"loc":{"start":{"line":2,"column":60},"end":{"line":2,"column":67}} + }, + { + "type": "CommentBlock", + "value": " 1 ", + "start":176,"end":183,"loc":{"start":{"line":3,"column":7},"end":{"line":3,"column":14}} + } + ] +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/comments/basic/object-method-async-generator/input.js b/packages/babel-parser/test/fixtures/comments/basic/object-method-async-generator/input.js new file mode 100644 index 000000000000..4fec8e847cb2 --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/object-method-async-generator/input.js @@ -0,0 +1,2 @@ +({ /* 1 */ async /* 2 */ * /* 3 */ f /* 4 */ (/* 5 */ a /* 6 */, /* 7 */) /* 8 */ {} }) +({ async /* 1 */ *f () {} }); diff --git a/packages/babel-parser/test/fixtures/comments/basic/object-method-async-generator/output.json b/packages/babel-parser/test/fixtures/comments/basic/object-method-async-generator/output.json new file mode 100644 index 000000000000..f7e58e7b502f --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/object-method-async-generator/output.json @@ -0,0 +1,197 @@ +{ + "type": "File", + "start":0,"end":117,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":29}}, + "program": { + "type": "Program", + "start":0,"end":117,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":29}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":117,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":29}}, + "expression": { + "type": "CallExpression", + "start":0,"end":116,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":28}}, + "callee": { + "type": "ObjectExpression", + "start":1,"end":86,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":86}}, + "extra": { + "parenthesized": true, + "parenStart": 0 + }, + "properties": [ + { + "type": "ObjectMethod", + "start":11,"end":84,"loc":{"start":{"line":1,"column":11},"end":{"line":1,"column":84}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 1 ", + "start":3,"end":10,"loc":{"start":{"line":1,"column":3},"end":{"line":1,"column":10}} + } + ], + "innerComments": [ + { + "type": "CommentBlock", + "value": " 2 ", + "start":17,"end":24,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":24}} + } + ], + "method": true, + "key": { + "type": "Identifier", + "start":35,"end":36,"loc":{"start":{"line":1,"column":35},"end":{"line":1,"column":36},"identifierName":"f"}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 3 ", + "start":27,"end":34,"loc":{"start":{"line":1,"column":27},"end":{"line":1,"column":34}} + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": " 4 ", + "start":37,"end":44,"loc":{"start":{"line":1,"column":37},"end":{"line":1,"column":44}} + } + ], + "name": "f" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": true, + "async": true, + "params": [ + { + "type": "Identifier", + "start":54,"end":55,"loc":{"start":{"line":1,"column":54},"end":{"line":1,"column":55},"identifierName":"a"}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 5 ", + "start":46,"end":53,"loc":{"start":{"line":1,"column":46},"end":{"line":1,"column":53}} + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": " 6 ", + "start":56,"end":63,"loc":{"start":{"line":1,"column":56},"end":{"line":1,"column":63}} + }, + { + "type": "CommentBlock", + "value": " 7 ", + "start":65,"end":72,"loc":{"start":{"line":1,"column":65},"end":{"line":1,"column":72}} + } + ], + "name": "a" + } + ], + "body": { + "type": "BlockStatement", + "start":82,"end":84,"loc":{"start":{"line":1,"column":82},"end":{"line":1,"column":84}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 8 ", + "start":74,"end":81,"loc":{"start":{"line":1,"column":74},"end":{"line":1,"column":81}} + } + ], + "body": [], + "directives": [] + } + } + ] + }, + "arguments": [ + { + "type": "ObjectExpression", + "start":89,"end":115,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":27}}, + "properties": [ + { + "type": "ObjectMethod", + "start":91,"end":113,"loc":{"start":{"line":2,"column":3},"end":{"line":2,"column":25}}, + "innerComments": [ + { + "type": "CommentBlock", + "value": " 1 ", + "start":97,"end":104,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":16}} + } + ], + "method": true, + "key": { + "type": "Identifier", + "start":106,"end":107,"loc":{"start":{"line":2,"column":18},"end":{"line":2,"column":19},"identifierName":"f"}, + "name": "f" + }, + "computed": false, + "kind": "method", + "id": null, + "generator": true, + "async": true, + "params": [], + "body": { + "type": "BlockStatement", + "start":111,"end":113,"loc":{"start":{"line":2,"column":23},"end":{"line":2,"column":25}}, + "body": [], + "directives": [] + } + } + ] + } + ] + } + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentBlock", + "value": " 1 ", + "start":3,"end":10,"loc":{"start":{"line":1,"column":3},"end":{"line":1,"column":10}} + }, + { + "type": "CommentBlock", + "value": " 2 ", + "start":17,"end":24,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":24}} + }, + { + "type": "CommentBlock", + "value": " 3 ", + "start":27,"end":34,"loc":{"start":{"line":1,"column":27},"end":{"line":1,"column":34}} + }, + { + "type": "CommentBlock", + "value": " 4 ", + "start":37,"end":44,"loc":{"start":{"line":1,"column":37},"end":{"line":1,"column":44}} + }, + { + "type": "CommentBlock", + "value": " 5 ", + "start":46,"end":53,"loc":{"start":{"line":1,"column":46},"end":{"line":1,"column":53}} + }, + { + "type": "CommentBlock", + "value": " 6 ", + "start":56,"end":63,"loc":{"start":{"line":1,"column":56},"end":{"line":1,"column":63}} + }, + { + "type": "CommentBlock", + "value": " 7 ", + "start":65,"end":72,"loc":{"start":{"line":1,"column":65},"end":{"line":1,"column":72}} + }, + { + "type": "CommentBlock", + "value": " 8 ", + "start":74,"end":81,"loc":{"start":{"line":1,"column":74},"end":{"line":1,"column":81}} + }, + { + "type": "CommentBlock", + "value": " 1 ", + "start":97,"end":104,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":16}} + } + ] +} \ No newline at end of file From 0981f2ef6e7cd6ead4d3975d522b5b185d9f25ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 5 Jul 2021 15:26:23 -0400 Subject: [PATCH 28/35] chore: add more comment testcases --- .../comments/basic/arrow-function/input.js | 3 + .../comments/basic/arrow-function/output.json | 126 +++++++ .../comments/basic/switch-case/input.js | 6 + .../comments/basic/switch-case/output.json | 342 ++++++++++++++++++ .../comments/basic/try-statement/input.js | 4 + .../comments/basic/try-statement/output.json | 259 +++++++++++++ 6 files changed, 740 insertions(+) create mode 100644 packages/babel-parser/test/fixtures/comments/basic/arrow-function/input.js create mode 100644 packages/babel-parser/test/fixtures/comments/basic/arrow-function/output.json create mode 100644 packages/babel-parser/test/fixtures/comments/basic/switch-case/input.js create mode 100644 packages/babel-parser/test/fixtures/comments/basic/switch-case/output.json create mode 100644 packages/babel-parser/test/fixtures/comments/basic/try-statement/input.js create mode 100644 packages/babel-parser/test/fixtures/comments/basic/try-statement/output.json diff --git a/packages/babel-parser/test/fixtures/comments/basic/arrow-function/input.js b/packages/babel-parser/test/fixtures/comments/basic/arrow-function/input.js new file mode 100644 index 000000000000..f923f755c59c --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/arrow-function/input.js @@ -0,0 +1,3 @@ +(/*1*/)=>x; + +(/*1*/x/*2*/,/*3*/) /*4*/ => /*5*/ {} diff --git a/packages/babel-parser/test/fixtures/comments/basic/arrow-function/output.json b/packages/babel-parser/test/fixtures/comments/basic/arrow-function/output.json new file mode 100644 index 000000000000..b0f4f03e995f --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/arrow-function/output.json @@ -0,0 +1,126 @@ +{ + "type": "File", + "start":0,"end":50,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":37}}, + "program": { + "type": "Program", + "start":0,"end":50,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":37}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":11,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":11}}, + "expression": { + "type": "ArrowFunctionExpression", + "start":0,"end":10,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":10}}, + "innerComments": [ + { + "type": "CommentBlock", + "value": "1", + "start":1,"end":6,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":6}} + } + ], + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "Identifier", + "start":9,"end":10,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":10},"identifierName":"x"}, + "name": "x" + } + } + }, + { + "type": "ExpressionStatement", + "start":13,"end":50,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":37}}, + "expression": { + "type": "ArrowFunctionExpression", + "start":13,"end":50,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":37}}, + "innerComments": [ + { + "type": "CommentBlock", + "value": "4", + "start":33,"end":38,"loc":{"start":{"line":3,"column":20},"end":{"line":3,"column":25}} + } + ], + "id": null, + "generator": false, + "async": false, + "params": [ + { + "type": "Identifier", + "start":19,"end":20,"loc":{"start":{"line":3,"column":6},"end":{"line":3,"column":7},"identifierName":"x"}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": "1", + "start":14,"end":19,"loc":{"start":{"line":3,"column":1},"end":{"line":3,"column":6}} + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": "2", + "start":20,"end":25,"loc":{"start":{"line":3,"column":7},"end":{"line":3,"column":12}} + }, + { + "type": "CommentBlock", + "value": "3", + "start":26,"end":31,"loc":{"start":{"line":3,"column":13},"end":{"line":3,"column":18}} + } + ], + "name": "x" + } + ], + "body": { + "type": "BlockStatement", + "start":48,"end":50,"loc":{"start":{"line":3,"column":35},"end":{"line":3,"column":37}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": "5", + "start":42,"end":47,"loc":{"start":{"line":3,"column":29},"end":{"line":3,"column":34}} + } + ], + "body": [], + "directives": [] + } + } + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentBlock", + "value": "1", + "start":1,"end":6,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":6}} + }, + { + "type": "CommentBlock", + "value": "1", + "start":14,"end":19,"loc":{"start":{"line":3,"column":1},"end":{"line":3,"column":6}} + }, + { + "type": "CommentBlock", + "value": "2", + "start":20,"end":25,"loc":{"start":{"line":3,"column":7},"end":{"line":3,"column":12}} + }, + { + "type": "CommentBlock", + "value": "3", + "start":26,"end":31,"loc":{"start":{"line":3,"column":13},"end":{"line":3,"column":18}} + }, + { + "type": "CommentBlock", + "value": "4", + "start":33,"end":38,"loc":{"start":{"line":3,"column":20},"end":{"line":3,"column":25}} + }, + { + "type": "CommentBlock", + "value": "5", + "start":42,"end":47,"loc":{"start":{"line":3,"column":29},"end":{"line":3,"column":34}} + } + ] +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/comments/basic/switch-case/input.js b/packages/babel-parser/test/fixtures/comments/basic/switch-case/input.js new file mode 100644 index 000000000000..bfca352a1b22 --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/switch-case/input.js @@ -0,0 +1,6 @@ +/*-1*/ foo /*0*/ : /*1*/ switch /*2*/ ( /*3*/ false /*4*/ ) /*5*/ { + /*6*/ case /*7*/ false /*8*/ : /*9*/ + break /*10*/ foo /*11*/ ; + /*12*/ default /*13*/ : /*14*/ + case /*15*/ false /*16*/ : /*17*/ { /*18*/ } /*19*/ + } diff --git a/packages/babel-parser/test/fixtures/comments/basic/switch-case/output.json b/packages/babel-parser/test/fixtures/comments/basic/switch-case/output.json new file mode 100644 index 000000000000..cd2a5e6aefb8 --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/switch-case/output.json @@ -0,0 +1,342 @@ +{ + "type": "File", + "start":0,"end":233,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":3}}, + "program": { + "type": "Program", + "start":0,"end":233,"loc":{"start":{"line":1,"column":0},"end":{"line":6,"column":3}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "LabeledStatement", + "start":7,"end":233,"loc":{"start":{"line":1,"column":7},"end":{"line":6,"column":3}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": "-1", + "start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}} + } + ], + "body": { + "type": "SwitchStatement", + "start":25,"end":233,"loc":{"start":{"line":1,"column":25},"end":{"line":6,"column":3}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": "1", + "start":19,"end":24,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":24}} + } + ], + "innerComments": [ + { + "type": "CommentBlock", + "value": "2", + "start":32,"end":37,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":37}} + }, + { + "type": "CommentBlock", + "value": "5", + "start":60,"end":65,"loc":{"start":{"line":1,"column":60},"end":{"line":1,"column":65}} + } + ], + "discriminant": { + "type": "BooleanLiteral", + "start":46,"end":51,"loc":{"start":{"line":1,"column":46},"end":{"line":1,"column":51}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": "3", + "start":40,"end":45,"loc":{"start":{"line":1,"column":40},"end":{"line":1,"column":45}} + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": "4", + "start":52,"end":57,"loc":{"start":{"line":1,"column":52},"end":{"line":1,"column":57}} + } + ], + "value": false + }, + "cases": [ + { + "type": "SwitchCase", + "start":78,"end":138,"loc":{"start":{"line":2,"column":10},"end":{"line":3,"column":29}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": "6", + "start":72,"end":77,"loc":{"start":{"line":2,"column":4},"end":{"line":2,"column":9}} + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": "12", + "start":143,"end":149,"loc":{"start":{"line":4,"column":4},"end":{"line":4,"column":10}} + } + ], + "consequent": [ + { + "type": "BreakStatement", + "start":113,"end":138,"loc":{"start":{"line":3,"column":4},"end":{"line":3,"column":29}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": "9", + "start":103,"end":108,"loc":{"start":{"line":2,"column":35},"end":{"line":2,"column":40}} + } + ], + "label": { + "type": "Identifier", + "start":126,"end":129,"loc":{"start":{"line":3,"column":17},"end":{"line":3,"column":20},"identifierName":"foo"}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": "10", + "start":119,"end":125,"loc":{"start":{"line":3,"column":10},"end":{"line":3,"column":16}} + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": "11", + "start":130,"end":136,"loc":{"start":{"line":3,"column":21},"end":{"line":3,"column":27}} + } + ], + "name": "foo" + } + } + ], + "test": { + "type": "BooleanLiteral", + "start":89,"end":94,"loc":{"start":{"line":2,"column":21},"end":{"line":2,"column":26}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": "7", + "start":83,"end":88,"loc":{"start":{"line":2,"column":15},"end":{"line":2,"column":20}} + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": "8", + "start":95,"end":100,"loc":{"start":{"line":2,"column":27},"end":{"line":2,"column":32}} + } + ], + "value": false + } + }, + { + "type": "SwitchCase", + "start":150,"end":166,"loc":{"start":{"line":4,"column":11},"end":{"line":4,"column":27}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": "12", + "start":143,"end":149,"loc":{"start":{"line":4,"column":4},"end":{"line":4,"column":10}} + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": "14", + "start":167,"end":173,"loc":{"start":{"line":4,"column":28},"end":{"line":4,"column":34}} + } + ], + "innerComments": [ + { + "type": "CommentBlock", + "value": "13", + "start":158,"end":164,"loc":{"start":{"line":4,"column":19},"end":{"line":4,"column":25}} + } + ], + "consequent": [], + "test": null + }, + { + "type": "SwitchCase", + "start":178,"end":222,"loc":{"start":{"line":5,"column":4},"end":{"line":5,"column":48}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": "14", + "start":167,"end":173,"loc":{"start":{"line":4,"column":28},"end":{"line":4,"column":34}} + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": "19", + "start":223,"end":229,"loc":{"start":{"line":5,"column":49},"end":{"line":5,"column":55}} + } + ], + "consequent": [ + { + "type": "BlockStatement", + "start":212,"end":222,"loc":{"start":{"line":5,"column":38},"end":{"line":5,"column":48}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": "17", + "start":205,"end":211,"loc":{"start":{"line":5,"column":31},"end":{"line":5,"column":37}} + } + ], + "innerComments": [ + { + "type": "CommentBlock", + "value": "18", + "start":214,"end":220,"loc":{"start":{"line":5,"column":40},"end":{"line":5,"column":46}} + } + ], + "body": [], + "directives": [] + } + ], + "test": { + "type": "BooleanLiteral", + "start":190,"end":195,"loc":{"start":{"line":5,"column":16},"end":{"line":5,"column":21}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": "15", + "start":183,"end":189,"loc":{"start":{"line":5,"column":9},"end":{"line":5,"column":15}} + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": "16", + "start":196,"end":202,"loc":{"start":{"line":5,"column":22},"end":{"line":5,"column":28}} + } + ], + "value": false + } + } + ] + }, + "label": { + "type": "Identifier", + "start":7,"end":10,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":10},"identifierName":"foo"}, + "trailingComments": [ + { + "type": "CommentBlock", + "value": "0", + "start":11,"end":16,"loc":{"start":{"line":1,"column":11},"end":{"line":1,"column":16}} + } + ], + "name": "foo" + } + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentBlock", + "value": "-1", + "start":0,"end":6,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":6}} + }, + { + "type": "CommentBlock", + "value": "0", + "start":11,"end":16,"loc":{"start":{"line":1,"column":11},"end":{"line":1,"column":16}} + }, + { + "type": "CommentBlock", + "value": "1", + "start":19,"end":24,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":24}} + }, + { + "type": "CommentBlock", + "value": "2", + "start":32,"end":37,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":37}} + }, + { + "type": "CommentBlock", + "value": "3", + "start":40,"end":45,"loc":{"start":{"line":1,"column":40},"end":{"line":1,"column":45}} + }, + { + "type": "CommentBlock", + "value": "4", + "start":52,"end":57,"loc":{"start":{"line":1,"column":52},"end":{"line":1,"column":57}} + }, + { + "type": "CommentBlock", + "value": "5", + "start":60,"end":65,"loc":{"start":{"line":1,"column":60},"end":{"line":1,"column":65}} + }, + { + "type": "CommentBlock", + "value": "6", + "start":72,"end":77,"loc":{"start":{"line":2,"column":4},"end":{"line":2,"column":9}} + }, + { + "type": "CommentBlock", + "value": "7", + "start":83,"end":88,"loc":{"start":{"line":2,"column":15},"end":{"line":2,"column":20}} + }, + { + "type": "CommentBlock", + "value": "8", + "start":95,"end":100,"loc":{"start":{"line":2,"column":27},"end":{"line":2,"column":32}} + }, + { + "type": "CommentBlock", + "value": "9", + "start":103,"end":108,"loc":{"start":{"line":2,"column":35},"end":{"line":2,"column":40}} + }, + { + "type": "CommentBlock", + "value": "10", + "start":119,"end":125,"loc":{"start":{"line":3,"column":10},"end":{"line":3,"column":16}} + }, + { + "type": "CommentBlock", + "value": "11", + "start":130,"end":136,"loc":{"start":{"line":3,"column":21},"end":{"line":3,"column":27}} + }, + { + "type": "CommentBlock", + "value": "12", + "start":143,"end":149,"loc":{"start":{"line":4,"column":4},"end":{"line":4,"column":10}} + }, + { + "type": "CommentBlock", + "value": "13", + "start":158,"end":164,"loc":{"start":{"line":4,"column":19},"end":{"line":4,"column":25}} + }, + { + "type": "CommentBlock", + "value": "14", + "start":167,"end":173,"loc":{"start":{"line":4,"column":28},"end":{"line":4,"column":34}} + }, + { + "type": "CommentBlock", + "value": "15", + "start":183,"end":189,"loc":{"start":{"line":5,"column":9},"end":{"line":5,"column":15}} + }, + { + "type": "CommentBlock", + "value": "16", + "start":196,"end":202,"loc":{"start":{"line":5,"column":22},"end":{"line":5,"column":28}} + }, + { + "type": "CommentBlock", + "value": "17", + "start":205,"end":211,"loc":{"start":{"line":5,"column":31},"end":{"line":5,"column":37}} + }, + { + "type": "CommentBlock", + "value": "18", + "start":214,"end":220,"loc":{"start":{"line":5,"column":40},"end":{"line":5,"column":46}} + }, + { + "type": "CommentBlock", + "value": "19", + "start":223,"end":229,"loc":{"start":{"line":5,"column":49},"end":{"line":5,"column":55}} + } + ] +} diff --git a/packages/babel-parser/test/fixtures/comments/basic/try-statement/input.js b/packages/babel-parser/test/fixtures/comments/basic/try-statement/input.js new file mode 100644 index 000000000000..b06c9f6c4176 --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/try-statement/input.js @@ -0,0 +1,4 @@ +/*1*/ try /*2*/ { + /*3*/ throw /*4*/ "no" /*5*/; +/*6*/} /*7*/ catch /*8*/ ( /*9*/ e /*10*/ ) /*11*/ { /*12*/ +} /*13*/ finally /*14*/ { /*15*/ } /*16*/ diff --git a/packages/babel-parser/test/fixtures/comments/basic/try-statement/output.json b/packages/babel-parser/test/fixtures/comments/basic/try-statement/output.json new file mode 100644 index 000000000000..fb3ba476e0a6 --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/try-statement/output.json @@ -0,0 +1,259 @@ +{ + "type": "File", + "start":0,"end":153,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":41}}, + "program": { + "type": "Program", + "start":0,"end":153,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":41}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "TryStatement", + "start":6,"end":146,"loc":{"start":{"line":1,"column":6},"end":{"line":4,"column":34}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": "1", + "start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}} + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": "16", + "start":147,"end":153,"loc":{"start":{"line":4,"column":35},"end":{"line":4,"column":41}} + } + ], + "block": { + "type": "BlockStatement", + "start":16,"end":58,"loc":{"start":{"line":1,"column":16},"end":{"line":3,"column":6}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": "2", + "start":10,"end":15,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":15}} + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": "7", + "start":59,"end":64,"loc":{"start":{"line":3,"column":7},"end":{"line":3,"column":12}} + } + ], + "body": [ + { + "type": "ThrowStatement", + "start":28,"end":51,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":33}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": "3", + "start":22,"end":27,"loc":{"start":{"line":2,"column":4},"end":{"line":2,"column":9}} + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": "6", + "start":52,"end":57,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":5}} + } + ], + "argument": { + "type": "StringLiteral", + "start":40,"end":44,"loc":{"start":{"line":2,"column":22},"end":{"line":2,"column":26}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": "4", + "start":34,"end":39,"loc":{"start":{"line":2,"column":16},"end":{"line":2,"column":21}} + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": "5", + "start":45,"end":50,"loc":{"start":{"line":2,"column":27},"end":{"line":2,"column":32}} + } + ], + "extra": { + "rawValue": "no", + "raw": "\"no\"" + }, + "value": "no" + } + } + ], + "directives": [] + }, + "handler": { + "type": "CatchClause", + "start":65,"end":113,"loc":{"start":{"line":3,"column":13},"end":{"line":4,"column":1}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": "7", + "start":59,"end":64,"loc":{"start":{"line":3,"column":7},"end":{"line":3,"column":12}} + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": "13", + "start":114,"end":120,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":8}} + } + ], + "innerComments": [ + { + "type": "CommentBlock", + "value": "8", + "start":71,"end":76,"loc":{"start":{"line":3,"column":19},"end":{"line":3,"column":24}} + } + ], + "param": { + "type": "Identifier", + "start":85,"end":86,"loc":{"start":{"line":3,"column":33},"end":{"line":3,"column":34},"identifierName":"e"}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": "9", + "start":79,"end":84,"loc":{"start":{"line":3,"column":27},"end":{"line":3,"column":32}} + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": "10", + "start":87,"end":93,"loc":{"start":{"line":3,"column":35},"end":{"line":3,"column":41}} + } + ], + "name": "e" + }, + "body": { + "type": "BlockStatement", + "start":103,"end":113,"loc":{"start":{"line":3,"column":51},"end":{"line":4,"column":1}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": "11", + "start":96,"end":102,"loc":{"start":{"line":3,"column":44},"end":{"line":3,"column":50}} + } + ], + "innerComments": [ + { + "type": "CommentBlock", + "value": "12", + "start":105,"end":111,"loc":{"start":{"line":3,"column":53},"end":{"line":3,"column":59}} + } + ], + "body": [], + "directives": [] + } + }, + "finalizer": { + "type": "BlockStatement", + "start":136,"end":146,"loc":{"start":{"line":4,"column":24},"end":{"line":4,"column":34}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": "14", + "start":129,"end":135,"loc":{"start":{"line":4,"column":17},"end":{"line":4,"column":23}} + } + ], + "innerComments": [ + { + "type": "CommentBlock", + "value": "15", + "start":138,"end":144,"loc":{"start":{"line":4,"column":26},"end":{"line":4,"column":32}} + } + ], + "body": [], + "directives": [] + } + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentBlock", + "value": "1", + "start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}} + }, + { + "type": "CommentBlock", + "value": "2", + "start":10,"end":15,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":15}} + }, + { + "type": "CommentBlock", + "value": "3", + "start":22,"end":27,"loc":{"start":{"line":2,"column":4},"end":{"line":2,"column":9}} + }, + { + "type": "CommentBlock", + "value": "4", + "start":34,"end":39,"loc":{"start":{"line":2,"column":16},"end":{"line":2,"column":21}} + }, + { + "type": "CommentBlock", + "value": "5", + "start":45,"end":50,"loc":{"start":{"line":2,"column":27},"end":{"line":2,"column":32}} + }, + { + "type": "CommentBlock", + "value": "6", + "start":52,"end":57,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":5}} + }, + { + "type": "CommentBlock", + "value": "7", + "start":59,"end":64,"loc":{"start":{"line":3,"column":7},"end":{"line":3,"column":12}} + }, + { + "type": "CommentBlock", + "value": "8", + "start":71,"end":76,"loc":{"start":{"line":3,"column":19},"end":{"line":3,"column":24}} + }, + { + "type": "CommentBlock", + "value": "9", + "start":79,"end":84,"loc":{"start":{"line":3,"column":27},"end":{"line":3,"column":32}} + }, + { + "type": "CommentBlock", + "value": "10", + "start":87,"end":93,"loc":{"start":{"line":3,"column":35},"end":{"line":3,"column":41}} + }, + { + "type": "CommentBlock", + "value": "11", + "start":96,"end":102,"loc":{"start":{"line":3,"column":44},"end":{"line":3,"column":50}} + }, + { + "type": "CommentBlock", + "value": "12", + "start":105,"end":111,"loc":{"start":{"line":3,"column":53},"end":{"line":3,"column":59}} + }, + { + "type": "CommentBlock", + "value": "13", + "start":114,"end":120,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":8}} + }, + { + "type": "CommentBlock", + "value": "14", + "start":129,"end":135,"loc":{"start":{"line":4,"column":17},"end":{"line":4,"column":23}} + }, + { + "type": "CommentBlock", + "value": "15", + "start":138,"end":144,"loc":{"start":{"line":4,"column":26},"end":{"line":4,"column":32}} + }, + { + "type": "CommentBlock", + "value": "16", + "start":147,"end":153,"loc":{"start":{"line":4,"column":35},"end":{"line":4,"column":41}} + } + ] +} \ No newline at end of file From 9e1b04bf7aa553f10b73f024195656a7ea481178 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 5 Jul 2021 15:44:51 -0400 Subject: [PATCH 29/35] fix flow errors --- packages/babel-parser/src/parser/comments.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-parser/src/parser/comments.js b/packages/babel-parser/src/parser/comments.js index bab22ad79fa0..b8e03c84f32a 100644 --- a/packages/babel-parser/src/parser/comments.js +++ b/packages/babel-parser/src/parser/comments.js @@ -225,7 +225,7 @@ export default class CommentsParser extends BaseParser { * @returns * @memberof CommentsParser */ - resetPreviousNodeTrailingComments(node: N.Node) { + resetPreviousNodeTrailingComments(node: Node) { const { commentStack } = this.state; let i = commentStack.length - 1; if (i < 0) return; From b28aba127660808da357cd7f5fbb6a5a2d038261 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 6 Jul 2021 15:04:21 -0400 Subject: [PATCH 30/35] fix: handle comments when parsing async arrow --- packages/babel-parser/src/parser/comments.js | 4 +- .../babel-parser/src/parser/expression.js | 6 + .../basic/async-arrow-function/input.js | 3 + .../basic/async-arrow-function/output.json | 177 ++++++++++++++++++ .../basic/async-call-expression/input.js | 1 + .../basic/async-call-expression/output.json | 65 +++++++ 6 files changed, 254 insertions(+), 2 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/comments/basic/async-arrow-function/input.js create mode 100644 packages/babel-parser/test/fixtures/comments/basic/async-arrow-function/output.json create mode 100644 packages/babel-parser/test/fixtures/comments/basic/async-call-expression/input.js create mode 100644 packages/babel-parser/test/fixtures/comments/basic/async-call-expression/output.json diff --git a/packages/babel-parser/src/parser/comments.js b/packages/babel-parser/src/parser/comments.js index b8e03c84f32a..04a40b35d87d 100644 --- a/packages/babel-parser/src/parser/comments.js +++ b/packages/babel-parser/src/parser/comments.js @@ -50,10 +50,10 @@ function setTrailingComments(node: Node, comments: Array) { * @param {Node} node * @param {Array} comments */ -function setInnerComments(node: Node, comments: Array) { +export function setInnerComments(node: Node, comments: Array) { if (node.innerComments === undefined) { node.innerComments = comments; - } else { + } else if (comments !== undefined) { node.innerComments.unshift(...comments); } } diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index befb8e533ba6..5bdd2c350951 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -56,6 +56,7 @@ import { } from "../util/expression-scope"; import { Errors, SourceTypeModuleErrors } from "./error"; import type { ParsingError } from "./error"; +import { setInnerComments } from "./comments"; /*:: import type { SourceType } from "../options"; @@ -941,6 +942,7 @@ export default class ExpressionParser extends LValParser { node: N.ArrowFunctionExpression, call: N.CallExpression, ): N.ArrowFunctionExpression { + this.resetPreviousNodeTrailingComments(call); this.expect(tt.arrow); this.parseArrowExpression( node, @@ -948,6 +950,10 @@ export default class ExpressionParser extends LValParser { true, call.extra?.trailingComma, ); + // mark inner comments of `async()` as inner comments of `async () =>` + setInnerComments(node, call.innerComments); + // mark trailing comments of `async` to be inner comments + setInnerComments(node, call.callee.trailingComments); return node; } diff --git a/packages/babel-parser/test/fixtures/comments/basic/async-arrow-function/input.js b/packages/babel-parser/test/fixtures/comments/basic/async-arrow-function/input.js new file mode 100644 index 000000000000..6e27c321e652 --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/async-arrow-function/input.js @@ -0,0 +1,3 @@ +/* 1 */ async /* 2 */ (/* 3 */) /* 4 */=>x; + +/* 1 */ async /* 2 */ (/* 3 */x/* 4 */) /* 5 */ => /* 6 */ {} diff --git a/packages/babel-parser/test/fixtures/comments/basic/async-arrow-function/output.json b/packages/babel-parser/test/fixtures/comments/basic/async-arrow-function/output.json new file mode 100644 index 000000000000..b75f51c4b8d9 --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/async-arrow-function/output.json @@ -0,0 +1,177 @@ +{ + "type": "File", + "start":0,"end":106,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":61}}, + "program": { + "type": "Program", + "start":0,"end":106,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":61}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":8,"end":43,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":43}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 1 ", + "start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7}} + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": " 1 ", + "start":45,"end":52,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":7}} + } + ], + "expression": { + "type": "ArrowFunctionExpression", + "start":8,"end":42,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":42}}, + "innerComments": [ + { + "type": "CommentBlock", + "value": " 2 ", + "start":14,"end":21,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":21}} + }, + { + "type": "CommentBlock", + "value": " 3 ", + "start":23,"end":30,"loc":{"start":{"line":1,"column":23},"end":{"line":1,"column":30}} + }, + { + "type": "CommentBlock", + "value": " 4 ", + "start":32,"end":39,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":39}} + } + ], + "id": null, + "generator": false, + "async": true, + "params": [], + "body": { + "type": "Identifier", + "start":41,"end":42,"loc":{"start":{"line":1,"column":41},"end":{"line":1,"column":42},"identifierName":"x"}, + "name": "x" + } + } + }, + { + "type": "ExpressionStatement", + "start":53,"end":106,"loc":{"start":{"line":3,"column":8},"end":{"line":3,"column":61}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 1 ", + "start":45,"end":52,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":7}} + } + ], + "expression": { + "type": "ArrowFunctionExpression", + "start":53,"end":106,"loc":{"start":{"line":3,"column":8},"end":{"line":3,"column":61}}, + "innerComments": [ + { + "type": "CommentBlock", + "value": " 2 ", + "start":59,"end":66,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":21}} + }, + { + "type": "CommentBlock", + "value": " 5 ", + "start":85,"end":92,"loc":{"start":{"line":3,"column":40},"end":{"line":3,"column":47}} + } + ], + "id": null, + "generator": false, + "async": true, + "params": [ + { + "type": "Identifier", + "start":75,"end":76,"loc":{"start":{"line":3,"column":30},"end":{"line":3,"column":31},"identifierName":"x"}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 3 ", + "start":68,"end":75,"loc":{"start":{"line":3,"column":23},"end":{"line":3,"column":30}} + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": " 4 ", + "start":76,"end":83,"loc":{"start":{"line":3,"column":31},"end":{"line":3,"column":38}} + } + ], + "name": "x" + } + ], + "body": { + "type": "BlockStatement", + "start":104,"end":106,"loc":{"start":{"line":3,"column":59},"end":{"line":3,"column":61}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 6 ", + "start":96,"end":103,"loc":{"start":{"line":3,"column":51},"end":{"line":3,"column":58}} + } + ], + "body": [], + "directives": [] + } + } + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentBlock", + "value": " 1 ", + "start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7}} + }, + { + "type": "CommentBlock", + "value": " 2 ", + "start":14,"end":21,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":21}} + }, + { + "type": "CommentBlock", + "value": " 3 ", + "start":23,"end":30,"loc":{"start":{"line":1,"column":23},"end":{"line":1,"column":30}} + }, + { + "type": "CommentBlock", + "value": " 4 ", + "start":32,"end":39,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":39}} + }, + { + "type": "CommentBlock", + "value": " 1 ", + "start":45,"end":52,"loc":{"start":{"line":3,"column":0},"end":{"line":3,"column":7}} + }, + { + "type": "CommentBlock", + "value": " 2 ", + "start":59,"end":66,"loc":{"start":{"line":3,"column":14},"end":{"line":3,"column":21}} + }, + { + "type": "CommentBlock", + "value": " 3 ", + "start":68,"end":75,"loc":{"start":{"line":3,"column":23},"end":{"line":3,"column":30}} + }, + { + "type": "CommentBlock", + "value": " 4 ", + "start":76,"end":83,"loc":{"start":{"line":3,"column":31},"end":{"line":3,"column":38}} + }, + { + "type": "CommentBlock", + "value": " 5 ", + "start":85,"end":92,"loc":{"start":{"line":3,"column":40},"end":{"line":3,"column":47}} + }, + { + "type": "CommentBlock", + "value": " 6 ", + "start":96,"end":103,"loc":{"start":{"line":3,"column":51},"end":{"line":3,"column":58}} + } + ] +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/comments/basic/async-call-expression/input.js b/packages/babel-parser/test/fixtures/comments/basic/async-call-expression/input.js new file mode 100644 index 000000000000..d50ac29542c6 --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/async-call-expression/input.js @@ -0,0 +1 @@ +async /* 1 */ (/* 2 */) /* 3 */ diff --git a/packages/babel-parser/test/fixtures/comments/basic/async-call-expression/output.json b/packages/babel-parser/test/fixtures/comments/basic/async-call-expression/output.json new file mode 100644 index 000000000000..1abf750eb932 --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/async-call-expression/output.json @@ -0,0 +1,65 @@ +{ + "type": "File", + "start":0,"end":31,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":31}}, + "program": { + "type": "Program", + "start":0,"end":31,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":31}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":23,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":23}}, + "trailingComments": [ + { + "type": "CommentBlock", + "value": " 3 ", + "start":24,"end":31,"loc":{"start":{"line":1,"column":24},"end":{"line":1,"column":31}} + } + ], + "expression": { + "type": "CallExpression", + "start":0,"end":23,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":23}}, + "innerComments": [ + { + "type": "CommentBlock", + "value": " 2 ", + "start":15,"end":22,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":22}} + } + ], + "callee": { + "type": "Identifier", + "start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5},"identifierName":"async"}, + "trailingComments": [ + { + "type": "CommentBlock", + "value": " 1 ", + "start":6,"end":13,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":13}} + } + ], + "name": "async" + }, + "arguments": [] + } + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentBlock", + "value": " 1 ", + "start":6,"end":13,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":13}} + }, + { + "type": "CommentBlock", + "value": " 2 ", + "start":15,"end":22,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":22}} + }, + { + "type": "CommentBlock", + "value": " 3 ", + "start":24,"end":31,"loc":{"start":{"line":1,"column":24},"end":{"line":1,"column":31}} + } + ] +} \ No newline at end of file From 6ac270c83485fe9ed2e268f4b5d8026bb6a427d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 6 Jul 2021 15:29:02 -0400 Subject: [PATCH 31/35] fix: handle comments when "static" is a class modifier --- packages/babel-parser/src/parser/comments.js | 8 +- packages/babel-parser/src/parser/statement.js | 1 + .../class-method-static-generator/input.js | 2 + .../class-method-static-generator/output.json | 175 ++++++++++++++++++ .../basic/class-static-block/input.js | 1 + .../basic/class-static-block/options.json | 3 + .../basic/class-static-block/output.json | 110 +++++++++++ 7 files changed, 296 insertions(+), 4 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/comments/basic/class-method-static-generator/input.js create mode 100644 packages/babel-parser/test/fixtures/comments/basic/class-method-static-generator/output.json create mode 100644 packages/babel-parser/test/fixtures/comments/basic/class-static-block/input.js create mode 100644 packages/babel-parser/test/fixtures/comments/basic/class-static-block/options.json create mode 100644 packages/babel-parser/test/fixtures/comments/basic/class-static-block/output.json diff --git a/packages/babel-parser/src/parser/comments.js b/packages/babel-parser/src/parser/comments.js index 04a40b35d87d..ee455fe2f432 100644 --- a/packages/babel-parser/src/parser/comments.js +++ b/packages/babel-parser/src/parser/comments.js @@ -213,10 +213,10 @@ export default class CommentsParser extends BaseParser { /** * Reset previous node trailing comments. Used in object / class - * property parsing. We parse `async`, `set` and `get` as an identifier - * but may reinterepret it into an async/accessor method later. In this case - * the identifier is not part of the AST and we should sync the knowledge - * to commentStacks + * property parsing. We parse `async`, `static`, `set` and `get` + * as an identifier but may reinterepret it into an async/static/accessor + * method later. In this case the identifier is not part of the AST and we + * should sync the knowledge to commentStacks * * For performance we do not check trailing node and we assume `node` is the * last finished node before current token. diff --git a/packages/babel-parser/src/parser/statement.js b/packages/babel-parser/src/parser/statement.js index 1571dd1d3a1a..fb51dd01587c 100644 --- a/packages/babel-parser/src/parser/statement.js +++ b/packages/babel-parser/src/parser/statement.js @@ -1375,6 +1375,7 @@ export default class StatementParser extends ExpressionParser { classBody.body.push(this.parseClassProperty(prop)); return true; } + this.resetPreviousNodeTrailingComments(key); return false; } diff --git a/packages/babel-parser/test/fixtures/comments/basic/class-method-static-generator/input.js b/packages/babel-parser/test/fixtures/comments/basic/class-method-static-generator/input.js new file mode 100644 index 000000000000..11083698a225 --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/class-method-static-generator/input.js @@ -0,0 +1,2 @@ +(class /* 1 */ { /* 2 */ static /* 3 */ * /* 4 */ f /* 5 */ (/* 6 */) /* 7 */ { /* 8 */ } /* 9 */ } /* 10 */); + diff --git a/packages/babel-parser/test/fixtures/comments/basic/class-method-static-generator/output.json b/packages/babel-parser/test/fixtures/comments/basic/class-method-static-generator/output.json new file mode 100644 index 000000000000..b6336d17844f --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/class-method-static-generator/output.json @@ -0,0 +1,175 @@ +{ + "type": "File", + "start":0,"end":110,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":110}}, + "program": { + "type": "Program", + "start":0,"end":110,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":110}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":110,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":110}}, + "expression": { + "type": "ClassExpression", + "start":1,"end":99,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":99}}, + "trailingComments": [ + { + "type": "CommentBlock", + "value": " 10 ", + "start":100,"end":108,"loc":{"start":{"line":1,"column":100},"end":{"line":1,"column":108}} + } + ], + "extra": { + "parenthesized": true, + "parenStart": 0 + }, + "id": null, + "superClass": null, + "body": { + "type": "ClassBody", + "start":15,"end":99,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":99}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 1 ", + "start":7,"end":14,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":14}} + } + ], + "body": [ + { + "type": "ClassMethod", + "start":25,"end":89,"loc":{"start":{"line":1,"column":25},"end":{"line":1,"column":89}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 2 ", + "start":17,"end":24,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":24}} + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": " 9 ", + "start":90,"end":97,"loc":{"start":{"line":1,"column":90},"end":{"line":1,"column":97}} + } + ], + "innerComments": [ + { + "type": "CommentBlock", + "value": " 3 ", + "start":32,"end":39,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":39}} + }, + { + "type": "CommentBlock", + "value": " 6 ", + "start":61,"end":68,"loc":{"start":{"line":1,"column":61},"end":{"line":1,"column":68}} + } + ], + "static": true, + "kind": "method", + "key": { + "type": "Identifier", + "start":50,"end":51,"loc":{"start":{"line":1,"column":50},"end":{"line":1,"column":51},"identifierName":"f"}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 4 ", + "start":42,"end":49,"loc":{"start":{"line":1,"column":42},"end":{"line":1,"column":49}} + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": " 5 ", + "start":52,"end":59,"loc":{"start":{"line":1,"column":52},"end":{"line":1,"column":59}} + } + ], + "name": "f" + }, + "computed": false, + "id": null, + "generator": true, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start":78,"end":89,"loc":{"start":{"line":1,"column":78},"end":{"line":1,"column":89}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 7 ", + "start":70,"end":77,"loc":{"start":{"line":1,"column":70},"end":{"line":1,"column":77}} + } + ], + "innerComments": [ + { + "type": "CommentBlock", + "value": " 8 ", + "start":80,"end":87,"loc":{"start":{"line":1,"column":80},"end":{"line":1,"column":87}} + } + ], + "body": [], + "directives": [] + } + } + ] + } + } + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentBlock", + "value": " 1 ", + "start":7,"end":14,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":14}} + }, + { + "type": "CommentBlock", + "value": " 2 ", + "start":17,"end":24,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":24}} + }, + { + "type": "CommentBlock", + "value": " 3 ", + "start":32,"end":39,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":39}} + }, + { + "type": "CommentBlock", + "value": " 4 ", + "start":42,"end":49,"loc":{"start":{"line":1,"column":42},"end":{"line":1,"column":49}} + }, + { + "type": "CommentBlock", + "value": " 5 ", + "start":52,"end":59,"loc":{"start":{"line":1,"column":52},"end":{"line":1,"column":59}} + }, + { + "type": "CommentBlock", + "value": " 6 ", + "start":61,"end":68,"loc":{"start":{"line":1,"column":61},"end":{"line":1,"column":68}} + }, + { + "type": "CommentBlock", + "value": " 7 ", + "start":70,"end":77,"loc":{"start":{"line":1,"column":70},"end":{"line":1,"column":77}} + }, + { + "type": "CommentBlock", + "value": " 8 ", + "start":80,"end":87,"loc":{"start":{"line":1,"column":80},"end":{"line":1,"column":87}} + }, + { + "type": "CommentBlock", + "value": " 9 ", + "start":90,"end":97,"loc":{"start":{"line":1,"column":90},"end":{"line":1,"column":97}} + }, + { + "type": "CommentBlock", + "value": " 10 ", + "start":100,"end":108,"loc":{"start":{"line":1,"column":100},"end":{"line":1,"column":108}} + } + ] +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/comments/basic/class-static-block/input.js b/packages/babel-parser/test/fixtures/comments/basic/class-static-block/input.js new file mode 100644 index 000000000000..00b068774ced --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/class-static-block/input.js @@ -0,0 +1 @@ +(class /* 1 */ { /* 2 */ static /* 3 */ {/* 4 */} /* 5 */ } /* 6 */); diff --git a/packages/babel-parser/test/fixtures/comments/basic/class-static-block/options.json b/packages/babel-parser/test/fixtures/comments/basic/class-static-block/options.json new file mode 100644 index 000000000000..70adcb5e62d3 --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/class-static-block/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["classStaticBlock"] +} diff --git a/packages/babel-parser/test/fixtures/comments/basic/class-static-block/output.json b/packages/babel-parser/test/fixtures/comments/basic/class-static-block/output.json new file mode 100644 index 000000000000..e118bbb4f167 --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/class-static-block/output.json @@ -0,0 +1,110 @@ +{ + "type": "File", + "start":0,"end":69,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":69}}, + "program": { + "type": "Program", + "start":0,"end":69,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":69}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":0,"end":69,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":69}}, + "expression": { + "type": "ClassExpression", + "start":1,"end":59,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":59}}, + "trailingComments": [ + { + "type": "CommentBlock", + "value": " 6 ", + "start":60,"end":67,"loc":{"start":{"line":1,"column":60},"end":{"line":1,"column":67}} + } + ], + "extra": { + "parenthesized": true, + "parenStart": 0 + }, + "id": null, + "superClass": null, + "body": { + "type": "ClassBody", + "start":15,"end":59,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":59}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 1 ", + "start":7,"end":14,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":14}} + } + ], + "body": [ + { + "type": "StaticBlock", + "start":25,"end":49,"loc":{"start":{"line":1,"column":25},"end":{"line":1,"column":49}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 2 ", + "start":17,"end":24,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":24}} + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": " 5 ", + "start":50,"end":57,"loc":{"start":{"line":1,"column":50},"end":{"line":1,"column":57}} + } + ], + "innerComments": [ + { + "type": "CommentBlock", + "value": " 3 ", + "start":32,"end":39,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":39}} + }, + { + "type": "CommentBlock", + "value": " 4 ", + "start":41,"end":48,"loc":{"start":{"line":1,"column":41},"end":{"line":1,"column":48}} + } + ], + "body": [] + } + ] + } + } + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentBlock", + "value": " 1 ", + "start":7,"end":14,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":14}} + }, + { + "type": "CommentBlock", + "value": " 2 ", + "start":17,"end":24,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":24}} + }, + { + "type": "CommentBlock", + "value": " 3 ", + "start":32,"end":39,"loc":{"start":{"line":1,"column":32},"end":{"line":1,"column":39}} + }, + { + "type": "CommentBlock", + "value": " 4 ", + "start":41,"end":48,"loc":{"start":{"line":1,"column":41},"end":{"line":1,"column":48}} + }, + { + "type": "CommentBlock", + "value": " 5 ", + "start":50,"end":57,"loc":{"start":{"line":1,"column":50},"end":{"line":1,"column":57}} + }, + { + "type": "CommentBlock", + "value": " 6 ", + "start":60,"end":67,"loc":{"start":{"line":1,"column":60},"end":{"line":1,"column":67}} + } + ] +} \ No newline at end of file From cdf6b4f5403a9d585f827ba887c175a2591a1d9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 6 Jul 2021 15:43:30 -0400 Subject: [PATCH 32/35] fix flow errors --- packages/babel-parser/src/parser/comments.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-parser/src/parser/comments.js b/packages/babel-parser/src/parser/comments.js index ee455fe2f432..67d8db6fa04f 100644 --- a/packages/babel-parser/src/parser/comments.js +++ b/packages/babel-parser/src/parser/comments.js @@ -50,7 +50,7 @@ function setTrailingComments(node: Node, comments: Array) { * @param {Node} node * @param {Array} comments */ -export function setInnerComments(node: Node, comments: Array) { +export function setInnerComments(node: Node, comments: Array | void) { if (node.innerComments === undefined) { node.innerComments = comments; } else if (comments !== undefined) { From fb29d7577beaba1db511be2389a124dac961165a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Wed, 7 Jul 2021 10:17:30 -0400 Subject: [PATCH 33/35] fix: handle comments when parsing async function/do --- .../babel-parser/src/parser/expression.js | 12 ++- .../basic/async-do-expression/input.js | 1 + .../basic/async-do-expression/options.json | 3 + .../basic/async-do-expression/output.json | 90 +++++++++++++++++++ .../comments/basic/async-function/input.js | 1 + .../comments/basic/async-function/output.json | 57 ++++++++++++ 6 files changed, 161 insertions(+), 3 deletions(-) create mode 100644 packages/babel-parser/test/fixtures/comments/basic/async-do-expression/input.js create mode 100644 packages/babel-parser/test/fixtures/comments/basic/async-do-expression/options.json create mode 100644 packages/babel-parser/test/fixtures/comments/basic/async-do-expression/output.json create mode 100644 packages/babel-parser/test/fixtures/comments/basic/async-function/input.js create mode 100644 packages/babel-parser/test/fixtures/comments/basic/async-function/output.json diff --git a/packages/babel-parser/src/parser/expression.js b/packages/babel-parser/src/parser/expression.js index 5bdd2c350951..82b531ff42a8 100644 --- a/packages/babel-parser/src/parser/expression.js +++ b/packages/babel-parser/src/parser/expression.js @@ -1008,6 +1008,7 @@ export default class ExpressionParser extends LValParser { if (!containsEsc && id.name === "async" && !this.canInsertSemicolon()) { if (this.match(tt._function)) { + this.resetPreviousNodeTrailingComments(id); this.next(); return this.parseFunction( this.startNodeAtNode(id), @@ -1019,13 +1020,19 @@ export default class ExpressionParser extends LValParser { // arrow function. (Peeking ahead for "=" lets us avoid a more // expensive full-token lookahead on this common path.) if (this.lookaheadCharCode() === charCodes.equalsTo) { - return this.parseAsyncArrowUnaryFunction(id); + // although `id` is not used in async arrow unary function, + // we don't need to reset `async`'s trailing comments because + // it will be attached to the upcoming async arrow binding identifier + return this.parseAsyncArrowUnaryFunction( + this.startNodeAtNode(id), + ); } else { // Otherwise, treat "async" as an identifier and let calling code // deal with the current tt.name token. return id; } } else if (this.match(tt._do)) { + this.resetPreviousNodeTrailingComments(id); return this.parseDo(this.startNodeAtNode(id), true); } } @@ -1198,8 +1205,7 @@ export default class ExpressionParser extends LValParser { } // async [no LineTerminator here] AsyncArrowBindingIdentifier[?Yield] [no LineTerminator here] => AsyncConciseBody[?In] - parseAsyncArrowUnaryFunction(id: N.Expression): N.ArrowFunctionExpression { - const node = this.startNodeAtNode(id); + parseAsyncArrowUnaryFunction(node: N.Node): N.ArrowFunctionExpression { // We don't need to push a new ParameterDeclarationScope here since we are sure // 1) it is an async arrow, 2) no biding pattern is allowed in params this.prodParam.enter(functionFlags(true, this.prodParam.hasYield)); diff --git a/packages/babel-parser/test/fixtures/comments/basic/async-do-expression/input.js b/packages/babel-parser/test/fixtures/comments/basic/async-do-expression/input.js new file mode 100644 index 000000000000..b61e768397a7 --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/async-do-expression/input.js @@ -0,0 +1 @@ +/* 1 */async/* 2 */do/* 3 */{/* 4 */}/* 5 */ diff --git a/packages/babel-parser/test/fixtures/comments/basic/async-do-expression/options.json b/packages/babel-parser/test/fixtures/comments/basic/async-do-expression/options.json new file mode 100644 index 000000000000..94fd7ea36118 --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/async-do-expression/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["asyncDoExpressions", "doExpressions"] +} diff --git a/packages/babel-parser/test/fixtures/comments/basic/async-do-expression/output.json b/packages/babel-parser/test/fixtures/comments/basic/async-do-expression/output.json new file mode 100644 index 000000000000..c82c2cfdbacc --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/async-do-expression/output.json @@ -0,0 +1,90 @@ +{ + "type": "File", + "start":0,"end":44,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":44}}, + "program": { + "type": "Program", + "start":0,"end":44,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":44}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "ExpressionStatement", + "start":7,"end":37,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":37}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 1 ", + "start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7}} + } + ], + "trailingComments": [ + { + "type": "CommentBlock", + "value": " 5 ", + "start":37,"end":44,"loc":{"start":{"line":1,"column":37},"end":{"line":1,"column":44}} + } + ], + "expression": { + "type": "DoExpression", + "start":19,"end":37,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":37}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 2 ", + "start":12,"end":19,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":19}} + } + ], + "async": true, + "body": { + "type": "BlockStatement", + "start":28,"end":37,"loc":{"start":{"line":1,"column":28},"end":{"line":1,"column":37}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 3 ", + "start":21,"end":28,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":28}} + } + ], + "innerComments": [ + { + "type": "CommentBlock", + "value": " 4 ", + "start":29,"end":36,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":36}} + } + ], + "body": [], + "directives": [] + } + } + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentBlock", + "value": " 1 ", + "start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7}} + }, + { + "type": "CommentBlock", + "value": " 2 ", + "start":12,"end":19,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":19}} + }, + { + "type": "CommentBlock", + "value": " 3 ", + "start":21,"end":28,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":28}} + }, + { + "type": "CommentBlock", + "value": " 4 ", + "start":29,"end":36,"loc":{"start":{"line":1,"column":29},"end":{"line":1,"column":36}} + }, + { + "type": "CommentBlock", + "value": " 5 ", + "start":37,"end":44,"loc":{"start":{"line":1,"column":37},"end":{"line":1,"column":44}} + } + ] +} \ No newline at end of file diff --git a/packages/babel-parser/test/fixtures/comments/basic/async-function/input.js b/packages/babel-parser/test/fixtures/comments/basic/async-function/input.js new file mode 100644 index 000000000000..561c4d2562b1 --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/async-function/input.js @@ -0,0 +1 @@ +/* 1 */async/* 2 */function f () {} diff --git a/packages/babel-parser/test/fixtures/comments/basic/async-function/output.json b/packages/babel-parser/test/fixtures/comments/basic/async-function/output.json new file mode 100644 index 000000000000..1cc9422626cb --- /dev/null +++ b/packages/babel-parser/test/fixtures/comments/basic/async-function/output.json @@ -0,0 +1,57 @@ +{ + "type": "File", + "start":0,"end":35,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":35}}, + "program": { + "type": "Program", + "start":0,"end":35,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":35}}, + "sourceType": "script", + "interpreter": null, + "body": [ + { + "type": "FunctionDeclaration", + "start":7,"end":35,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":35}}, + "leadingComments": [ + { + "type": "CommentBlock", + "value": " 1 ", + "start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7}} + } + ], + "innerComments": [ + { + "type": "CommentBlock", + "value": " 2 ", + "start":12,"end":19,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":19}} + } + ], + "id": { + "type": "Identifier", + "start":28,"end":29,"loc":{"start":{"line":1,"column":28},"end":{"line":1,"column":29},"identifierName":"f"}, + "name": "f" + }, + "generator": false, + "async": true, + "params": [], + "body": { + "type": "BlockStatement", + "start":33,"end":35,"loc":{"start":{"line":1,"column":33},"end":{"line":1,"column":35}}, + "body": [], + "directives": [] + } + } + ], + "directives": [] + }, + "comments": [ + { + "type": "CommentBlock", + "value": " 1 ", + "start":0,"end":7,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":7}} + }, + { + "type": "CommentBlock", + "value": " 2 ", + "start":12,"end":19,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":19}} + } + ] +} From 430b3e0e3204b059c314a530dd708bcfbd078f79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 6 Jul 2021 21:12:26 -0400 Subject: [PATCH 34/35] refactor: simplify resetPreviousNodeTrailingComments --- packages/babel-parser/src/parser/comments.js | 21 +++++++++----------- 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/packages/babel-parser/src/parser/comments.js b/packages/babel-parser/src/parser/comments.js index 67d8db6fa04f..e6dc327e1f38 100644 --- a/packages/babel-parser/src/parser/comments.js +++ b/packages/babel-parser/src/parser/comments.js @@ -218,8 +218,12 @@ export default class CommentsParser extends BaseParser { * method later. In this case the identifier is not part of the AST and we * should sync the knowledge to commentStacks * - * For performance we do not check trailing node and we assume `node` is the - * last finished node before current token. + * For example, when parsing */ + // async /* 1 */ function f() {} + /* + * the comment whitespace "* 1 *" has leading node Identifier(async). When + * we see the function token, we create a Function node and mark "* 1 *" as + * inner comments. So "* 1 *" should be detached from the Identifier node. * * @param {N.Node} node the last finished AST node _before_ current token * @returns @@ -227,16 +231,9 @@ export default class CommentsParser extends BaseParser { */ resetPreviousNodeTrailingComments(node: Node) { const { commentStack } = this.state; - let i = commentStack.length - 1; - if (i < 0) return; - let commentWS = commentStack[i]; - if (commentWS.leadingNode === node) { - commentWS.leadingNode = null; - return; - } - i--; - if (i < 0) return; - commentWS = commentStack[i]; + const { length } = commentStack; + if (length === 0) return; + const commentWS = commentStack[length - 1]; if (commentWS.leadingNode === node) { commentWS.leadingNode = null; } From 4f076f8f36c1c08972e2306dc9a66bc8aa3fc3ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Wed, 7 Jul 2021 10:24:56 -0400 Subject: [PATCH 35/35] update test fixtures --- .../fixtures/comments/basic/async-do-expression/output.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/babel-parser/test/fixtures/comments/basic/async-do-expression/output.json b/packages/babel-parser/test/fixtures/comments/basic/async-do-expression/output.json index c82c2cfdbacc..481331445e28 100644 --- a/packages/babel-parser/test/fixtures/comments/basic/async-do-expression/output.json +++ b/packages/babel-parser/test/fixtures/comments/basic/async-do-expression/output.json @@ -26,8 +26,8 @@ ], "expression": { "type": "DoExpression", - "start":19,"end":37,"loc":{"start":{"line":1,"column":19},"end":{"line":1,"column":37}}, - "leadingComments": [ + "start":7,"end":37,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":37}}, + "innerComments": [ { "type": "CommentBlock", "value": " 2 ",