From d94c813f86e666d2e4da3ff0045c08ed40d8d9a3 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Mon, 6 Feb 2023 10:54:23 +0800 Subject: [PATCH] Use `printStatementSequence` to print babel directives (#14257) --- src/language-js/print/block.js | 49 +++++++++++++++++------------- src/language-js/print/statement.js | 3 +- src/language-js/printer-estree.js | 2 +- 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/src/language-js/print/block.js b/src/language-js/print/block.js index 3827b1e0f..043862d17 100644 --- a/src/language-js/print/block.js +++ b/src/language-js/print/block.js @@ -10,6 +10,11 @@ import { printStatementSequence } from "./statement.js"; /** @typedef {import("../../document/builders.js").Doc} Doc */ +/* +- `BlockStatement` +- `StaticBlock` +- `TSModuleBlock` (TypeScript) +*/ function printBlock(path, options, print) { const { node } = path; const parts = []; @@ -52,44 +57,46 @@ function printBlock(path, options, print) { return parts; } +/* +- `Program` +- `BlockStatement` +- `StaticBlock` +- `TSModuleBlock` (TypeScript) +*/ function printBlockBody(path, options, print) { const { node } = path; - const nodeHasDirectives = isNonEmptyArray(node.directives); - const nodeHasBody = node.body.some((node) => node.type !== "EmptyStatement"); - const nodeHasComment = hasComment(node, CommentCheckFlags.Dangling); + const hasDirectives = isNonEmptyArray(node.directives); + const hasBody = node.body.some((node) => node.type !== "EmptyStatement"); + const hasDanglingComments = hasComment(node, CommentCheckFlags.Dangling); - if (!nodeHasDirectives && !nodeHasBody && !nodeHasComment) { + if (!hasDirectives && !hasBody && !hasDanglingComments) { return ""; } const parts = []; - // Babel 6 - if (nodeHasDirectives) { - path.each(({ node, isLast }) => { - parts.push(print()); - if (!isLast || nodeHasBody || nodeHasComment) { + // Babel + if (hasDirectives) { + parts.push(printStatementSequence(path, options, print, "directives")); + + if (hasBody || hasDanglingComments) { + parts.push(hardline); + if (isNextLineEmpty(node.directives.at(-1), options)) { parts.push(hardline); - if (isNextLineEmpty(node, options)) { - parts.push(hardline); - } } - }, "directives"); + } } - if (nodeHasBody) { - parts.push(printStatementSequence(path, options, print)); + if (hasBody) { + parts.push(printStatementSequence(path, options, print, "body")); } - if (nodeHasComment) { + if (hasDanglingComments) { parts.push(printDanglingComments(path, options)); } - if (node.type === "Program") { - const { parent } = path; - if (!parent || parent.type !== "ModuleExpression") { - parts.push(hardline); - } + if (node.type === "Program" && path.parent?.type !== "ModuleExpression") { + parts.push(hardline); } return parts; diff --git a/src/language-js/print/statement.js b/src/language-js/print/statement.js index 6b7c2e4c7..ea0aaedf3 100644 --- a/src/language-js/print/statement.js +++ b/src/language-js/print/statement.js @@ -16,10 +16,9 @@ import { shouldPrintParamsWithoutParens } from "./function.js"; * @typedef {import("../../common/ast-path.js")} AstPath */ -function printStatementSequence(path, options, print) { +function printStatementSequence(path, options, print, property) { const { node } = path; const parts = []; - const property = node.type === "SwitchCase" ? "consequent" : "body"; const lastStatement = getLastStatement(node[property]); path.each(({ node }) => { diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index 21abfe0d6..baedbee9d 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -717,7 +717,7 @@ function printPathNoParens(path, options, print, args) { ); if (consequent.length > 0) { - const cons = printStatementSequence(path, options, print); + const cons = printStatementSequence(path, options, print, "consequent"); parts.push( consequent.length === 1 && consequent[0].type === "BlockStatement"