@@ -16,7 +16,7 @@ import { createNodeSlugger } from './utils/slugger.mjs';
1616/**
1717 * Creates an API doc parser for a given Markdown API doc file
1818 */
19- const createParser = ( ) => {
19+ export const createMarkdownParser = ( ) => {
2020 // Creates an instance of the Remark processor with GFM support
2121 // which is used for stringifying the AST tree back to Markdown
2222 const remarkProcessor = getRemark ( ) ;
@@ -183,38 +183,40 @@ const createParser = () => {
183183 return resolvedApiDocEntries . flat ( ) ;
184184 } ;
185185
186+ return { parseApiDocs, parseApiDoc } ;
187+ } ;
188+
189+ /**
190+ * Creates a Javascript source parser for a given source file
191+ */
192+ export const createJsParser = ( ) => {
186193 /**
187194 * Parses a given JavaScript file into an ESTree AST representation of it
188195 *
189- * @param {import('vfile').VFile | Promise<import('vfile').VFile> } apiDoc
196+ * @param {import('vfile').VFile | Promise<import('vfile').VFile> } sourceFile
190197 * @returns {Promise<JsProgram> }
191198 */
192- const parseJsSource = async apiDoc => {
199+ const parseJsSource = async sourceFile => {
193200 // We allow the API doc VFile to be a Promise of a VFile also,
194201 // hence we want to ensure that it first resolves before we pass it to the parser
195- const resolvedApiDoc = await Promise . resolve ( apiDoc ) ;
202+ const resolvedSourceFile = await Promise . resolve ( sourceFile ) ;
196203
197- if ( typeof resolvedApiDoc . value !== 'string' ) {
204+ if ( typeof resolvedSourceFile . value !== 'string' ) {
198205 throw new TypeError (
199- `expected resolvedApiDoc .value to be string but got ${ typeof resolvedApiDoc . value } `
206+ `expected resolvedSourceFile .value to be string but got ${ typeof resolvedSourceFile . value } `
200207 ) ;
201208 }
202209
203- try {
204- const res = acorn . parse ( resolvedApiDoc . value , {
205- allowReturnOutsideFunction : true ,
206- ecmaVersion : 'latest' ,
207- locations : true ,
208- } ) ;
209-
210- return {
211- ...res ,
212- path : resolvedApiDoc . path ,
213- } ;
214- } catch ( err ) {
215- console . log ( `error parsing ${ resolvedApiDoc . basename } ` ) ;
216- throw err ;
217- }
210+ const res = acorn . parse ( resolvedSourceFile . value , {
211+ allowReturnOutsideFunction : true ,
212+ ecmaVersion : 'latest' ,
213+ locations : true ,
214+ } ) ;
215+
216+ return {
217+ ...res ,
218+ path : resolvedSourceFile . path ,
219+ } ;
218220 } ;
219221
220222 /**
@@ -231,7 +233,5 @@ const createParser = () => {
231233 return resolvedApiDocEntries ;
232234 } ;
233235
234- return { parseApiDocs , parseApiDoc , parseJsSources, parseJsSource } ;
236+ return { parseJsSource , parseJsSources } ;
235237} ;
236-
237- export default createParser ;
0 commit comments