-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
docs.js
162 lines (150 loc) 路 4.33 KB
/
docs.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
import util from "util";
import stringifyValidator from "../utils/stringifyValidator.js";
import toFunctionName from "../utils/toFunctionName.js";
import t from "../../lib/index.js";
const readme = [
`---
id: babel-types
title: @babel/types
---
<!-- Do not modify! This file is automatically generated by
github.com/babel/babel/babel-types/scripts/generators/docs.js !-->
> This module contains methods for building ASTs manually and for checking the types of AST nodes.
## Install
\`\`\`sh
npm install --save-dev @babel/types
\`\`\`
## API`,
];
const customTypes = {
ClassMethod: {
key: "if computed then `Expression` else `Identifier | Literal`",
},
Identifier: {
name: "`string`",
},
MemberExpression: {
property: "if computed then `Expression` else `Identifier`",
},
ObjectMethod: {
key: "if computed then `Expression` else `Identifier | Literal`",
},
ObjectProperty: {
key: "if computed then `Expression` else `Identifier | Literal`",
},
ClassPrivateMethod: {
computed: "'false'",
},
ClassPrivateProperty: {
computed: "'false'",
},
};
const APIHistory = {
ClassProperty: [["v7.6.0", "Supports `static`"]],
};
function formatHistory(historyItems) {
const lines = historyItems.map(
item => "| `" + item[0] + "` | " + item[1] + " |"
);
return [
"<details>",
" <summary>History</summary>",
"| Version | Changes |",
"| --- | --- |",
...lines,
"</details>",
];
}
function printAPIHistory(key, readme) {
if (APIHistory[key]) {
readme.push("");
readme.push(...formatHistory(APIHistory[key]));
}
}
function printNodeFields(key, readme) {
if (Object.keys(t.NODE_FIELDS[key]).length > 0) {
readme.push("");
readme.push("AST Node `" + key + "` shape:");
Object.keys(t.NODE_FIELDS[key])
.sort(function (fieldA, fieldB) {
const indexA = t.BUILDER_KEYS[key].indexOf(fieldA);
const indexB = t.BUILDER_KEYS[key].indexOf(fieldB);
if (indexA === indexB) return fieldA < fieldB ? -1 : 1;
if (indexA === -1) return 1;
if (indexB === -1) return -1;
return indexA - indexB;
})
.forEach(function (field) {
const defaultValue = t.NODE_FIELDS[key][field].default;
const fieldDescription = ["`" + field + "`"];
const validator = t.NODE_FIELDS[key][field].validate;
if (customTypes[key] && customTypes[key][field]) {
fieldDescription.push(`: ${customTypes[key][field]}`);
} else if (validator) {
try {
fieldDescription.push(
": `" + stringifyValidator(validator, "") + "`"
);
} catch (ex) {
if (ex.code === "UNEXPECTED_VALIDATOR_TYPE") {
console.log(
"Unrecognised validator type for " + key + "." + field
);
console.dir(ex.validator, { depth: 10, colors: true });
}
}
}
if (defaultValue !== null || t.NODE_FIELDS[key][field].optional) {
fieldDescription.push(
" (default: `" + util.inspect(defaultValue) + "`"
);
if (t.BUILDER_KEYS[key].indexOf(field) < 0) {
fieldDescription.push(", excluded from builder function");
}
fieldDescription.push(")");
} else {
fieldDescription.push(" (required)");
}
readme.push("- " + fieldDescription.join(""));
});
}
}
function printAliasKeys(key, readme) {
if (t.ALIAS_KEYS[key] && t.ALIAS_KEYS[key].length) {
readme.push("");
readme.push(
"Aliases: " +
t.ALIAS_KEYS[key]
.map(function (key) {
return "`" + key + "`";
})
.join(", ")
);
}
}
Object.keys(t.BUILDER_KEYS)
.sort()
.forEach(function (key) {
readme.push("### " + toFunctionName(key));
readme.push("");
readme.push("```javascript");
readme.push(
"t." + toFunctionName(key) + "(" + t.BUILDER_KEYS[key].join(", ") + ");"
);
readme.push("```");
printAPIHistory(key, readme);
readme.push("");
readme.push(
"See also `t.is" +
key +
"(node, opts)` and `t.assert" +
key +
"(node, opts)`."
);
printNodeFields(key, readme);
printAliasKeys(key, readme);
readme.push("");
readme.push("---");
readme.push("");
});
process.stdout.write(readme.join("\n"));