Skip to content

Commit d8527a5

Browse files
committed
review
1 parent 065ba36 commit d8527a5

File tree

13 files changed

+390
-212
lines changed

13 files changed

+390
-212
lines changed

bin/cli.mjs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,14 @@ program
6868
*/
6969
const { input, output, target = [], version, changelog } = program.opts();
7070

71-
const { loadFiles, loadJsFiles } = createLoader();
71+
const { loadMarkdownFiles, loadJsFiles } = createLoader();
7272
const { parseApiDocs, parseJsSources } = createParser();
7373

74-
const apiDocFiles = loadFiles(input);
74+
const apiDocFiles = loadMarkdownFiles(input);
7575

7676
const parsedApiDocs = await parseApiDocs(apiDocFiles);
7777

78-
const sourceFiles = loadJsFiles(
79-
parsedApiDocs
80-
.map(({ source_link_local }) => source_link_local)
81-
.filter(path => path?.endsWith('.js'))
82-
);
78+
const sourceFiles = loadJsFiles(input);
8379

8480
const parsedJsFiles = await parseJsSources(sourceFiles);
8581

package-lock.json

Lines changed: 41 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
"dependencies": {
3232
"acorn": "^8.14.0",
3333
"commander": "^12.1.0",
34+
"estree-util-visit": "^2.0.0",
35+
"gitconfiglocal": "^2.1.0",
3436
"github-slugger": "^2.0.0",
3537
"glob": "^11.0.0",
3638
"hast-util-to-string": "^3.0.1",
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
'use strict';
2+
3+
// Checks if a string is a valid name for a constructor in JavaScript
4+
export const CONSTRUCTOR_EXPRESSION = /^[A-Z]/;

src/generators/api-links/index.mjs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
import { basename, dirname, join } from 'node:path';
44
import { writeFile } from 'node:fs/promises';
5-
import { getGitRepository, getGitTag } from '../../utils/git.mjs';
5+
import {
6+
getBaseGitHubUrl,
7+
getCurrentGitHash,
8+
} from './utils/getBaseGitHubUrl.mjs';
69
import { extractExports } from './utils/extractExports.mjs';
710
import { findDefinitions } from './utils/findDefinitions.mjs';
11+
import { checkIndirectReferences } from './utils/checkIndirectReferences.mjs';
812

913
/**
1014
* This generator is responsible for mapping publicly accessible functions in
@@ -36,7 +40,7 @@ export default {
3640
*/
3741
async generate(input, { output }) {
3842
/**
39-
* @type {Record<string, string>}
43+
* @type Record<string, string>
4044
*/
4145
const definitions = {};
4246

@@ -45,14 +49,25 @@ export default {
4549
*/
4650
let baseGithubLink;
4751

52+
if (input.length > 0) {
53+
const repositoryDirectory = dirname(input[0].path);
54+
55+
const repository = getBaseGitHubUrl(repositoryDirectory);
56+
57+
const tag = getCurrentGitHash(repositoryDirectory);
58+
59+
baseGithubLink = `${repository}/blob/${tag}`;
60+
}
61+
4862
input.forEach(program => {
4963
/**
5064
* Mapping of definitions to their line number
5165
* @type {Record<string, number>}
52-
* @example { 'someclass.foo', 10 }
66+
* @example { 'someclass.foo': 10 }
5367
*/
5468
const nameToLineNumberMap = {};
5569

70+
// `http.js` -> `http`
5671
const programBasename = basename(program.path, '.js');
5772

5873
const exports = extractExports(
@@ -63,19 +78,12 @@ export default {
6378

6479
findDefinitions(program, programBasename, nameToLineNumberMap, exports);
6580

66-
if (!baseGithubLink) {
67-
const directory = dirname(program.path);
68-
69-
const repository = getGitRepository(directory);
70-
71-
const tag = getGitTag(directory);
72-
73-
baseGithubLink = `https://github.com/${repository}/blob/${tag}`;
74-
}
81+
checkIndirectReferences(program, exports, nameToLineNumberMap);
7582

7683
const githubLink =
7784
`${baseGithubLink}/lib/${programBasename}.js`.replaceAll('\\', '/');
7885

86+
// Add the exports we found in this program to our output
7987
Object.keys(nameToLineNumberMap).forEach(key => {
8088
const lineNumber = nameToLineNumberMap[key];
8189

src/generators/api-links/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export interface ProgramExports {
22
ctors: Array<string>;
33
identifiers: Array<string>;
4+
indirects: Record<string, string>;
45
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { visit } from 'estree-util-visit';
2+
3+
/**
4+
*
5+
* @param program
6+
* @param {import('../types.d.ts').ProgramExports} exports
7+
* @param {Record<string, number>} nameToLineNumberMap
8+
*/
9+
export function checkIndirectReferences(program, exports, nameToLineNumberMap) {
10+
if (Object.keys(exports.indirects).length === 0) {
11+
return;
12+
}
13+
14+
visit(program, node => {
15+
if (!node.loc || node.type !== 'FunctionDeclaration') {
16+
return;
17+
}
18+
19+
const name = node.id.name;
20+
21+
if (name in exports.indirects) {
22+
nameToLineNumberMap[exports.indirects[name]] = node.loc.start.line;
23+
}
24+
});
25+
}

0 commit comments

Comments
 (0)