2022-04-29 14:04:17 +08:00
|
|
|
import {
|
|
|
|
|
join,
|
|
|
|
|
hardline,
|
|
|
|
|
line,
|
|
|
|
|
softline,
|
|
|
|
|
group,
|
|
|
|
|
indent,
|
|
|
|
|
ifBreak,
|
|
|
|
|
} from "../document/builders.js";
|
2022-02-21 10:25:39 +08:00
|
|
|
import { isNextLineEmpty, isNonEmptyArray } from "../common/util.js";
|
2022-08-17 09:47:08 +08:00
|
|
|
import createGetVisitorKeys from "../utils/create-get-visitor-keys.js";
|
2022-02-21 10:25:39 +08:00
|
|
|
import { insertPragma } from "./pragma.js";
|
|
|
|
|
import { locStart, locEnd } from "./loc.js";
|
2022-08-17 09:47:08 +08:00
|
|
|
import visitorKeys from "./visitor-keys.evaluate.js";
|
2017-06-05 15:34:08 -04:00
|
|
|
|
2022-08-02 18:11:34 +08:00
|
|
|
function genericPrint(path, options, print) {
|
2021-03-11 04:14:08 +08:00
|
|
|
const node = path.getValue();
|
|
|
|
|
if (!node) {
|
2017-06-05 15:34:08 -04:00
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-11 04:14:08 +08:00
|
|
|
if (typeof node === "string") {
|
|
|
|
|
return node;
|
2017-06-05 15:34:08 -04:00
|
|
|
}
|
|
|
|
|
|
2021-03-11 04:14:08 +08:00
|
|
|
switch (node.kind) {
|
2017-06-05 15:34:08 -04:00
|
|
|
case "Document": {
|
2018-05-20 23:14:49 +05:30
|
|
|
const parts = [];
|
2022-08-02 18:11:34 +08:00
|
|
|
path.each((pathChild, index, definitions) => {
|
|
|
|
|
parts.push(print());
|
2020-12-17 15:52:51 +08:00
|
|
|
if (index !== definitions.length - 1) {
|
2018-05-20 23:14:49 +05:30
|
|
|
parts.push(hardline);
|
2018-06-01 00:38:39 +08:00
|
|
|
if (
|
2020-11-10 09:06:08 +08:00
|
|
|
isNextLineEmpty(options.originalText, pathChild.getValue(), locEnd)
|
2018-06-01 00:38:39 +08:00
|
|
|
) {
|
|
|
|
|
parts.push(hardline);
|
|
|
|
|
}
|
2018-05-20 23:14:49 +05:30
|
|
|
}
|
|
|
|
|
}, "definitions");
|
2021-01-11 13:46:05 +08:00
|
|
|
return [...parts, hardline];
|
2017-06-05 15:34:08 -04:00
|
|
|
}
|
|
|
|
|
case "OperationDefinition": {
|
2021-03-11 04:14:08 +08:00
|
|
|
const hasOperation = options.originalText[locStart(node)] !== "{";
|
|
|
|
|
const hasName = Boolean(node.name);
|
2021-01-11 13:46:05 +08:00
|
|
|
return [
|
2021-03-11 04:14:08 +08:00
|
|
|
hasOperation ? node.operation : "",
|
2022-08-02 18:11:34 +08:00
|
|
|
hasOperation && hasName ? [" ", print("name")] : "",
|
2021-04-12 10:50:39 +02:00
|
|
|
hasOperation && !hasName && isNonEmptyArray(node.variableDefinitions)
|
|
|
|
|
? " "
|
|
|
|
|
: "",
|
2021-03-11 04:14:08 +08:00
|
|
|
isNonEmptyArray(node.variableDefinitions)
|
2021-01-11 13:46:05 +08:00
|
|
|
? group([
|
|
|
|
|
"(",
|
|
|
|
|
indent([
|
2017-06-06 08:26:04 -07:00
|
|
|
softline,
|
2021-01-11 13:46:05 +08:00
|
|
|
join(
|
|
|
|
|
[ifBreak("", ", "), softline],
|
2022-08-02 18:11:34 +08:00
|
|
|
path.map(print, "variableDefinitions")
|
2021-01-11 13:46:05 +08:00
|
|
|
),
|
|
|
|
|
]),
|
|
|
|
|
softline,
|
|
|
|
|
")",
|
|
|
|
|
])
|
2017-06-06 17:42:47 -07:00
|
|
|
: "",
|
2022-08-02 18:11:34 +08:00
|
|
|
printDirectives(path, print, node),
|
2021-03-11 04:14:08 +08:00
|
|
|
node.selectionSet ? (!hasOperation && !hasName ? "" : " ") : "",
|
2022-08-02 18:11:34 +08:00
|
|
|
print("selectionSet"),
|
2021-01-11 13:46:05 +08:00
|
|
|
];
|
2017-06-05 15:34:08 -04:00
|
|
|
}
|
2017-06-06 12:35:40 -07:00
|
|
|
case "FragmentDefinition": {
|
2021-01-11 13:46:05 +08:00
|
|
|
return [
|
2017-06-06 12:35:40 -07:00
|
|
|
"fragment ",
|
2022-08-02 18:11:34 +08:00
|
|
|
print("name"),
|
2021-03-11 04:14:08 +08:00
|
|
|
isNonEmptyArray(node.variableDefinitions)
|
2021-01-11 13:46:05 +08:00
|
|
|
? group([
|
|
|
|
|
"(",
|
|
|
|
|
indent([
|
2019-04-04 13:44:52 +01:00
|
|
|
softline,
|
2021-01-11 13:46:05 +08:00
|
|
|
join(
|
|
|
|
|
[ifBreak("", ", "), softline],
|
2022-08-02 18:11:34 +08:00
|
|
|
path.map(print, "variableDefinitions")
|
2021-01-11 13:46:05 +08:00
|
|
|
),
|
|
|
|
|
]),
|
|
|
|
|
softline,
|
|
|
|
|
")",
|
|
|
|
|
])
|
2019-04-04 13:44:52 +01:00
|
|
|
: "",
|
2017-06-06 12:35:40 -07:00
|
|
|
" on ",
|
2022-08-02 18:11:34 +08:00
|
|
|
print("typeCondition"),
|
|
|
|
|
printDirectives(path, print, node),
|
2017-06-06 12:35:40 -07:00
|
|
|
" ",
|
2022-08-02 18:11:34 +08:00
|
|
|
print("selectionSet"),
|
2021-01-11 13:46:05 +08:00
|
|
|
];
|
2017-06-06 12:35:40 -07:00
|
|
|
}
|
2017-06-05 15:34:08 -04:00
|
|
|
case "SelectionSet": {
|
2021-01-11 13:46:05 +08:00
|
|
|
return [
|
2017-06-05 15:34:08 -04:00
|
|
|
"{",
|
2021-01-11 13:46:05 +08:00
|
|
|
indent([
|
|
|
|
|
hardline,
|
2022-08-02 18:11:34 +08:00
|
|
|
join(hardline, printSequence(path, options, print, "selections")),
|
2021-01-11 13:46:05 +08:00
|
|
|
]),
|
2017-06-05 15:34:08 -04:00
|
|
|
hardline,
|
|
|
|
|
"}",
|
2021-01-11 13:46:05 +08:00
|
|
|
];
|
2017-06-05 15:34:08 -04:00
|
|
|
}
|
|
|
|
|
case "Field": {
|
2021-01-11 13:46:05 +08:00
|
|
|
return group([
|
2022-08-02 18:11:34 +08:00
|
|
|
node.alias ? [print("alias"), ": "] : "",
|
|
|
|
|
print("name"),
|
2021-03-11 04:14:08 +08:00
|
|
|
node.arguments.length > 0
|
2021-01-11 13:46:05 +08:00
|
|
|
? group([
|
|
|
|
|
"(",
|
|
|
|
|
indent([
|
|
|
|
|
softline,
|
|
|
|
|
join(
|
|
|
|
|
[ifBreak("", ", "), softline],
|
2022-08-02 18:11:34 +08:00
|
|
|
printSequence(path, options, print, "arguments")
|
2021-01-11 13:46:05 +08:00
|
|
|
),
|
|
|
|
|
]),
|
|
|
|
|
softline,
|
|
|
|
|
")",
|
|
|
|
|
])
|
|
|
|
|
: "",
|
2022-08-02 18:11:34 +08:00
|
|
|
printDirectives(path, print, node),
|
2021-03-11 04:14:08 +08:00
|
|
|
node.selectionSet ? " " : "",
|
2022-08-02 18:11:34 +08:00
|
|
|
print("selectionSet"),
|
2021-01-11 13:46:05 +08:00
|
|
|
]);
|
2017-06-05 15:34:08 -04:00
|
|
|
}
|
|
|
|
|
case "Name": {
|
2021-03-11 04:14:08 +08:00
|
|
|
return node.value;
|
2017-06-05 15:34:08 -04:00
|
|
|
}
|
|
|
|
|
case "StringValue": {
|
2021-03-11 04:14:08 +08:00
|
|
|
if (node.block) {
|
2022-04-22 14:38:44 +02:00
|
|
|
const lines = node.value.replace(/"""/g, "\\$&").split("\n");
|
|
|
|
|
if (lines.length === 1) {
|
|
|
|
|
lines[0] = lines[0].trim();
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-16 07:33:09 +02:00
|
|
|
if (lines.every((line) => line === "")) {
|
|
|
|
|
lines.length = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return join(hardline, ['"""', ...lines, '"""']);
|
2017-12-31 16:45:06 +01:00
|
|
|
}
|
2021-01-11 13:46:05 +08:00
|
|
|
return [
|
2018-07-05 00:14:09 +08:00
|
|
|
'"',
|
2021-03-11 04:14:08 +08:00
|
|
|
node.value.replace(/["\\]/g, "\\$&").replace(/\n/g, "\\n"),
|
2018-07-05 00:14:09 +08:00
|
|
|
'"',
|
2021-01-11 13:46:05 +08:00
|
|
|
];
|
2017-06-05 15:34:08 -04:00
|
|
|
}
|
2017-06-06 08:26:04 -07:00
|
|
|
case "IntValue":
|
|
|
|
|
case "FloatValue":
|
|
|
|
|
case "EnumValue": {
|
2021-03-11 04:14:08 +08:00
|
|
|
return node.value;
|
2017-06-05 22:25:12 -07:00
|
|
|
}
|
|
|
|
|
case "BooleanValue": {
|
2021-03-11 04:14:08 +08:00
|
|
|
return node.value ? "true" : "false";
|
2017-06-05 22:25:12 -07:00
|
|
|
}
|
2017-06-06 18:06:15 -07:00
|
|
|
case "NullValue": {
|
|
|
|
|
return "null";
|
|
|
|
|
}
|
2017-06-05 22:25:12 -07:00
|
|
|
case "Variable": {
|
2022-08-02 18:11:34 +08:00
|
|
|
return ["$", print("name")];
|
2017-06-05 23:45:15 -07:00
|
|
|
}
|
|
|
|
|
case "ListValue": {
|
2021-01-11 13:46:05 +08:00
|
|
|
return group([
|
|
|
|
|
"[",
|
|
|
|
|
indent([
|
2017-06-05 23:45:15 -07:00
|
|
|
softline,
|
2022-08-02 18:11:34 +08:00
|
|
|
join([ifBreak("", ", "), softline], path.map(print, "values")),
|
2021-01-11 13:46:05 +08:00
|
|
|
]),
|
|
|
|
|
softline,
|
|
|
|
|
"]",
|
|
|
|
|
]);
|
2017-06-05 22:25:12 -07:00
|
|
|
}
|
2017-06-06 14:28:15 -07:00
|
|
|
case "ObjectValue": {
|
2021-01-11 13:46:05 +08:00
|
|
|
return group([
|
|
|
|
|
"{",
|
2021-03-11 04:14:08 +08:00
|
|
|
options.bracketSpacing && node.fields.length > 0 ? " " : "",
|
2021-01-11 13:46:05 +08:00
|
|
|
indent([
|
2017-06-06 14:28:15 -07:00
|
|
|
softline,
|
2022-08-02 18:11:34 +08:00
|
|
|
join([ifBreak("", ", "), softline], path.map(print, "fields")),
|
2021-01-11 13:46:05 +08:00
|
|
|
]),
|
|
|
|
|
softline,
|
2021-03-11 04:14:08 +08:00
|
|
|
ifBreak(
|
|
|
|
|
"",
|
|
|
|
|
options.bracketSpacing && node.fields.length > 0 ? " " : ""
|
|
|
|
|
),
|
2021-01-11 13:46:05 +08:00
|
|
|
"}",
|
|
|
|
|
]);
|
2017-06-06 14:28:15 -07:00
|
|
|
}
|
|
|
|
|
case "ObjectField":
|
2017-06-05 15:34:08 -04:00
|
|
|
case "Argument": {
|
2022-08-02 18:11:34 +08:00
|
|
|
return [print("name"), ": ", print("value")];
|
2017-06-05 15:34:08 -04:00
|
|
|
}
|
|
|
|
|
|
2017-06-06 17:42:47 -07:00
|
|
|
case "Directive": {
|
2021-01-11 13:46:05 +08:00
|
|
|
return [
|
2017-06-06 17:42:47 -07:00
|
|
|
"@",
|
2022-08-02 18:11:34 +08:00
|
|
|
print("name"),
|
2021-03-11 04:14:08 +08:00
|
|
|
node.arguments.length > 0
|
2021-01-11 13:46:05 +08:00
|
|
|
? group([
|
|
|
|
|
"(",
|
|
|
|
|
indent([
|
2017-06-06 17:42:47 -07:00
|
|
|
softline,
|
2021-01-11 13:46:05 +08:00
|
|
|
join(
|
|
|
|
|
[ifBreak("", ", "), softline],
|
2022-08-02 18:11:34 +08:00
|
|
|
printSequence(path, options, print, "arguments")
|
2021-01-11 13:46:05 +08:00
|
|
|
),
|
|
|
|
|
]),
|
|
|
|
|
softline,
|
|
|
|
|
")",
|
|
|
|
|
])
|
2017-06-06 17:42:47 -07:00
|
|
|
: "",
|
2021-01-11 13:46:05 +08:00
|
|
|
];
|
2017-06-06 17:42:47 -07:00
|
|
|
}
|
|
|
|
|
|
2017-06-06 08:26:04 -07:00
|
|
|
case "NamedType": {
|
2021-03-22 22:57:03 +08:00
|
|
|
return print("name");
|
2017-06-06 08:26:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "VariableDefinition": {
|
2021-01-11 13:46:05 +08:00
|
|
|
return [
|
2022-08-02 18:11:34 +08:00
|
|
|
print("variable"),
|
2017-06-06 08:26:04 -07:00
|
|
|
": ",
|
2022-08-02 18:11:34 +08:00
|
|
|
print("type"),
|
|
|
|
|
node.defaultValue ? [" = ", print("defaultValue")] : "",
|
|
|
|
|
printDirectives(path, print, node),
|
2021-01-11 13:46:05 +08:00
|
|
|
];
|
2017-06-06 08:26:04 -07:00
|
|
|
}
|
|
|
|
|
|
2017-12-31 16:45:06 +01:00
|
|
|
case "ObjectTypeExtension":
|
2017-06-27 18:22:04 -07:00
|
|
|
case "ObjectTypeDefinition": {
|
2021-01-11 13:46:05 +08:00
|
|
|
return [
|
2022-08-02 18:11:34 +08:00
|
|
|
print("description"),
|
2021-03-11 04:14:08 +08:00
|
|
|
node.description ? hardline : "",
|
|
|
|
|
node.kind === "ObjectTypeExtension" ? "extend " : "",
|
2017-06-27 18:22:04 -07:00
|
|
|
"type ",
|
2022-08-02 18:11:34 +08:00
|
|
|
print("name"),
|
2021-03-11 04:14:08 +08:00
|
|
|
node.interfaces.length > 0
|
2022-08-02 18:11:34 +08:00
|
|
|
? [" implements ", ...printInterfaces(path, options, print)]
|
2017-06-27 18:51:56 -07:00
|
|
|
: "",
|
2022-08-02 18:11:34 +08:00
|
|
|
printDirectives(path, print, node),
|
2021-03-11 04:14:08 +08:00
|
|
|
node.fields.length > 0
|
2021-01-11 13:46:05 +08:00
|
|
|
? [
|
2017-12-31 16:45:06 +01:00
|
|
|
" {",
|
2021-01-11 13:46:05 +08:00
|
|
|
indent([
|
|
|
|
|
hardline,
|
2022-08-02 18:11:34 +08:00
|
|
|
join(hardline, printSequence(path, options, print, "fields")),
|
2021-01-11 13:46:05 +08:00
|
|
|
]),
|
2017-12-31 16:45:06 +01:00
|
|
|
hardline,
|
|
|
|
|
"}",
|
2021-01-11 13:46:05 +08:00
|
|
|
]
|
2017-12-31 16:45:06 +01:00
|
|
|
: "",
|
2021-01-11 13:46:05 +08:00
|
|
|
];
|
2017-06-27 18:22:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "FieldDefinition": {
|
2021-01-11 13:46:05 +08:00
|
|
|
return [
|
2022-08-02 18:11:34 +08:00
|
|
|
print("description"),
|
2021-03-11 04:14:08 +08:00
|
|
|
node.description ? hardline : "",
|
2022-08-02 18:11:34 +08:00
|
|
|
print("name"),
|
2021-03-11 04:14:08 +08:00
|
|
|
node.arguments.length > 0
|
2021-01-11 13:46:05 +08:00
|
|
|
? group([
|
|
|
|
|
"(",
|
|
|
|
|
indent([
|
2017-06-27 19:49:59 -07:00
|
|
|
softline,
|
2021-01-11 13:46:05 +08:00
|
|
|
join(
|
|
|
|
|
[ifBreak("", ", "), softline],
|
2022-08-02 18:11:34 +08:00
|
|
|
printSequence(path, options, print, "arguments")
|
2021-01-11 13:46:05 +08:00
|
|
|
),
|
|
|
|
|
]),
|
|
|
|
|
softline,
|
|
|
|
|
")",
|
|
|
|
|
])
|
2017-06-27 19:49:59 -07:00
|
|
|
: "",
|
|
|
|
|
": ",
|
2022-08-02 18:11:34 +08:00
|
|
|
print("type"),
|
|
|
|
|
printDirectives(path, print, node),
|
2021-01-11 13:46:05 +08:00
|
|
|
];
|
2017-06-27 18:22:04 -07:00
|
|
|
}
|
|
|
|
|
|
2017-06-27 19:10:07 -07:00
|
|
|
case "DirectiveDefinition": {
|
2021-01-11 13:46:05 +08:00
|
|
|
return [
|
2022-08-02 18:11:34 +08:00
|
|
|
print("description"),
|
2021-03-11 04:14:08 +08:00
|
|
|
node.description ? hardline : "",
|
2017-06-27 19:10:07 -07:00
|
|
|
"directive ",
|
|
|
|
|
"@",
|
2022-08-02 18:11:34 +08:00
|
|
|
print("name"),
|
2021-03-11 04:14:08 +08:00
|
|
|
node.arguments.length > 0
|
2021-01-11 13:46:05 +08:00
|
|
|
? group([
|
|
|
|
|
"(",
|
|
|
|
|
indent([
|
2017-06-27 19:10:07 -07:00
|
|
|
softline,
|
2021-01-11 13:46:05 +08:00
|
|
|
join(
|
|
|
|
|
[ifBreak("", ", "), softline],
|
2022-08-02 18:11:34 +08:00
|
|
|
printSequence(path, options, print, "arguments")
|
2021-01-11 13:46:05 +08:00
|
|
|
),
|
|
|
|
|
]),
|
|
|
|
|
softline,
|
|
|
|
|
")",
|
|
|
|
|
])
|
2017-06-27 19:10:07 -07:00
|
|
|
: "",
|
2021-03-11 04:14:08 +08:00
|
|
|
node.repeatable ? " repeatable" : "",
|
2021-01-11 10:19:13 +08:00
|
|
|
" on ",
|
2022-08-02 18:11:34 +08:00
|
|
|
join(" | ", path.map(print, "locations")),
|
2021-01-11 13:46:05 +08:00
|
|
|
];
|
2017-06-27 19:10:07 -07:00
|
|
|
}
|
|
|
|
|
|
2017-12-31 16:45:06 +01:00
|
|
|
case "EnumTypeExtension":
|
2017-06-27 19:20:53 -07:00
|
|
|
case "EnumTypeDefinition": {
|
2021-01-11 13:46:05 +08:00
|
|
|
return [
|
2022-08-02 18:11:34 +08:00
|
|
|
print("description"),
|
2021-03-11 04:14:08 +08:00
|
|
|
node.description ? hardline : "",
|
|
|
|
|
node.kind === "EnumTypeExtension" ? "extend " : "",
|
2017-06-27 19:20:53 -07:00
|
|
|
"enum ",
|
2022-08-02 18:11:34 +08:00
|
|
|
print("name"),
|
|
|
|
|
printDirectives(path, print, node),
|
2017-12-31 16:45:06 +01:00
|
|
|
|
2021-03-11 04:14:08 +08:00
|
|
|
node.values.length > 0
|
2021-01-11 13:46:05 +08:00
|
|
|
? [
|
2017-12-31 16:45:06 +01:00
|
|
|
" {",
|
2021-01-11 13:46:05 +08:00
|
|
|
indent([
|
|
|
|
|
hardline,
|
2022-08-02 18:11:34 +08:00
|
|
|
join(hardline, printSequence(path, options, print, "values")),
|
2021-01-11 13:46:05 +08:00
|
|
|
]),
|
2017-12-31 16:45:06 +01:00
|
|
|
hardline,
|
|
|
|
|
"}",
|
2021-01-11 13:46:05 +08:00
|
|
|
]
|
2017-12-31 16:45:06 +01:00
|
|
|
: "",
|
2021-01-11 13:46:05 +08:00
|
|
|
];
|
2017-06-27 19:20:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "EnumValueDefinition": {
|
2021-01-11 13:46:05 +08:00
|
|
|
return [
|
2022-08-02 18:11:34 +08:00
|
|
|
print("description"),
|
2021-03-11 04:14:08 +08:00
|
|
|
node.description ? hardline : "",
|
2022-08-02 18:11:34 +08:00
|
|
|
print("name"),
|
|
|
|
|
printDirectives(path, print, node),
|
2021-01-11 13:46:05 +08:00
|
|
|
];
|
2017-06-27 19:20:53 -07:00
|
|
|
}
|
|
|
|
|
|
2017-06-27 19:10:07 -07:00
|
|
|
case "InputValueDefinition": {
|
2021-01-11 13:46:05 +08:00
|
|
|
return [
|
2022-08-02 18:11:34 +08:00
|
|
|
print("description"),
|
2021-03-11 04:14:08 +08:00
|
|
|
node.description ? (node.description.block ? hardline : line) : "",
|
2022-08-02 18:11:34 +08:00
|
|
|
print("name"),
|
2017-06-27 19:10:07 -07:00
|
|
|
": ",
|
2022-08-02 18:11:34 +08:00
|
|
|
print("type"),
|
|
|
|
|
node.defaultValue ? [" = ", print("defaultValue")] : "",
|
|
|
|
|
printDirectives(path, print, node),
|
2021-01-11 13:46:05 +08:00
|
|
|
];
|
2017-06-27 19:10:07 -07:00
|
|
|
}
|
|
|
|
|
|
2017-12-31 16:45:06 +01:00
|
|
|
case "InputObjectTypeExtension":
|
2017-06-27 19:27:02 -07:00
|
|
|
case "InputObjectTypeDefinition": {
|
2021-01-11 13:46:05 +08:00
|
|
|
return [
|
2022-08-02 18:11:34 +08:00
|
|
|
print("description"),
|
2021-03-11 04:14:08 +08:00
|
|
|
node.description ? hardline : "",
|
|
|
|
|
node.kind === "InputObjectTypeExtension" ? "extend " : "",
|
2017-06-27 19:27:02 -07:00
|
|
|
"input ",
|
2022-08-02 18:11:34 +08:00
|
|
|
print("name"),
|
|
|
|
|
printDirectives(path, print, node),
|
2021-03-11 04:14:08 +08:00
|
|
|
node.fields.length > 0
|
2021-01-11 13:46:05 +08:00
|
|
|
? [
|
2017-12-31 16:45:06 +01:00
|
|
|
" {",
|
2021-01-11 13:46:05 +08:00
|
|
|
indent([
|
2017-06-27 20:15:55 -07:00
|
|
|
hardline,
|
2022-08-02 18:11:34 +08:00
|
|
|
join(hardline, printSequence(path, options, print, "fields")),
|
2021-01-11 13:46:05 +08:00
|
|
|
]),
|
|
|
|
|
hardline,
|
|
|
|
|
"}",
|
|
|
|
|
]
|
|
|
|
|
: "",
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-26 08:59:04 -07:00
|
|
|
case "SchemaExtension": {
|
|
|
|
|
return [
|
|
|
|
|
"extend schema",
|
2022-08-02 18:11:34 +08:00
|
|
|
printDirectives(path, print, node),
|
2022-03-26 08:59:04 -07:00
|
|
|
...(node.operationTypes.length > 0
|
|
|
|
|
? [
|
|
|
|
|
" {",
|
|
|
|
|
indent([
|
|
|
|
|
hardline,
|
|
|
|
|
join(
|
|
|
|
|
hardline,
|
2022-08-02 18:11:34 +08:00
|
|
|
printSequence(path, options, print, "operationTypes")
|
2022-03-26 08:59:04 -07:00
|
|
|
),
|
|
|
|
|
]),
|
|
|
|
|
hardline,
|
|
|
|
|
"}",
|
|
|
|
|
]
|
|
|
|
|
: []),
|
|
|
|
|
];
|
|
|
|
|
}
|
2021-01-11 13:46:05 +08:00
|
|
|
case "SchemaDefinition": {
|
|
|
|
|
return [
|
2022-08-02 18:11:34 +08:00
|
|
|
print("description"),
|
2021-12-02 21:42:09 -08:00
|
|
|
node.description ? hardline : "",
|
2021-01-11 13:46:05 +08:00
|
|
|
"schema",
|
2022-08-02 18:11:34 +08:00
|
|
|
printDirectives(path, print, node),
|
2021-01-11 13:46:05 +08:00
|
|
|
" {",
|
2021-03-11 04:14:08 +08:00
|
|
|
node.operationTypes.length > 0
|
2021-01-11 13:46:05 +08:00
|
|
|
? indent([
|
|
|
|
|
hardline,
|
|
|
|
|
join(
|
|
|
|
|
hardline,
|
2022-08-02 18:11:34 +08:00
|
|
|
printSequence(path, options, print, "operationTypes")
|
2021-01-11 13:46:05 +08:00
|
|
|
),
|
|
|
|
|
])
|
2017-06-27 20:15:55 -07:00
|
|
|
: "",
|
|
|
|
|
hardline,
|
|
|
|
|
"}",
|
2021-01-11 13:46:05 +08:00
|
|
|
];
|
2017-06-27 20:15:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "OperationTypeDefinition": {
|
2022-08-02 18:11:34 +08:00
|
|
|
return [print("operation"), ": ", print("type")];
|
2017-06-27 20:15:55 -07:00
|
|
|
}
|
|
|
|
|
|
2017-12-31 16:45:06 +01:00
|
|
|
case "InterfaceTypeExtension":
|
2017-06-27 19:30:58 -07:00
|
|
|
case "InterfaceTypeDefinition": {
|
2021-01-11 13:46:05 +08:00
|
|
|
return [
|
2022-08-02 18:11:34 +08:00
|
|
|
print("description"),
|
2021-03-11 04:14:08 +08:00
|
|
|
node.description ? hardline : "",
|
|
|
|
|
node.kind === "InterfaceTypeExtension" ? "extend " : "",
|
2017-06-27 19:30:58 -07:00
|
|
|
"interface ",
|
2022-08-02 18:11:34 +08:00
|
|
|
print("name"),
|
2021-03-11 04:14:08 +08:00
|
|
|
node.interfaces.length > 0
|
2022-08-02 18:11:34 +08:00
|
|
|
? [" implements ", ...printInterfaces(path, options, print)]
|
2020-04-10 20:27:25 +08:00
|
|
|
: "",
|
2022-08-02 18:11:34 +08:00
|
|
|
printDirectives(path, print, node),
|
2021-03-11 04:14:08 +08:00
|
|
|
node.fields.length > 0
|
2021-01-11 13:46:05 +08:00
|
|
|
? [
|
2017-12-31 16:45:06 +01:00
|
|
|
" {",
|
2021-01-11 13:46:05 +08:00
|
|
|
indent([
|
|
|
|
|
hardline,
|
2022-08-02 18:11:34 +08:00
|
|
|
join(hardline, printSequence(path, options, print, "fields")),
|
2021-01-11 13:46:05 +08:00
|
|
|
]),
|
2017-12-31 16:45:06 +01:00
|
|
|
hardline,
|
|
|
|
|
"}",
|
2021-01-11 13:46:05 +08:00
|
|
|
]
|
2017-12-31 16:45:06 +01:00
|
|
|
: "",
|
2021-01-11 13:46:05 +08:00
|
|
|
];
|
2017-06-27 19:30:58 -07:00
|
|
|
}
|
|
|
|
|
|
2017-06-06 12:35:40 -07:00
|
|
|
case "FragmentSpread": {
|
2022-08-02 18:11:34 +08:00
|
|
|
return ["...", print("name"), printDirectives(path, print, node)];
|
2017-06-06 12:35:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "InlineFragment": {
|
2021-01-11 13:46:05 +08:00
|
|
|
return [
|
2017-06-06 12:35:40 -07:00
|
|
|
"...",
|
2022-08-02 18:11:34 +08:00
|
|
|
node.typeCondition ? [" on ", print("typeCondition")] : "",
|
|
|
|
|
printDirectives(path, print, node),
|
2017-06-06 12:35:40 -07:00
|
|
|
" ",
|
2022-08-02 18:11:34 +08:00
|
|
|
print("selectionSet"),
|
2021-01-11 13:46:05 +08:00
|
|
|
];
|
2017-06-06 12:35:40 -07:00
|
|
|
}
|
|
|
|
|
|
2017-12-31 16:45:06 +01:00
|
|
|
case "UnionTypeExtension":
|
2017-06-07 19:33:46 +02:00
|
|
|
case "UnionTypeDefinition": {
|
2021-01-11 13:46:05 +08:00
|
|
|
return group([
|
2022-08-02 18:11:34 +08:00
|
|
|
print("description"),
|
2021-03-11 04:14:08 +08:00
|
|
|
node.description ? hardline : "",
|
2021-01-11 13:46:05 +08:00
|
|
|
group([
|
2021-03-11 04:14:08 +08:00
|
|
|
node.kind === "UnionTypeExtension" ? "extend " : "",
|
2021-01-11 13:46:05 +08:00
|
|
|
"union ",
|
2022-08-02 18:11:34 +08:00
|
|
|
print("name"),
|
|
|
|
|
printDirectives(path, print, node),
|
2021-03-11 04:14:08 +08:00
|
|
|
node.types.length > 0
|
2021-01-11 13:46:05 +08:00
|
|
|
? [
|
|
|
|
|
" =",
|
|
|
|
|
ifBreak("", " "),
|
|
|
|
|
indent([
|
|
|
|
|
ifBreak([line, " "]),
|
2022-08-02 18:11:34 +08:00
|
|
|
join([line, "| "], path.map(print, "types")),
|
2021-01-11 13:46:05 +08:00
|
|
|
]),
|
|
|
|
|
]
|
|
|
|
|
: "",
|
|
|
|
|
]),
|
|
|
|
|
]);
|
2017-06-07 19:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
2017-12-31 16:45:06 +01:00
|
|
|
case "ScalarTypeExtension":
|
2017-06-27 19:34:25 -07:00
|
|
|
case "ScalarTypeDefinition": {
|
2021-01-11 13:46:05 +08:00
|
|
|
return [
|
2022-08-02 18:11:34 +08:00
|
|
|
print("description"),
|
2021-03-11 04:14:08 +08:00
|
|
|
node.description ? hardline : "",
|
|
|
|
|
node.kind === "ScalarTypeExtension" ? "extend " : "",
|
2017-06-27 19:34:25 -07:00
|
|
|
"scalar ",
|
2022-08-02 18:11:34 +08:00
|
|
|
print("name"),
|
|
|
|
|
printDirectives(path, print, node),
|
2021-01-11 13:46:05 +08:00
|
|
|
];
|
2017-06-27 19:34:25 -07:00
|
|
|
}
|
|
|
|
|
|
2017-06-08 12:20:41 -04:00
|
|
|
case "NonNullType": {
|
2022-08-02 18:11:34 +08:00
|
|
|
return [print("type"), "!"];
|
2017-06-08 12:20:41 -04:00
|
|
|
}
|
|
|
|
|
|
2017-06-08 12:34:35 -04:00
|
|
|
case "ListType": {
|
2022-08-02 18:11:34 +08:00
|
|
|
return ["[", print("type"), "]"];
|
2017-06-08 12:34:35 -04:00
|
|
|
}
|
|
|
|
|
|
2017-06-05 15:34:08 -04:00
|
|
|
default:
|
2017-09-03 22:40:44 +02:00
|
|
|
/* istanbul ignore next */
|
2021-03-11 04:14:08 +08:00
|
|
|
throw new Error("unknown graphql type: " + JSON.stringify(node.kind));
|
2017-06-05 15:34:08 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-02 18:11:34 +08:00
|
|
|
function printDirectives(path, print, node) {
|
2021-03-11 04:14:08 +08:00
|
|
|
if (node.directives.length === 0) {
|
2017-06-06 17:42:47 -07:00
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-02 18:11:34 +08:00
|
|
|
const printed = join(line, path.map(print, "directives"));
|
2020-09-03 02:57:17 +09:00
|
|
|
|
2021-03-11 04:14:08 +08:00
|
|
|
if (
|
|
|
|
|
node.kind === "FragmentDefinition" ||
|
|
|
|
|
node.kind === "OperationDefinition"
|
|
|
|
|
) {
|
2021-01-11 13:46:05 +08:00
|
|
|
return group([line, printed]);
|
2020-09-03 02:57:17 +09:00
|
|
|
}
|
|
|
|
|
|
2021-01-11 13:46:05 +08:00
|
|
|
return [" ", group(indent([softline, printed]))];
|
2017-06-06 17:42:47 -07:00
|
|
|
}
|
|
|
|
|
|
2022-03-31 21:04:12 +08:00
|
|
|
function printSequence(path, options, print, property) {
|
2022-08-02 18:11:34 +08:00
|
|
|
return path.map((path, index, sequence) => {
|
|
|
|
|
const printed = print();
|
2017-11-12 02:10:54 -02:00
|
|
|
|
|
|
|
|
if (
|
2022-03-31 21:04:12 +08:00
|
|
|
index < sequence.length - 1 &&
|
|
|
|
|
isNextLineEmpty(options.originalText, path.getValue(), locEnd)
|
2017-11-12 02:10:54 -02:00
|
|
|
) {
|
2021-01-11 13:46:05 +08:00
|
|
|
return [printed, hardline];
|
2017-11-12 02:10:54 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return printed;
|
2022-03-31 21:04:12 +08:00
|
|
|
}, property);
|
2017-11-12 02:10:54 -02:00
|
|
|
}
|
|
|
|
|
|
2017-12-27 00:04:09 +11:00
|
|
|
function canAttachComment(node) {
|
|
|
|
|
return node.kind && node.kind !== "Comment";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function printComment(commentPath) {
|
|
|
|
|
const comment = commentPath.getValue();
|
2018-07-18 02:31:44 +05:30
|
|
|
if (comment.kind === "Comment") {
|
2019-11-19 04:22:24 +08:00
|
|
|
return "#" + comment.value.trimEnd();
|
2017-12-27 00:04:09 +11:00
|
|
|
}
|
2018-07-18 02:31:44 +05:30
|
|
|
|
2020-08-21 09:43:24 +08:00
|
|
|
/* istanbul ignore next */
|
2018-07-18 02:31:44 +05:30
|
|
|
throw new Error("Not a comment: " + JSON.stringify(comment));
|
2017-12-27 00:04:09 +11:00
|
|
|
}
|
|
|
|
|
|
2022-08-02 18:11:34 +08:00
|
|
|
function printInterfaces(path, options, print) {
|
2020-01-21 19:18:53 +08:00
|
|
|
const node = path.getNode();
|
|
|
|
|
const parts = [];
|
|
|
|
|
const { interfaces } = node;
|
2022-08-02 18:11:34 +08:00
|
|
|
const printed = path.map((node) => print(node), "interfaces");
|
2020-01-21 19:18:53 +08:00
|
|
|
|
|
|
|
|
for (let index = 0; index < interfaces.length; index++) {
|
|
|
|
|
const interfaceNode = interfaces[index];
|
|
|
|
|
parts.push(printed[index]);
|
2020-04-10 21:09:54 +08:00
|
|
|
const nextInterfaceNode = interfaces[index + 1];
|
|
|
|
|
if (nextInterfaceNode) {
|
|
|
|
|
const textBetween = options.originalText.slice(
|
|
|
|
|
interfaceNode.loc.end,
|
|
|
|
|
nextInterfaceNode.loc.start
|
|
|
|
|
);
|
|
|
|
|
const hasComment = textBetween.includes("#");
|
|
|
|
|
|
2022-05-08 20:20:34 +08:00
|
|
|
parts.push(" &", hasComment ? line : " ");
|
2020-04-10 21:09:54 +08:00
|
|
|
}
|
2018-03-27 08:52:57 +02:00
|
|
|
}
|
2020-04-10 21:09:54 +08:00
|
|
|
|
2020-01-21 19:18:53 +08:00
|
|
|
return parts;
|
2018-03-27 08:52:57 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-22 14:38:44 +02:00
|
|
|
function clean(node, newNode /* , parent */) {
|
|
|
|
|
// We print single line `""" string """` as multiple line string,
|
|
|
|
|
// and the parser ignores space in multiple line string
|
|
|
|
|
if (node.kind === "StringValue" && node.block && !node.value.includes("\n")) {
|
|
|
|
|
newNode.value = newNode.value.trim();
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-05 14:12:14 +08:00
|
|
|
clean.ignoredProperties = new Set(["loc", "comments"]);
|
2018-05-14 20:52:27 +08:00
|
|
|
|
2020-11-10 09:12:46 +08:00
|
|
|
function hasPrettierIgnore(path) {
|
|
|
|
|
const node = path.getValue();
|
2022-04-24 10:28:20 +08:00
|
|
|
return node?.comments?.some(
|
|
|
|
|
(comment) => comment.value.trim() === "prettier-ignore"
|
2020-11-10 09:12:46 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-21 10:25:39 +08:00
|
|
|
const printer = {
|
2017-12-26 12:23:50 +11:00
|
|
|
print: genericPrint,
|
2018-05-14 20:52:27 +08:00
|
|
|
massageAstNode: clean,
|
2020-11-10 09:12:46 +08:00
|
|
|
hasPrettierIgnore,
|
2018-06-17 14:04:48 +08:00
|
|
|
insertPragma,
|
2017-12-27 00:04:09 +11:00
|
|
|
printComment,
|
|
|
|
|
canAttachComment,
|
2022-08-17 09:47:08 +08:00
|
|
|
getVisitorKeys: createGetVisitorKeys(visitorKeys, "kind"),
|
2017-12-26 12:23:50 +11:00
|
|
|
};
|
2022-02-21 10:25:39 +08:00
|
|
|
|
|
|
|
|
export default printer;
|