Add printTrailingComma (#18773)
This commit is contained in:
1 parent
e8413ba558
commit
e8bcedaacd
11 files changed
+45
-50
No files matched your search
@@ -21,7 +21,7 @@ import {
|
||||
isObjectExpression,
|
||||
isTupleType,
|
||||
} from "../utilities/node-types.js";
|
||||
import { shouldPrintComma } from "../utilities/should-print-comma.js";
|
||||
import { shouldPrintTrailingComma } from "../utilities/should-print-trailing-comma.js";
|
||||
import {
|
||||
printDanglingCommentsInList,
|
||||
printOptionalToken,
|
||||
@@ -92,7 +92,7 @@ function printArray(path, options, print) {
|
||||
? ""
|
||||
: needsForcedTrailingComma
|
||||
? ","
|
||||
: !shouldPrintComma(options)
|
||||
: !shouldPrintTrailingComma(options)
|
||||
? ""
|
||||
: shouldUseConciseFormatting
|
||||
? ifBreak(",", "", { groupId })
|
||||
|
||||
@@ -25,10 +25,10 @@ import {
|
||||
isJsxElement,
|
||||
isObjectExpression,
|
||||
} from "../utilities/node-types.js";
|
||||
import { shouldPrintComma } from "../utilities/should-print-comma.js";
|
||||
import { startsWithNoLookaheadToken } from "../utilities/starts-with-no-lookahead-token.js";
|
||||
import { printReturnType, shouldPrintParamsWithoutParens } from "./function.js";
|
||||
import { printFunctionParameters } from "./function-parameters.js";
|
||||
import { printTrailingComma } from "./miscellaneous.js";
|
||||
|
||||
/**
|
||||
* @import AstPath from "../../common/ast-path.js"
|
||||
@@ -291,8 +291,9 @@ function printArrowFunctionBody(
|
||||
) {
|
||||
const { node, parent } = path;
|
||||
|
||||
const trailingComma =
|
||||
args.expandLastArg && shouldPrintComma(options, "all") ? ifBreak(",") : "";
|
||||
const trailingComma = args.expandLastArg
|
||||
? printTrailingComma(options, "all")
|
||||
: "";
|
||||
|
||||
// if the arrow function is expanded as last argument, we are adding a
|
||||
// level of indentation and need to add a softline to align the closing )
|
||||
|
||||
@@ -4,7 +4,6 @@ import {
|
||||
conditionalGroup,
|
||||
group,
|
||||
hardline,
|
||||
ifBreak,
|
||||
indent,
|
||||
line,
|
||||
softline,
|
||||
@@ -34,10 +33,12 @@ import {
|
||||
isRegExpLiteral,
|
||||
isStringLiteral,
|
||||
} from "../utilities/node-types.js";
|
||||
import { shouldPrintComma } from "../utilities/should-print-comma.js";
|
||||
import { stripChainElementWrappers } from "../utilities/strip-chain-element-wrappers.js";
|
||||
import { isConciselyPrintedArray } from "./array.js";
|
||||
import { printDanglingCommentsInList } from "./miscellaneous.js";
|
||||
import {
|
||||
printDanglingCommentsInList,
|
||||
printTrailingComma,
|
||||
} from "./miscellaneous.js";
|
||||
|
||||
/*
|
||||
- `NewExpression`
|
||||
@@ -89,20 +90,19 @@ function printCallArguments(path, options, print) {
|
||||
printedArguments.push(argDoc);
|
||||
});
|
||||
|
||||
const maybeTrailingComma =
|
||||
const trailingComma =
|
||||
// Angular does not allow trailing comma
|
||||
path.root.type !== "NGRoot" &&
|
||||
// Dynamic imports cannot have trailing commas
|
||||
node.type !== "ImportExpression" &&
|
||||
node.type !== "TSImportType" &&
|
||||
node.type !== "TSExternalModuleReference" &&
|
||||
shouldPrintComma(options, "all")
|
||||
? ","
|
||||
node.type !== "TSExternalModuleReference"
|
||||
? printTrailingComma(options, "all")
|
||||
: "";
|
||||
|
||||
function allArgsBrokenOut() {
|
||||
return group(
|
||||
["(", indent([line, ...printedArguments]), maybeTrailingComma, line, ")"],
|
||||
["(", indent([line, ...printedArguments]), trailingComma, line, ")"],
|
||||
{ shouldBreak: true },
|
||||
);
|
||||
}
|
||||
@@ -187,7 +187,7 @@ function printCallArguments(path, options, print) {
|
||||
const contents = [
|
||||
"(",
|
||||
indent([softline, ...printedArguments]),
|
||||
ifBreak(maybeTrailingComma),
|
||||
trailingComma,
|
||||
softline,
|
||||
")",
|
||||
];
|
||||
|
||||
@@ -17,8 +17,8 @@ import {
|
||||
} from "../utilities/comments.js";
|
||||
import { createTypeCheckFunction } from "../utilities/create-type-check-function.js";
|
||||
import { isNextLineEmpty } from "../utilities/is-next-line-empty.js";
|
||||
import { shouldPrintComma } from "../utilities/should-print-comma.js";
|
||||
import { shouldHugTheOnlyParameter } from "./function-parameters.js";
|
||||
import { printTrailingComma } from "./miscellaneous.js";
|
||||
|
||||
/*
|
||||
- `ClassBody`
|
||||
@@ -49,8 +49,8 @@ function printClassBody(path, options, print) {
|
||||
|
||||
if (parent.inexact || !isLast) {
|
||||
parts.push(",");
|
||||
} else if (shouldPrintComma(options)) {
|
||||
parts.push(ifBreak(","));
|
||||
} else {
|
||||
parts.push(printTrailingComma(options));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ import { ArgExpansionBailout } from "../../common/errors.js";
|
||||
import {
|
||||
group,
|
||||
hardline,
|
||||
ifBreak,
|
||||
indent,
|
||||
line,
|
||||
removeLines,
|
||||
@@ -25,9 +24,11 @@ import {
|
||||
isObjectExpression,
|
||||
isObjectType,
|
||||
} from "../utilities/node-types.js";
|
||||
import { shouldPrintComma } from "../utilities/should-print-comma.js";
|
||||
import { isTestCall } from "../utilities/test-libraries.js";
|
||||
import { printDanglingCommentsInList } from "./miscellaneous.js";
|
||||
import {
|
||||
printDanglingCommentsInList,
|
||||
printTrailingComma,
|
||||
} from "./miscellaneous.js";
|
||||
|
||||
/** @import AstPath from "../../common/ast-path.js" */
|
||||
|
||||
@@ -184,11 +185,7 @@ function printFunctionParameters(
|
||||
typeParametersDoc,
|
||||
"(",
|
||||
indent([softline, ...printed]),
|
||||
ifBreak(
|
||||
!hasRestParameter(functionNode) && shouldPrintComma(options, "all")
|
||||
? ","
|
||||
: "",
|
||||
),
|
||||
!hasRestParameter(functionNode) ? printTrailingComma(options, "all") : "",
|
||||
softline,
|
||||
")",
|
||||
];
|
||||
|
||||
@@ -1,12 +1,6 @@
|
||||
import {
|
||||
group,
|
||||
ifBreak,
|
||||
indent,
|
||||
join,
|
||||
softline,
|
||||
} from "../../document/index.js";
|
||||
import { shouldPrintComma } from "../utilities/should-print-comma.js";
|
||||
import { group, indent, join, softline } from "../../document/index.js";
|
||||
import { printClassMemberSemicolon } from "./class.js";
|
||||
import { printTrailingComma } from "./miscellaneous.js";
|
||||
import { printTypeAnnotationProperty } from "./type-annotation.js";
|
||||
|
||||
function printIndexSignature(path, options, print) {
|
||||
@@ -16,9 +10,7 @@ function printIndexSignature(path, options, print) {
|
||||
// aren't, this is more like a computed property name than an array.
|
||||
// So we leave off the trailing comma when there's just one parameter.
|
||||
const trailingComma =
|
||||
node.parameters.length > 1
|
||||
? ifBreak(shouldPrintComma(options) ? "," : "")
|
||||
: "";
|
||||
node.parameters.length > 1 ? printTrailingComma(options) : "";
|
||||
|
||||
const parametersGroup = group([
|
||||
indent([softline, join([", ", softline], path.map(print, "parameters"))]),
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import {
|
||||
group,
|
||||
hardline,
|
||||
ifBreak,
|
||||
indent,
|
||||
line,
|
||||
softline,
|
||||
@@ -12,6 +13,7 @@ import {
|
||||
isCallExpression,
|
||||
isMemberExpression,
|
||||
} from "../utilities/node-types.js";
|
||||
import { shouldPrintTrailingComma } from "../utilities/should-print-trailing-comma.js";
|
||||
|
||||
/**
|
||||
@import AstPath from "../../common/ast-path.js"
|
||||
@@ -192,6 +194,14 @@ function printDanglingCommentsInList(path, options, filter) {
|
||||
: "";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {("es5" | "all")} [level]
|
||||
* @returns {Doc}
|
||||
*/
|
||||
function printTrailingComma(options, level = "es5") {
|
||||
return shouldPrintTrailingComma(options, level) ? ifBreak(",") : "";
|
||||
}
|
||||
|
||||
export {
|
||||
adjustClause,
|
||||
printAbstractToken,
|
||||
@@ -201,6 +211,7 @@ export {
|
||||
printIfOrWhileCondition as printDoWhileStatementCondition,
|
||||
printIfOrWhileCondition as printIfStatementCondition,
|
||||
printOptionalToken,
|
||||
printTrailingComma,
|
||||
printTypeScriptAccessibilityToken,
|
||||
printIfOrWhileCondition as printWhileStatementCondition,
|
||||
};
|
||||
@@ -1,7 +1,6 @@
|
||||
import {
|
||||
group,
|
||||
hardline,
|
||||
ifBreak,
|
||||
indent,
|
||||
join,
|
||||
line,
|
||||
@@ -18,9 +17,8 @@ import getTextWithoutComments from "../utilities/get-text-without-comments.js";
|
||||
import { isShorthandSpecifier } from "../utilities/is-shorthand-specifier.js";
|
||||
import { needsHardlineAfterDanglingComment } from "../utilities/needs-hardline-after-dangling-comment.js";
|
||||
import { isStringLiteral } from "../utilities/node-types.js";
|
||||
import { shouldPrintComma } from "../utilities/should-print-comma.js";
|
||||
import { printDecoratorsBeforeExport } from "./decorators.js";
|
||||
import { printDeclareToken } from "./miscellaneous.js";
|
||||
import { printDeclareToken, printTrailingComma } from "./miscellaneous.js";
|
||||
import { printObject } from "./object.js";
|
||||
|
||||
/**
|
||||
@@ -211,7 +209,7 @@ function printModuleSpecifiers(path, options, print) {
|
||||
options.bracketSpacing ? line : softline,
|
||||
join([",", line], groupedSpecifiers),
|
||||
]),
|
||||
ifBreak(shouldPrintComma(options) ? "," : ""),
|
||||
printTrailingComma(options),
|
||||
options.bracketSpacing ? line : softline,
|
||||
"}",
|
||||
]),
|
||||
|
||||
@@ -2,7 +2,6 @@ import * as assert from "#universal/assert";
|
||||
import {
|
||||
group,
|
||||
hardline,
|
||||
ifBreak,
|
||||
indent,
|
||||
line,
|
||||
softline,
|
||||
@@ -21,11 +20,11 @@ import { createTypeCheckFunction } from "../utilities/create-type-check-function
|
||||
import getTextWithoutComments from "../utilities/get-text-without-comments.js";
|
||||
import { isNextLineEmpty } from "../utilities/is-next-line-empty.js";
|
||||
import { isObjectType } from "../utilities/node-types.js";
|
||||
import { shouldPrintComma } from "../utilities/should-print-comma.js";
|
||||
import { shouldHugTheOnlyParameter } from "./function-parameters.js";
|
||||
import {
|
||||
printDanglingCommentsInList,
|
||||
printOptionalToken,
|
||||
printTrailingComma,
|
||||
} from "./miscellaneous.js";
|
||||
import { printTypeAnnotationProperty } from "./type-annotation.js";
|
||||
|
||||
@@ -149,7 +148,7 @@ function printObject(path, options, print) {
|
||||
content = [
|
||||
"{",
|
||||
indent([spacing, ...parts]),
|
||||
ifBreak(canHaveTrailingSeparator && shouldPrintComma(options) ? "," : ""),
|
||||
canHaveTrailingSeparator ? printTrailingComma(options) : "",
|
||||
spacing,
|
||||
"}",
|
||||
printOptionalToken(path),
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import {
|
||||
group,
|
||||
hardline,
|
||||
ifBreak,
|
||||
indent,
|
||||
indentIfBreak,
|
||||
join,
|
||||
@@ -15,9 +14,9 @@ import { locEnd } from "../loc.js";
|
||||
import { CommentCheckFlags, hasComment } from "../utilities/comments.js";
|
||||
import { getFunctionParameters } from "../utilities/function-parameters.js";
|
||||
import { isObjectType } from "../utilities/node-types.js";
|
||||
import { shouldPrintComma } from "../utilities/should-print-comma.js";
|
||||
import { isTestCall } from "../utilities/test-libraries.js";
|
||||
import { isArrowFunctionVariableDeclarator } from "./assignment.js";
|
||||
import { printTrailingComma } from "./miscellaneous.js";
|
||||
import {
|
||||
printTypeAnnotationProperty,
|
||||
shouldHugType,
|
||||
@@ -108,9 +107,7 @@ function printTypeParameters(path, options, print, paramsKey) {
|
||||
? ""
|
||||
: shouldForceTrailingComma(path, options, paramsKey)
|
||||
? ","
|
||||
: shouldPrintComma(options)
|
||||
? ifBreak(",")
|
||||
: "";
|
||||
: printTrailingComma(options);
|
||||
|
||||
return group([
|
||||
"<",
|
||||
|
||||
+2
-2
@@ -3,11 +3,11 @@
|
||||
* @param {("es5" | "all")} [level]
|
||||
* @returns {boolean}
|
||||
*/
|
||||
function shouldPrintComma(options, level = "es5") {
|
||||
function shouldPrintTrailingComma(options, level = "es5") {
|
||||
return (
|
||||
(options.trailingComma === "es5" && level === "es5") ||
|
||||
(options.trailingComma === "all" && (level === "all" || level === "es5"))
|
||||
);
|
||||
}
|
||||
|
||||
export { shouldPrintComma };
|
||||
export { shouldPrintTrailingComma };
|
||||
Reference in new issue
Block a user