Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"rolldown": "^1.0.0-beta.53",
"semver": "^7.7.3",
"shiki": "^3.19.0",
"to-vfile": "^8.0.0",
"unified": "^11.0.5",
"unist-builder": "^4.0.0",
"unist-util-find-after": "^5.0.0",
Expand Down
24 changes: 5 additions & 19 deletions src/generators/ast-js/index.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import { extname } from 'node:path';

import { globSync } from 'glob';
import { read } from 'to-vfile';

import createJsLoader from '../../loaders/javascript.mjs';
import createJsParser from '../../parsers/javascript.mjs';

const { loadFiles } = createJsLoader();

const { parseJsSource } = createJsParser();
import { parseJsSource } from '../../parsers/javascript.mjs';

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

const vfilesPromises = loadFiles(filePaths);

const results = [];

for (const vfilePromise of vfilesPromises) {
const vfile = await vfilePromise;

const parsed = await parseJsSource(vfile);

results.push(parsed);
}

return results;
return Promise.all(
filePaths.map(async path => parseJsSource(await read(path, 'utf-8')))
);
},

/**
Expand Down
28 changes: 13 additions & 15 deletions src/generators/ast/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import { extname } from 'node:path';

import { globSync } from 'glob';
import { read } from 'to-vfile';

import createLoader from '../../loaders/markdown.mjs';
import createQueries from '../../utils/queries/index.mjs';
import { getRemark } from '../../utils/remark.mjs';

const { loadFiles } = createLoader();

const { updateStabilityPrefixToLink } = createQueries();
const remarkProcessor = getRemark();

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

const vfilesPromises = loadFiles(filePaths);

const results = [];
return Promise.all(
filePaths.map(async path => {
const vfile = await read(path, 'utf-8');

for (const vfilePromise of vfilesPromises) {
const vfile = await vfilePromise;

results.push({
tree: remarkProcessor.parse(vfile),
file: { stem: vfile.stem, basename: vfile.basename },
});
}
updateStabilityPrefixToLink(vfile);

return results;
return {
tree: remarkProcessor.parse(vfile),
file: { stem: vfile.stem, basename: vfile.basename },
};
})
);
},

/**
Expand Down
33 changes: 0 additions & 33 deletions src/loaders/javascript.mjs

This file was deleted.

44 changes: 0 additions & 44 deletions src/loaders/markdown.mjs

This file was deleted.

63 changes: 18 additions & 45 deletions src/parsers/javascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,23 @@
import * as acorn from 'acorn';

/**
* Creates a Javascript source parser for a given source file
* Parses a given JavaScript file into an ESTree AST representation of it
*
* @param {import('vfile').VFile} sourceFile
* @returns {Promise<JsProgram>}
*/
const createParser = () => {
/**
* Parses a given JavaScript file into an ESTree AST representation of it
*
* @param {import('vfile').VFile | Promise<import('vfile').VFile>} sourceFile
* @returns {Promise<JsProgram>}
*/
const parseJsSource = async sourceFile => {
// We allow the API doc VFile to be a Promise of a VFile also,
// hence we want to ensure that it first resolves before we pass it to the parser
const resolvedSourceFile = await Promise.resolve(sourceFile);

if (typeof resolvedSourceFile.value !== 'string') {
throw new TypeError(
`expected resolvedSourceFile.value to be string but got ${typeof resolvedSourceFile.value}`
);
}

const res = acorn.parse(resolvedSourceFile.value, {
allowReturnOutsideFunction: true,
ecmaVersion: 'latest',
locations: true,
});

return { ...res, path: resolvedSourceFile.path };
};

/**
* Parses multiple JavaScript files into ESTree ASTs by wrapping parseJsSource
*
* @param {Array<import('vfile').VFile | Promise<import('vfile').VFile>>} apiDocs List of API doc files to be parsed
* @returns {Promise<Array<JsProgram>>}
*/
const parseJsSources = async apiDocs => {
// We do a Promise.all, to ensure that each API doc is resolved asynchronously
// but all need to be resolved first before we return the result to the caller
const resolvedApiDocEntries = await Promise.all(apiDocs.map(parseJsSource));

return resolvedApiDocEntries;
};

return { parseJsSource, parseJsSources };
export const parseJsSource = async sourceFile => {
if (typeof sourceFile.value !== 'string') {
throw new TypeError(
`expected sourceFile.value to be string but got ${typeof sourceFile.value}`
);
}

const res = acorn.parse(sourceFile.value, {
allowReturnOutsideFunction: true,
ecmaVersion: 'latest',
locations: true,
});

return { ...res, path: sourceFile.path };
};

export default createParser;
Loading