-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #152 Signed-off-by: flakey5 <73616808+flakey5@users.noreply.github.com>
- Loading branch information
Showing
15 changed files
with
645 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
'use strict'; | ||
|
||
import { basename, dirname, join } from 'node:path'; | ||
import { writeFile } from 'node:fs/promises'; | ||
import { getGitRepository, getGitTag } from '../../utils/git.mjs'; | ||
import { extractExports } from './utils/extractExports.mjs'; | ||
import { findDefinitions } from './utils/findDefinitions.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 {import('../types.d.ts').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.', | ||
|
||
dependsOn: 'ast-js', | ||
|
||
/** | ||
* Generates the `apilinks.json` file. | ||
* | ||
* @param {Input} input | ||
* @param {Partial<GeneratorOptions>} options | ||
*/ | ||
async generate(input, { output }) { | ||
/** | ||
* @type {Record<string, string>} | ||
*/ | ||
const definitions = {}; | ||
|
||
/** | ||
* @type {string} | ||
*/ | ||
let baseGithubLink; | ||
|
||
input.forEach(program => { | ||
/** | ||
* Mapping of definitions to their line number | ||
* @type {Record<string, number>} | ||
* @example { 'someclass.foo', 10 } | ||
*/ | ||
const nameToLineNumberMap = {}; | ||
|
||
const programBasename = basename(program.path, '.js'); | ||
|
||
const exports = extractExports( | ||
program, | ||
programBasename, | ||
nameToLineNumberMap | ||
); | ||
|
||
findDefinitions(program, programBasename, nameToLineNumberMap, exports); | ||
|
||
if (!baseGithubLink) { | ||
const directory = dirname(program.path); | ||
|
||
const repository = getGitRepository(directory); | ||
|
||
const tag = getGitTag(directory); | ||
|
||
baseGithubLink = `https://github.com/${repository}/blob/${tag}`; | ||
} | ||
|
||
const githubLink = | ||
`${baseGithubLink}/lib/${programBasename}.js`.replaceAll('\\', '/'); | ||
|
||
Object.keys(nameToLineNumberMap).forEach(key => { | ||
const lineNumber = nameToLineNumberMap[key]; | ||
|
||
definitions[key] = `${githubLink}#L${lineNumber}`; | ||
}); | ||
}); | ||
|
||
if (output) { | ||
await writeFile( | ||
join(output, 'apilinks.json'), | ||
JSON.stringify(definitions) | ||
); | ||
} | ||
|
||
return definitions; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface ProgramExports { | ||
ctors: Array<string>; | ||
identifiers: Array<string>; | ||
} |
Oops, something went wrong.