|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import { basename, dirname, join } from 'node:path'; |
| 4 | +import { writeFile } from 'node:fs/promises'; |
| 5 | +import { |
| 6 | + getBaseGitHubUrl, |
| 7 | + getCurrentGitHash, |
| 8 | +} from './utils/getBaseGitHubUrl.mjs'; |
| 9 | +import { extractExports } from './utils/extractExports.mjs'; |
| 10 | +import { findDefinitions } from './utils/findDefinitions.mjs'; |
| 11 | +import { checkIndirectReferences } from './utils/checkIndirectReferences.mjs'; |
| 12 | + |
| 13 | +/** |
| 14 | + * This generator is responsible for mapping publicly accessible functions in |
| 15 | + * Node.js to their source locations in the Node.js repository. |
| 16 | + * |
| 17 | + * This is a top-level generator. It takes in the raw AST tree of the JavaScript |
| 18 | + * source files. It outputs a `apilinks.json` file into the specified output |
| 19 | + * directory. |
| 20 | + * |
| 21 | + * @typedef {Array<JsProgram>} Input |
| 22 | + * |
| 23 | + * @type {import('../types.d.ts').GeneratorMetadata<Input, Record<string, string>>} |
| 24 | + */ |
| 25 | +export default { |
| 26 | + name: 'api-links', |
| 27 | + |
| 28 | + version: '1.0.0', |
| 29 | + |
| 30 | + description: |
| 31 | + 'Creates a mapping of publicly accessible functions to their source locations in the Node.js repository.', |
| 32 | + |
| 33 | + // Unlike the rest of the generators, this utilizes Javascript sources being |
| 34 | + // passed into the input field rather than Markdown. |
| 35 | + dependsOn: 'ast-js', |
| 36 | + |
| 37 | + /** |
| 38 | + * Generates the `apilinks.json` file. |
| 39 | + * |
| 40 | + * @param {Input} input |
| 41 | + * @param {Partial<GeneratorOptions>} options |
| 42 | + */ |
| 43 | + async generate(input, { output }) { |
| 44 | + /** |
| 45 | + * @type Record<string, string> |
| 46 | + */ |
| 47 | + const definitions = {}; |
| 48 | + |
| 49 | + /** |
| 50 | + * @type {string} |
| 51 | + */ |
| 52 | + let baseGithubLink; |
| 53 | + |
| 54 | + if (input.length > 0) { |
| 55 | + const repositoryDirectory = dirname(input[0].path); |
| 56 | + |
| 57 | + const repository = getBaseGitHubUrl(repositoryDirectory); |
| 58 | + |
| 59 | + const tag = getCurrentGitHash(repositoryDirectory); |
| 60 | + |
| 61 | + baseGithubLink = `${repository}/blob/${tag}`; |
| 62 | + } |
| 63 | + |
| 64 | + input.forEach(program => { |
| 65 | + /** |
| 66 | + * Mapping of definitions to their line number |
| 67 | + * @type {Record<string, number>} |
| 68 | + * @example { 'someclass.foo': 10 } |
| 69 | + */ |
| 70 | + const nameToLineNumberMap = {}; |
| 71 | + |
| 72 | + // `http.js` -> `http` |
| 73 | + const programBasename = basename(program.path, '.js'); |
| 74 | + |
| 75 | + const exports = extractExports( |
| 76 | + program, |
| 77 | + programBasename, |
| 78 | + nameToLineNumberMap |
| 79 | + ); |
| 80 | + |
| 81 | + findDefinitions(program, programBasename, nameToLineNumberMap, exports); |
| 82 | + |
| 83 | + checkIndirectReferences(program, exports, nameToLineNumberMap); |
| 84 | + |
| 85 | + const githubLink = |
| 86 | + `${baseGithubLink}/lib/${programBasename}.js`.replaceAll('\\', '/'); |
| 87 | + |
| 88 | + // Add the exports we found in this program to our output |
| 89 | + Object.keys(nameToLineNumberMap).forEach(key => { |
| 90 | + const lineNumber = nameToLineNumberMap[key]; |
| 91 | + |
| 92 | + definitions[key] = `${githubLink}#L${lineNumber}`; |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + if (output) { |
| 97 | + await writeFile( |
| 98 | + join(output, 'apilinks.json'), |
| 99 | + JSON.stringify(definitions) |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + return definitions; |
| 104 | + }, |
| 105 | +}; |
0 commit comments