Skip to content

Commit 6aec954

Browse files
committed
fix: don't globSync redundantly
1 parent 50f6fbf commit 6aec954

File tree

7 files changed

+65
-165
lines changed

7 files changed

+65
-165
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)))
41+
);
5642
},
5743

5844
/**

src/generators/ast/index.mjs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
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';
88
import { getRemark } from '../../utils/remark.mjs';
99

10-
const { loadFiles } = createLoader();
11-
1210
const remarkProcessor = getRemark();
1311

1412
/**
@@ -38,20 +36,16 @@ export default {
3836
async processChunk(inputSlice, itemIndices) {
3937
const filePaths = itemIndices.map(idx => inputSlice[idx]);
4038

41-
const vfilesPromises = loadFiles(filePaths);
42-
43-
const results = [];
44-
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-
}
39+
return Promise.all(
40+
filePaths.map(async path => {
41+
const vfile = await read(path);
5342

54-
return results;
43+
return {
44+
tree: remarkProcessor.parse(vfile),
45+
file: { stem: vfile.stem, basename: vfile.basename },
46+
};
47+
})
48+
);
5549
},
5650

5751
/**

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: 22 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,27 @@
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 | Promise<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+
// We allow the API doc VFile to be a Promise of a VFile also,
13+
// hence we want to ensure that it first resolves before we pass it to the parser
14+
const resolvedSourceFile = await Promise.resolve(sourceFile);
15+
16+
if (typeof resolvedSourceFile.value !== 'string') {
17+
throw new TypeError(
18+
`expected resolvedSourceFile.value to be string but got ${typeof resolvedSourceFile.value}`
19+
);
20+
}
21+
22+
const res = acorn.parse(resolvedSourceFile.value, {
23+
allowReturnOutsideFunction: true,
24+
ecmaVersion: 'latest',
25+
locations: true,
26+
});
27+
28+
return { ...res, path: resolvedSourceFile.path };
5029
};
51-
52-
export default createParser;

0 commit comments

Comments
 (0)