Refactor Enum print (#14045)

This commit is contained in:
fisker Cheung authored and GitHub committed 2023-01-04 16:25:41 +08:00
1 parent acb7354f85
commit 2f72344944
3 files changed
+13 -33

No files matched your search

+1 -1
View File
@@ -209,4 +209,4 @@ function printArrayItemsConcisely(path, options, print, trailingComma) {
return fill(parts);
}
export { printArray, printArrayItems, isConciselyPrintedArray };
export { printArray, isConciselyPrintedArray };
+2 -30
View File
@@ -1,36 +1,8 @@
import { hardline, softline, group, indent } from "../../document/builders.js";
import { printDanglingComments } from "../../main/comments.js";
import { shouldPrintComma } from "../utils/index.js";
import { printArrayItems } from "./array.js";
import { printTypeScriptModifiers } from "./misc.js";
function printEnumMemberList(path, print, options) {
const {
node: { members, hasUnknownMembers },
} = path;
if (members.length === 0 && !hasUnknownMembers) {
return [printDanglingComments(path, options), softline];
}
return [
indent([
...(members.length > 0
? [
hardline,
printArrayItems(path, options, "members", print),
hasUnknownMembers || shouldPrintComma(options) ? "," : "",
]
: []),
...(hasUnknownMembers ? [hardline, "..."] : []),
]),
printDanglingComments(path, options, /* sameIndent */ true),
hardline,
];
}
import { printObject } from "./object.js";
function printEnumMembers(path, print, options) {
return group(["{", printEnumMemberList(path, print, options), "}"]);
return printObject(path, options, print);
}
/*
+10 -2
View File
@@ -33,8 +33,14 @@ function printObject(path, options, print) {
const { node } = path;
const isTypeAnnotation = node.type === "ObjectTypeAnnotation";
const isEnumBody =
node.type === "TSEnumDeclaration" ||
node.type === "EnumBooleanBody" ||
node.type === "EnumNumberBody" ||
node.type === "EnumStringBody" ||
node.type === "EnumSymbolBody";
const fields = [
node.type === "TSTypeLiteral"
node.type === "TSTypeLiteral" || isEnumBody
? "members"
: node.type === "TSInterfaceBody"
? "body"
@@ -72,6 +78,7 @@ function printObject(path, options, print) {
path.getName() === "body";
const shouldBreak =
node.type === "TSInterfaceBody" ||
isEnumBody ||
isFlowInterfaceLikeBody ||
(node.type === "ObjectPattern" &&
parent.type !== "FunctionDeclaration" &&
@@ -124,7 +131,7 @@ function printObject(path, options, print) {
return result;
});
if (node.inexact) {
if (node.inexact || node.hasUnknownMembers) {
let printed;
if (hasComment(node, CommentCheckFlags.Dangling)) {
const hasLineComments = hasComment(node, CommentCheckFlags.Line);
@@ -151,6 +158,7 @@ function printObject(path, options, print) {
const canHaveTrailingSeparator = !(
node.inexact ||
node.hasUnknownMembers ||
(lastElem &&
(lastElem.type === "RestElement" ||
((lastElem.type === "TSPropertySignature" ||