-
Notifications
You must be signed in to change notification settings - Fork 8
/
typeAstToMdAst.mjs
197 lines (186 loc) · 5.89 KB
/
typeAstToMdAst.mjs
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* Converts a JSDoc type Doctrine AST node to markdown AST children list.
* @kind function
* @name typeAstToMdAst
* @param {object} typeJsdocAst JSDoc type Doctrine AST node.
* @param {Array<JsdocMember>} members Outlined JSDoc members.
* @returns {Array<object>} Markdown AST children list.
* @ignore
*/
export default function typeAstToMdAst(typeJsdocAst, members) {
if (typeof typeJsdocAst !== "object")
throw new TypeError("Argument 1 `typeJsdocAst` must be an object.");
if (!Array.isArray(members))
throw new TypeError("Argument 2 `members` must be an array.");
const children = [];
switch (typeJsdocAst.type) {
case "AllLiteral":
children.push({ type: "text", value: "*" });
break;
case "NullableLiteral":
children.push({ type: "text", value: "?" });
break;
case "NullLiteral":
children.push({ type: "inlineCode", value: "null" });
break;
case "UndefinedLiteral":
children.push({ type: "inlineCode", value: "undefined" });
break;
case "VoidLiteral":
children.push({ type: "inlineCode", value: "void" });
break;
case "NumericLiteralType":
case "BooleanLiteralType":
children.push({ type: "inlineCode", value: String(typeJsdocAst.value) });
break;
case "StringLiteralType":
children.push({
type: "inlineCode",
value:
// It would be nice to always display quotes around strings, but
// escaping nested quotes would be tricky and may confuse readers
// about what the value is exactly. Quotes are necessary if the string
// is empty or only contains whitespace to avoid resulting empty
// inline code and markdown rendering issues.
typeJsdocAst.value.match(/^\s*$/u)
? `'${typeJsdocAst.value}'`
: typeJsdocAst.value,
});
break;
case "RestType":
children.push(
{ type: "text", value: "…" },
...typeAstToMdAst(typeJsdocAst.expression, members)
);
break;
case "OptionalType":
children.push(...typeAstToMdAst(typeJsdocAst.expression, members), {
type: "text",
value: "?",
});
break;
case "UnionType": {
for (const [
index,
elementTypeJsdocAst,
] of typeJsdocAst.elements.entries()) {
if (index !== 0)
children.push({
type: "text",
value:
// These are special no-break space Unicode characters.
" | ",
});
children.push(...typeAstToMdAst(elementTypeJsdocAst, members));
}
break;
}
case "TypeApplication": {
children.push(...typeAstToMdAst(typeJsdocAst.expression, members), {
type: "text",
value: "<",
});
for (const [
index,
applicationTypeJsdocAst,
] of typeJsdocAst.applications.entries()) {
if (index !== 0)
children.push({
type: "text",
value:
// This is a special no-break space Unicode character.
", ",
});
children.push(...typeAstToMdAst(applicationTypeJsdocAst, members));
}
children.push({ type: "text", value: ">" });
break;
}
case "ArrayType": {
children.push({ type: "text", value: "[" });
for (const [
index,
elementTypeJsdocAst,
] of typeJsdocAst.elements.entries()) {
if (index !== 0)
children.push({
type: "text",
value:
// These are special no-break space Unicode characters.
", ",
});
children.push(...typeAstToMdAst(elementTypeJsdocAst, members));
}
children.push({ type: "text", value: "]" });
break;
}
case "FieldType":
children.push(
{ type: "text", value: `${typeJsdocAst.key}: ` },
...typeAstToMdAst(typeJsdocAst.value, members)
);
break;
case "RecordType": {
children.push({ type: "text", value: "{" });
for (const [index, fieldTypeJsdocAst] of typeJsdocAst.fields.entries()) {
if (index !== 0)
children.push({
type: "text",
value:
// This is a special no-break space Unicode character.
", ",
});
children.push(...typeAstToMdAst(fieldTypeJsdocAst, members));
}
children.push({ type: "text", value: "}" });
break;
}
case "NameExpression": {
const linkedMember = members.find(
({ namepath: { data } }) => data === typeJsdocAst.name
);
children.push(
linkedMember
? {
type: "link",
url: `#${linkedMember.slug}`,
children: [{ type: "text", value: typeJsdocAst.name }],
}
: { type: "text", value: typeJsdocAst.name }
);
break;
}
case "FunctionType": {
children.push({ type: "text", value: "function(" });
if (typeJsdocAst.this)
children.push(
{ type: "text", value: `${typeJsdocAst.new ? "new" : "this"}:` },
...typeAstToMdAst(typeJsdocAst.this, members)
);
if (typeJsdocAst.params.length)
for (const [
index,
paramTypeJsdocAst,
] of typeJsdocAst.params.entries()) {
if (index !== 0 || typeJsdocAst.this)
children.push({
type: "text",
value:
// This is a special no-break space Unicode character.
", ",
});
children.push(...typeAstToMdAst(paramTypeJsdocAst, members));
}
children.push({ type: "text", value: ")" });
if (typeJsdocAst.result)
children.push(
{ type: "text", value: ":" },
...typeAstToMdAst(typeJsdocAst.result, members)
);
break;
}
default:
throw new TypeError(`Unknown JSDoc type \`${typeJsdocAst.type}\`.`);
}
return children;
}