Skip to content

Commit a05fb38

Browse files
committed
chore: more self review
1 parent f34eb76 commit a05fb38

File tree

3 files changed

+52
-39
lines changed

3 files changed

+52
-39
lines changed

src/generators/ast-js/index.mjs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import { extname } from 'node:path';
2+
3+
import { globSync } from 'glob';
4+
15
import createJsLoader from '../../loaders/javascript.mjs';
26
import createJsParser from '../../parsers/javascript.mjs';
37

@@ -49,10 +53,10 @@ export default {
4953
* @param {Input} _
5054
* @param {Partial<GeneratorOptions>} options
5155
*/
52-
async generate(_, { input, worker }) {
53-
// Load all of the Javascript sources into memory
54-
const sourceFiles =
55-
input?.filter(filename => filename.endsWith('.js')) ?? [];
56+
async generate(_, { input = [], worker }) {
57+
const sourceFiles = globSync(input).filter(
58+
filePath => extname(filePath) === '.js'
59+
);
5660

5761
// Parse the Javascript sources into ASTs in parallel using worker threads
5862
return worker.map(sourceFiles, _, { input: sourceFiles });

src/generators/jsx-ast/index.mjs

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,52 @@ import buildContent from './utils/buildContent.mjs';
44
import { groupNodesByModule } from '../../utils/generators.mjs';
55
import { getRemarkRecma } from '../../utils/remark.mjs';
66

7-
/**
8-
* Generator for converting MDAST to JSX AST.
9-
*
10-
* @typedef {Array<ApiDocMetadataEntry>} Input
11-
* @type {GeneratorMetadata<Input, string>}
12-
*/
13-
147
/**
158
* Sorts entries by OVERRIDDEN_POSITIONS and then heading name.
169
* @param {Array<ApiDocMetadataEntry>} entries
1710
*/
1811
const getSortedHeadNodes = entries => {
19-
return entries
20-
.filter(node => node.heading.depth === 1)
21-
.sort((a, b) => {
22-
const ai = OVERRIDDEN_POSITIONS.indexOf(a.api);
23-
const bi = OVERRIDDEN_POSITIONS.indexOf(b.api);
24-
25-
if (ai !== -1 && bi !== -1) {
26-
return ai - bi;
27-
}
28-
29-
if (ai !== -1) {
30-
return -1;
31-
}
32-
33-
if (bi !== -1) {
34-
return 1;
35-
}
36-
37-
return a.heading.data.name.localeCompare(b.heading.data.name);
38-
});
12+
/**
13+
* Sorts entries by OVERRIDDEN_POSITIONS and then heading name.
14+
* @param {ApiDocMetadataEntry} a
15+
* @param {ApiDocMetadataEntry} b
16+
* @returns {number}
17+
*/
18+
const headingSortFn = (a, b) => {
19+
const ai = OVERRIDDEN_POSITIONS.indexOf(a.api);
20+
const bi = OVERRIDDEN_POSITIONS.indexOf(b.api);
21+
22+
if (ai !== -1 && bi !== -1) {
23+
return ai - bi;
24+
}
25+
26+
if (ai !== -1) {
27+
return -1;
28+
}
29+
30+
if (bi !== -1) {
31+
return 1;
32+
}
33+
34+
return a.heading.data.name.localeCompare(b.heading.data.name);
35+
};
36+
37+
return entries.filter(node => node.heading.depth === 1).sort(headingSortFn);
3938
};
4039

40+
/**
41+
* Generator for converting MDAST to JSX AST.
42+
*
43+
* @typedef {Array<ApiDocMetadataEntry>} Input
44+
* @type {GeneratorMetadata<Input, string>}
45+
*/
4146
export default {
4247
name: 'jsx-ast',
48+
4349
version: '1.0.0',
50+
4451
description: 'Generates JSX AST from the input MDAST',
52+
4553
dependsOn: 'metadata',
4654

4755
/**
@@ -71,14 +79,14 @@ export default {
7179
docPages
7280
);
7381

74-
results.push(
75-
await buildContent(
76-
groupedModules.get(entry.api),
77-
entry,
78-
sideBarProps,
79-
remarkRecma
80-
)
82+
const content = await buildContent(
83+
groupedModules.get(entry.api),
84+
entry,
85+
sideBarProps,
86+
remarkRecma
8187
);
88+
89+
results.push(content);
8290
}
8391

8492
return results;
@@ -92,7 +100,7 @@ export default {
92100
* @returns {Promise<Array<string>>} Array of generated content
93101
*/
94102
async generate(entries, { index, releases, version, worker }) {
95-
const headNodes = getSortedHeadNodes(entries);
103+
const headNodes = entries.filter(node => node.heading.depth === 1);
96104

97105
return worker.map(headNodes, entries, { index, releases, version });
98106
},

src/threading/chunk-worker.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { parentPort, workerData } from 'node:worker_threads';
33
import { allGenerators } from '../generators/index.mjs';
44

55
const { generatorName, fullInput, itemIndices, options } = workerData;
6+
67
const generator = allGenerators[generatorName];
78

89
// Generators must implement processChunk for item-level parallelization

0 commit comments

Comments
 (0)