forked from tidalcycles/strudel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathundocumented.mjs
82 lines (74 loc) · 2.44 KB
/
undocumented.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
import { readFile } from 'node:fs/promises';
import { dirname, resolve } from 'path';
import { fileURLToPath } from 'url';
import { parse } from 'acorn';
import dependencyTree from 'dependency-tree';
const __dirname = dirname(fileURLToPath(import.meta.url));
function getExports(code) {
// parse it with acorn
let ast;
try {
ast = parse(code, {
ecmaVersion: 11,
sourceType: 'module',
});
} catch (err) {
return [`acorn parse error: ${err.name}: ${err.messsage}`];
}
// find all names exported in the file
return ast.body
.filter((node) => node?.type === 'ExportNamedDeclaration')
.map((node) => {
const { declaration } = node;
if (!declaration) {
// e.g. "export { Fraction, controls }"
return [];
}
switch (declaration.type) {
case 'VariableDeclaration':
return declaration.declarations
.map((d) => {
switch (d.id.type) {
case 'Identifier':
return d.id.name;
case 'ObjectPattern':
return d.id.properties.map((p) => p.value.name);
default:
return 'unknown declaration: ' + declaration;
}
})
.flat();
default:
// FunctionDeclaration, ClassDeclaration
return declaration.id.name;
}
})
.flat();
}
function isDocumented(name, docs) {
return docs.find(
(d) => d.name === name || d.tags?.find((t) => t.title === 'synonyms' && t.value.split(', ').includes(name)),
);
}
async function getUndocumented(path, docs) {
try {
// load the code of pattern.mjs as a string
const code = await readFile(path, 'utf8');
return getExports(code).filter((name) => !isDocumented(name, docs));
} catch (err) {
return [`parse error: ${err.name}: ${err.message}`];
}
}
// read doc.json file
const { docs } = JSON.parse(await readFile(resolve(__dirname, 'doc.json'), 'utf8'));
const paths = dependencyTree.toList({
filename: 'index.mjs',
// filename: 'packages/core/index.mjs',
directory: resolve(__dirname),
filter: (path) => !path.includes('node_modules'),
});
// const paths = ['../packages/core/pattern.mjs', '../packages/core/hap.mjs'].map((rel) => resolve(__dirname, rel));
const undocumented = Object.fromEntries(
await Promise.all(paths.map(async (path) => [path, await getUndocumented(path, docs)])),
);
console.log(JSON.stringify(undocumented, null, 2));