-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathindex.mjs
More file actions
84 lines (67 loc) · 2.4 KB
/
index.mjs
File metadata and controls
84 lines (67 loc) · 2.4 KB
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
'use strict';
import { basename, join } from 'node:path';
import { writeFile } from 'node:fs/promises';
import { extractExports } from './utils/extractExports.mjs';
import { findDefinitions } from './utils/findDefinitions.mjs';
import { checkIndirectReferences } from './utils/checkIndirectReferences.mjs';
/**
* This generator is responsible for mapping publicly accessible functions in
* Node.js to their source locations in the Node.js repository.
*
* This is a top-level generator. It takes in the raw AST tree of the JavaScript
* source files. It outputs a `apilinks.json` file into the specified output
* directory.
*
* @typedef {Array<JsProgram>} Input
*
* @type {GeneratorMetadata<Input, Record<string, string>>}
*/
export default {
name: 'api-links',
version: '1.0.0',
description:
'Creates a mapping of publicly accessible functions to their source locations in the Node.js repository.',
// Unlike the rest of the generators, this utilizes Javascript sources being
// passed into the input field rather than Markdown.
dependsOn: 'ast-js',
/**
* Generates the `apilinks.json` file.
*
* @param {Input} input
* @param {Partial<GeneratorOptions>} options
*/
async generate(input, { output, gitRef }) {
/**
* @type Record<string, string>
*/
const definitions = {};
const gitBaseUrl = `https://${gitRef.host}/${gitRef.full_name}/blob/${gitRef.commit ?? 'HEAD'}`;
input.forEach(program => {
/**
* Mapping of definitions to their line number
*
* @type {Record<string, number>}
* @example { 'someclass.foo': 10 }
*/
const nameToLineNumberMap = {};
// `http.js` -> `http`
const baseName = basename(program.path, '.js');
const exports = extractExports(program, baseName, nameToLineNumberMap);
findDefinitions(program, baseName, nameToLineNumberMap, exports);
checkIndirectReferences(program, exports, nameToLineNumberMap);
const fullGitUrl = `${gitBaseUrl}/lib/${baseName}.js`;
// Add the exports we found in this program to our output
Object.keys(nameToLineNumberMap).forEach(key => {
const lineNumber = nameToLineNumberMap[key];
definitions[key] = `${fullGitUrl}#L${lineNumber}`;
});
});
if (output) {
await writeFile(
join(output, 'apilinks.json'),
JSON.stringify(definitions)
);
}
return definitions;
},
};