-
Notifications
You must be signed in to change notification settings - Fork 9
feat apilinks.json generator #153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.