-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathgenerate-symbol-graph
executable file
·333 lines (301 loc) · 9.64 KB
/
generate-symbol-graph
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/usr/bin/env node
/**
* This source file is part of the Swift.org open source project
*
* Copyright (c) 2023 Apple Inc. and the Swift project authors
* Licensed under Apache License v2.0 with Runtime Library Exception
*
* See https://swift.org/LICENSE.txt for license information
* See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
/* eslint-disable no-restricted-syntax, import/no-extraneous-dependencies */
const fs = require('fs');
const path = require('path');
const Parser = require('tree-sitter');
const JavaScript = require('tree-sitter-javascript');
const JSDoc = require('tree-sitter-jsdoc');
const Vue = require('tree-sitter-vue');
async function* find(dir, predicate = () => true) {
const files = await fs.promises.readdir(dir);
for await (const file of files) {
const fpath = path.join(dir, file);
const fstat = await fs.promises.stat(fpath);
if (fstat.isDirectory()) {
yield* find(fpath, predicate);
} else if (predicate(fpath)) {
yield fpath;
}
}
}
async function* findVueFiles(dir) {
const isVueFile = fpath => path.extname(fpath) === '.vue';
yield* find(dir, isVueFile);
}
function uniqueCaptures(query, tree) {
const capturesArray = query.captures(tree.rootNode);
return capturesArray.reduce((obj, capture) => ({
...obj,
[capture.name]: capture.node,
}), {});
}
const line = (
text,
range = {
start: { // FIXME: use real ranges from parser in the future
line: 0,
character: 0,
},
end: {
line: 0,
character: 0,
},
},
) => ({ text, range });
function createDocComment(descriptionNode = { text: '' }, params = []) {
const lines = descriptionNode.text.split('\n').map((txt, i) => line((i === 0 ? (
txt
) : (
// this seems like a tree-sitter-jsdoc bug with handling
// multi-line descriptions
txt.replace(/^\s*\*/, '')
))));
// generate automated parameter description content that just shows the name
// of each prop and its type
const hasParamContent = lines.some(l => /^\s*- Parameter/.test(l.text));
if (params.length && !hasParamContent) {
lines.push(line(''));
lines.push(line('- Parameters:'));
params.forEach((param) => {
lines.push(line(` - ${param.name}: \`${param.type}\``));
});
}
return { lines };
}
const Token = {
identifier: spelling => ({ kind: 'identifier', spelling }),
string: spelling => ({ kind: 'string', spelling }),
text: spelling => ({ kind: 'text', spelling }),
typeIdentifier: spelling => ({ kind: 'typeIdentifier', spelling }),
};
function createDeclaration(componentName, slotNames = []) {
if (!slotNames.length) {
return [
Token.text('<'),
Token.typeIdentifier(componentName),
Token.text(' />'),
];
}
const isDefault = name => name === 'default';
return [
Token.text('<'),
Token.typeIdentifier(componentName),
Token.text('>\n'),
...slotNames.flatMap(name => (isDefault(name) ? ([
Token.text(' <slot />\n'),
]) : ([
Token.text(' <slot name='),
Token.string(`"${name}"`),
Token.text(' />\n'),
]))),
Token.text('</'),
Token.typeIdentifier(componentName),
Token.text('>'),
];
}
(async () => {
const vueParser = new Parser();
vueParser.setLanguage(Vue);
const scriptTextQuery = new Parser.Query(Vue,
`(script_element
(raw_text) @script)`);
const jsParser = new Parser();
jsParser.setLanguage(JavaScript);
const exportNameQuery = new Parser.Query(JavaScript,
`(
(comment)? @comment (#match? @comment "^/[*]{2}")
.
(export_statement
(object
(pair
(property_identifier) @key (#eq? @key "name")
.
(string (string_fragment) @component)))))`);
const exportPropsQuery = new Parser.Query(JavaScript,
`(export_statement
(object
(pair
(property_identifier) @key (#eq? @key "props")
.
(object
(pair
(property_identifier) @prop.name
.
(object
(pair
(property_identifier) @key2 (#eq? @key2 "type")
.
(_) @prop.type)))))))`);
const jsDocParser = new Parser();
jsDocParser.setLanguage(JSDoc);
const commentDescriptionQuery = new Parser.Query(JSDoc,
`(document
(description) @description)`);
const slotsQuery = new Parser.Query(Vue,
`[
(self_closing_tag
(tag_name) @tag
(attribute
(attribute_name) @attr.name
(quoted_attribute_value (attribute_value) @attr.value))?
(#eq? @tag "slot")
(#eq? @attr.name "name"))
(start_tag
(tag_name) @tag
(attribute
(attribute_name) @attr.name
(quoted_attribute_value (attribute_value) @attr.value))?
(#eq? @tag "slot")
(#eq? @attr.name "name"))
]`);
const symbols = [];
const relationships = [];
const identifiers = new Set();
const rootDir = path.join(__dirname, '..');
const componentsDir = path.join(rootDir, 'src/components');
for await (const filepath of findVueFiles(componentsDir)) {
const contents = await fs.promises.readFile(filepath, { encoding: 'utf8' });
const vueTree = vueParser.parse(contents);
const { script } = uniqueCaptures(scriptTextQuery, vueTree);
if (script) {
const jsTree = jsParser.parse(script.text);
const { comment, component } = uniqueCaptures(exportNameQuery, jsTree);
if (component) {
const componentName = component.text;
const pathComponents = filepath
.replace(componentsDir, '')
.split('/')
.filter(part => part.length)
.map(part => path.parse(part).name);
const preciseIdentifier = pathComponents.join('');
const subHeading = [
Token.text('<'),
Token.identifier(componentName),
Token.text('>'),
];
let functionSignature;
const captures = exportPropsQuery.captures(jsTree.rootNode);
const params = captures.reduce((memo, capture) => {
if (capture.name === 'prop.name') {
memo.push({ name: capture.node.text });
}
if (capture.name === 'prop.type') {
// eslint-disable-next-line no-param-reassign
memo[memo.length - 1].type = capture.node.text;
}
return memo;
}, []);
if (params.length) {
// not sure if DocC actually uses `functionSignature` or not...
functionSignature = {
parameters: params.map(param => ({
name: param.name,
declarationFragments: [Token.text(param.type)],
})),
};
}
// TODO: eventually we should also capture slots that are expressed in
// a render function instead of the template
const slots = slotsQuery.captures(vueTree.rootNode).reduce((memo, capture) => {
if (capture.name === 'tag') {
memo.push({ name: 'default' });
}
if (capture.name === 'attr.value') {
// eslint-disable-next-line no-param-reassign
memo[memo.length - 1].name = capture.node.text;
}
return memo;
}, []);
const slotNames = [...new Set(slots.map(slot => slot.name))];
const declarationFragments = createDeclaration(componentName, slotNames);
let docComment;
let description;
if (comment) {
const jsDocTree = jsDocParser.parse(comment.text);
description = uniqueCaptures(commentDescriptionQuery, jsDocTree).description;
}
if (!!description || params.length) {
docComment = createDocComment(description, params);
}
symbols.push({
accessLevel: 'public',
identifier: {
interfaceLanguage: 'vue',
precise: preciseIdentifier,
},
kind: {
identifier: 'class', // FIXME
displayName: 'Component',
},
names: {
title: componentName,
subHeading,
},
pathComponents,
docComment,
declarationFragments,
functionSignature,
});
identifiers.add(preciseIdentifier);
}
}
}
// construct parent/child relationships and fixup the `pathComponents` for
// each symbol so that it only contains items that map to real symbols (TODO:
// this could probably be done in the first loop depending on the order that
// `find` traverses the filesystem (breadth vs depth))
for (let i = 0; i < symbols.length; i += 1) {
const symbol = symbols[i];
const {
identifier: { precise: childIdentifier },
pathComponents,
} = symbol;
const parentPathComponents = pathComponents.slice(0, pathComponents.length - 1);
if (!parentPathComponents.length) {
// eslint-disable-next-line no-continue
continue;
}
const parentIdentifier = parentPathComponents.join('');
if (identifiers.has(parentIdentifier)) {
relationships.push({
source: childIdentifier,
target: parentIdentifier,
kind: 'memberOf',
});
} else {
symbol.pathComponents = pathComponents.filter((_, j) => (
identifiers.has(pathComponents.slice(0, j + 1).join(''))
));
}
}
const formatVersion = {
major: 0,
minor: 1,
patch: 0,
};
const metadata = {
formatVersion,
generator: 'SwiftDocCRender',
};
const $module = {
name: 'SwiftDocCRender',
platform: {},
};
const symbolGraph = {
metadata,
module: $module,
relationships,
symbols,
};
const symbolGraphJSON = JSON.stringify(symbolGraph);
console.log(symbolGraphJSON);
})();