-
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.
* feat apilinks.json generator Closes #152 Signed-off-by: flakey5 <[email protected]> Co-authored-by: Claudio W <[email protected]> * Update getBaseGitHubUrl.mjs --------- Co-authored-by: Claudio W <[email protected]>
- Loading branch information
Showing
22 changed files
with
832 additions
and
20 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,4 @@ | ||
'use strict'; | ||
|
||
// Checks if a string is a valid name for a constructor in JavaScript | ||
export const CONSTRUCTOR_EXPRESSION = /^[A-Z]/; |
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,105 @@ | ||
'use strict'; | ||
|
||
import { basename, dirname, join } from 'node:path'; | ||
import { writeFile } from 'node:fs/promises'; | ||
import { | ||
getBaseGitHubUrl, | ||
getCurrentGitHash, | ||
} from './utils/getBaseGitHubUrl.mjs'; | ||
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 {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.', | ||
|
||
// 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 }) { | ||
/** | ||
* @type Record<string, string> | ||
*/ | ||
const definitions = {}; | ||
|
||
/** | ||
* @type {string} | ||
*/ | ||
let baseGithubLink; | ||
|
||
if (input.length > 0) { | ||
const repositoryDirectory = dirname(input[0].path); | ||
|
||
const repository = getBaseGitHubUrl(repositoryDirectory); | ||
|
||
const tag = getCurrentGitHash(repositoryDirectory); | ||
|
||
baseGithubLink = `${repository}/blob/${tag}`; | ||
} | ||
|
||
input.forEach(program => { | ||
/** | ||
* Mapping of definitions to their line number | ||
* @type {Record<string, number>} | ||
* @example { 'someclass.foo': 10 } | ||
*/ | ||
const nameToLineNumberMap = {}; | ||
|
||
// `http.js` -> `http` | ||
const programBasename = basename(program.path, '.js'); | ||
|
||
const exports = extractExports( | ||
program, | ||
programBasename, | ||
nameToLineNumberMap | ||
); | ||
|
||
findDefinitions(program, programBasename, nameToLineNumberMap, exports); | ||
|
||
checkIndirectReferences(program, exports, nameToLineNumberMap); | ||
|
||
const githubLink = | ||
`${baseGithubLink}/lib/${programBasename}.js`.replaceAll('\\', '/'); | ||
|
||
// Add the exports we found in this program to our output | ||
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,5 @@ | ||
export interface ProgramExports { | ||
ctors: Array<string>; | ||
identifiers: Array<string>; | ||
indirects: Record<string, string>; | ||
} |
24 changes: 24 additions & 0 deletions
24
src/generators/api-links/utils/checkIndirectReferences.mjs
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,24 @@ | ||
import { visit } from 'estree-util-visit'; | ||
|
||
/** | ||
* @param {import('acorn').Program} program | ||
* @param {import('../types.d.ts').ProgramExports} exports | ||
* @param {Record<string, number>} nameToLineNumberMap | ||
*/ | ||
export function checkIndirectReferences(program, exports, nameToLineNumberMap) { | ||
if (Object.keys(exports.indirects).length === 0) { | ||
return; | ||
} | ||
|
||
visit(program, node => { | ||
if (!node.loc || node.type !== 'FunctionDeclaration') { | ||
return; | ||
} | ||
|
||
const name = node.id.name; | ||
|
||
if (name in exports.indirects) { | ||
nameToLineNumberMap[exports.indirects[name]] = node.loc.start.line; | ||
} | ||
}); | ||
} |
Oops, something went wrong.