From aadb1e57ef15ec839a5337d1fd0c8cde86da2573 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Mon, 22 May 2023 16:48:49 +0200 Subject: [PATCH] Review --- packages/babel-parser/src/parser/comments.ts | 24 +++++++++---------- packages/babel-parser/src/parser/statement.ts | 2 +- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/packages/babel-parser/src/parser/comments.ts b/packages/babel-parser/src/parser/comments.ts index a81dfd6d58af..44022899d492 100644 --- a/packages/babel-parser/src/parser/comments.ts +++ b/packages/babel-parser/src/parser/comments.ts @@ -1,7 +1,7 @@ /*:: declare var invariant; */ import BaseParser from "./base"; -import type { Comment, Node } from "../types"; +import type { Comment, Node, Identifier } from "../types"; import * as charCodes from "charcodes"; import type { Undone } from "./node"; @@ -248,11 +248,11 @@ export default class CommentsParser extends BaseParser { /* eslint-disable no-irregular-whitespace */ /** - * Reset previous node leading comments. Used in import phase modifiers - * parsing. We parse `module` in `import module foo from ...` as an - * identifier but may reinterpret it into a phase modifier later. In this - * case the identifier is not part of the AST and we should sync the - * knowledge to commentStacks + * Reset previous node leading comments, assuming that `node` is a + * single-token node. Used in import phase modifiers parsing. We parse + * `module` in `import module foo from ...` as an identifier but may + * reinterpret it into a phase modifier later. In this case the identifier is + * not part of the AST and we should sync the knowledge to commentStacks * * For example, when parsing * ``` @@ -266,17 +266,15 @@ export default class CommentsParser extends BaseParser { * @param node the last finished AST node _before_ current token */ /* eslint-enable no-irregular-whitespace */ - resetPreviousNodeLeadingComments(node: Node) { + resetPreviousIdentifierLeadingComments(node: Identifier) { const { commentStack } = this.state; const { length } = commentStack; if (length === 0) return; - for (let i = length - 1; i >= 0; i--) { - const commentWS = commentStack[i]; - if (commentWS.trailingNode === node) { - commentWS.trailingNode = null; - } - if (commentWS.end < node.start) break; + if (commentStack[length - 1].trailingNode === node) { + commentStack[length - 1].trailingNode = null; + } else if (length >= 2 && commentStack[length - 2].trailingNode === node) { + commentStack[length - 2].trailingNode = null; } } diff --git a/packages/babel-parser/src/parser/statement.ts b/packages/babel-parser/src/parser/statement.ts index 2e2a24a89e6b..6bbab77998ed 100644 --- a/packages/babel-parser/src/parser/statement.ts +++ b/packages/babel-parser/src/parser/statement.ts @@ -3003,7 +3003,7 @@ export default abstract class StatementParser extends ExpressionParser { type !== tt.comma; if (isImportPhase) { - this.resetPreviousNodeLeadingComments(phaseIdentifier); + this.resetPreviousIdentifierLeadingComments(phaseIdentifier); this.applyImportPhase( node as Undone, isExport,