forked from prettier/prettier
/
type-parameters.js
166 lines (144 loc) · 4.07 KB
/
type-parameters.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
"use strict";
const { printDanglingComments } = require("../../main/comments.js");
const {
builders: { join, line, hardline, softline, group, indent, ifBreak },
} = require("../../document/index.js");
const {
isTestCall,
hasComment,
CommentCheckFlags,
isTSXFile,
shouldPrintComma,
getFunctionParameters,
isObjectType,
getTypeScriptMappedTypeModifier,
} = require("../utils/index.js");
const { createGroupIdMapper } = require("../../common/util.js");
const { shouldHugType } = require("./type-annotation.js");
const { isArrowFunctionVariableDeclarator } = require("./assignment.js");
const getTypeParametersGroupId = createGroupIdMapper("typeParameters");
function printTypeParameters(path, options, print, paramsKey) {
const node = path.getValue();
if (!node[paramsKey]) {
return "";
}
// for TypeParameterDeclaration typeParameters is a single node
if (!Array.isArray(node[paramsKey])) {
return print(paramsKey);
}
const grandparent = path.getNode(2);
const isParameterInTestCall = grandparent && isTestCall(grandparent);
const isArrowFunctionVariable = path.match(
(node) =>
!(node[paramsKey].length === 1 && isObjectType(node[paramsKey][0])),
undefined,
(node, name) => name === "typeAnnotation",
(node) => node.type === "Identifier",
isArrowFunctionVariableDeclarator
);
const shouldInline =
node[paramsKey].length === 0 ||
(!isArrowFunctionVariable &&
(isParameterInTestCall ||
(node[paramsKey].length === 1 &&
(node[paramsKey][0].type === "NullableTypeAnnotation" ||
shouldHugType(node[paramsKey][0])))));
if (shouldInline) {
return [
"<",
join(", ", path.map(print, paramsKey)),
printDanglingCommentsForInline(path, options),
">",
];
}
// Keep comma if the file extension is .tsx and
// has one type parameter that isn't extend with any types.
// Because, otherwise formatted result will be invalid as tsx.
const trailingComma =
node.type === "TSTypeParameterInstantiation" // https://github.com/microsoft/TypeScript/issues/21984
? ""
: getFunctionParameters(node).length === 1 &&
isTSXFile(options) &&
!node[paramsKey][0].constraint &&
path.getParentNode().type === "ArrowFunctionExpression"
? ","
: shouldPrintComma(options, "all")
? ifBreak(",")
: "";
return group(
[
"<",
indent([softline, join([",", line], path.map(print, paramsKey))]),
trailingComma,
softline,
">",
],
{ id: getTypeParametersGroupId(node) }
);
}
function printDanglingCommentsForInline(path, options) {
const node = path.getValue();
if (!hasComment(node, CommentCheckFlags.Dangling)) {
return "";
}
const hasOnlyBlockComments = !hasComment(node, CommentCheckFlags.Line);
const printed = printDanglingComments(
path,
options,
/* sameIndent */ hasOnlyBlockComments
);
if (hasOnlyBlockComments) {
return printed;
}
return [printed, hardline];
}
function printTypeParameter(path, options, print) {
const node = path.getValue();
const parts = [];
const parent = path.getParentNode();
if (parent.type === "TSMappedType") {
if (parent.readonly) {
parts.push(
getTypeScriptMappedTypeModifier(parent.readonly, "readonly"),
" "
);
}
parts.push("[", print("name"));
if (node.constraint) {
parts.push(" in ", print("constraint"));
}
if (parent.nameType) {
parts.push(
" as ",
path.callParent(() => print("nameType"))
);
}
parts.push("]");
return parts;
}
if (node.variance) {
parts.push(print("variance"));
}
if (node.in) {
parts.push("in ");
}
if (node.out) {
parts.push("out ");
}
parts.push(print("name"));
if (node.bound) {
parts.push(": ", print("bound"));
}
if (node.constraint) {
parts.push(" extends ", print("constraint"));
}
if (node.default) {
parts.push(" = ", print("default"));
}
return parts;
}
module.exports = {
printTypeParameter,
printTypeParameters,
getTypeParametersGroupId,
};