Skip to content

Commit 06f981e

Browse files
authored
fix: don't globSync redundantly (#511)
1 parent 50f6fbf commit 06f981e

File tree

7 files changed

+64
-164
lines changed

7 files changed

+64
-164
lines changed

npm-shrinkwrap.json

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"rolldown": "^1.0.0-beta.53",
7676
"semver": "^7.7.3",
7777
"shiki": "^3.19.0",
78+
"to-vfile": "^8.0.0",
7879
"unified": "^11.0.5",
7980
"unist-builder": "^4.0.0",
8081
"unist-util-find-after": "^5.0.0",

src/generators/ast-js/index.mjs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
import { extname } from 'node:path';
22

33
import { globSync } from 'glob';
4+
import { read } from 'to-vfile';
45

5-
import createJsLoader from '../../loaders/javascript.mjs';
6-
import createJsParser from '../../parsers/javascript.mjs';
7-
8-
const { loadFiles } = createJsLoader();
9-
10-
const { parseJsSource } = createJsParser();
6+
import { parseJsSource } from '../../parsers/javascript.mjs';
117

128
/**
139
* This generator parses Javascript sources passed into the generator's input
@@ -40,19 +36,9 @@ export default {
4036
async processChunk(inputSlice, itemIndices) {
4137
const filePaths = itemIndices.map(idx => inputSlice[idx]);
4238

43-
const vfilesPromises = loadFiles(filePaths);
44-
45-
const results = [];
46-
47-
for (const vfilePromise of vfilesPromises) {
48-
const vfile = await vfilePromise;
49-
50-
const parsed = await parseJsSource(vfile);
51-
52-
results.push(parsed);
53-
}
54-
55-
return results;
39+
return Promise.all(
40+
filePaths.map(async path => parseJsSource(await read(path, 'utf-8')))
41+
);
5642
},
5743

5844
/**

src/generators/ast/index.mjs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
import { extname } from 'node:path';
44

55
import { globSync } from 'glob';
6+
import { read } from 'to-vfile';
67

7-
import createLoader from '../../loaders/markdown.mjs';
8+
import createQueries from '../../utils/queries/index.mjs';
89
import { getRemark } from '../../utils/remark.mjs';
910

10-
const { loadFiles } = createLoader();
11-
11+
const { updateStabilityPrefixToLink } = createQueries();
1212
const remarkProcessor = getRemark();
1313

1414
/**
@@ -38,20 +38,18 @@ export default {
3838
async processChunk(inputSlice, itemIndices) {
3939
const filePaths = itemIndices.map(idx => inputSlice[idx]);
4040

41-
const vfilesPromises = loadFiles(filePaths);
42-
43-
const results = [];
41+
return Promise.all(
42+
filePaths.map(async path => {
43+
const vfile = await read(path, 'utf-8');
4444

45-
for (const vfilePromise of vfilesPromises) {
46-
const vfile = await vfilePromise;
47-
48-
results.push({
49-
tree: remarkProcessor.parse(vfile),
50-
file: { stem: vfile.stem, basename: vfile.basename },
51-
});
52-
}
45+
updateStabilityPrefixToLink(vfile);
5346

54-
return results;
47+
return {
48+
tree: remarkProcessor.parse(vfile),
49+
file: { stem: vfile.stem, basename: vfile.basename },
50+
};
51+
})
52+
);
5553
},
5654

5755
/**

src/loaders/javascript.mjs

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/loaders/markdown.mjs

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/parsers/javascript.mjs

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,23 @@
33
import * as acorn from 'acorn';
44

55
/**
6-
* Creates a Javascript source parser for a given source file
6+
* Parses a given JavaScript file into an ESTree AST representation of it
7+
*
8+
* @param {import('vfile').VFile} sourceFile
9+
* @returns {Promise<JsProgram>}
710
*/
8-
const createParser = () => {
9-
/**
10-
* Parses a given JavaScript file into an ESTree AST representation of it
11-
*
12-
* @param {import('vfile').VFile | Promise<import('vfile').VFile>} sourceFile
13-
* @returns {Promise<JsProgram>}
14-
*/
15-
const parseJsSource = async sourceFile => {
16-
// We allow the API doc VFile to be a Promise of a VFile also,
17-
// hence we want to ensure that it first resolves before we pass it to the parser
18-
const resolvedSourceFile = await Promise.resolve(sourceFile);
19-
20-
if (typeof resolvedSourceFile.value !== 'string') {
21-
throw new TypeError(
22-
`expected resolvedSourceFile.value to be string but got ${typeof resolvedSourceFile.value}`
23-
);
24-
}
25-
26-
const res = acorn.parse(resolvedSourceFile.value, {
27-
allowReturnOutsideFunction: true,
28-
ecmaVersion: 'latest',
29-
locations: true,
30-
});
31-
32-
return { ...res, path: resolvedSourceFile.path };
33-
};
34-
35-
/**
36-
* Parses multiple JavaScript files into ESTree ASTs by wrapping parseJsSource
37-
*
38-
* @param {Array<import('vfile').VFile | Promise<import('vfile').VFile>>} apiDocs List of API doc files to be parsed
39-
* @returns {Promise<Array<JsProgram>>}
40-
*/
41-
const parseJsSources = async apiDocs => {
42-
// We do a Promise.all, to ensure that each API doc is resolved asynchronously
43-
// but all need to be resolved first before we return the result to the caller
44-
const resolvedApiDocEntries = await Promise.all(apiDocs.map(parseJsSource));
45-
46-
return resolvedApiDocEntries;
47-
};
48-
49-
return { parseJsSource, parseJsSources };
11+
export const parseJsSource = async sourceFile => {
12+
if (typeof sourceFile.value !== 'string') {
13+
throw new TypeError(
14+
`expected sourceFile.value to be string but got ${typeof sourceFile.value}`
15+
);
16+
}
17+
18+
const res = acorn.parse(sourceFile.value, {
19+
allowReturnOutsideFunction: true,
20+
ecmaVersion: 'latest',
21+
locations: true,
22+
});
23+
24+
return { ...res, path: sourceFile.path };
5025
};
51-
52-
export default createParser;

0 commit comments

Comments
 (0)