Use printStatementSequence to print babel directives (#14257)
This commit is contained in:
1 parent
bad0906a8d
commit
d94c813f86
3 files changed
+30
-24
No files matched your search
@@ -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;
|
||||
|
||||
@@ -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 }) => {
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in new issue
Block a user