Skip to content

Commit

Permalink
feat: extractJSDoc
Browse files Browse the repository at this point in the history
  • Loading branch information
GalacticHypernova authored Jan 24, 2024
1 parent dd4ee1c commit 6ae7ef6
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { relative, resolve } from 'pathe'
import { readFileSync, existsSync } from "node:fs"
const files = new Map<string, string>();

/**
* A utility for extracting the JSDoc annotations from a function in the given module, if it exists.
*/
export function extractJSDoc(modulePath: string, functionName: string) {
try {
const jsDocRE = new RegExp(`(\\/\\*\\*[?;,.:\/@\\-\\s\\w\\{\\}\\[\\]\\(\\)\\<\\>\\"\`\|*]*\\*\\/)(?:\nexport d?e?c?l?a?r?e? (?:function|const) ${functionName})`,'i')
modulePath = resolve(modulePath.slice(6))
if (!files.has(modulePath)) {
if (existsSync(modulePath + "/package.json")) {
const pkg = JSON.parse(readFileSync(modulePath + "/package.json", "utf8"))
files.set(modulePath, readFileSync(modulePath + "/" + pkg.main, "utf8"))
}
else {
for (const ext of [".ts",".js",".mjs",".cjs"]) {
if (existsSync(modulePath + ext)) {
files.set(modulePath, readFileSync(modulePath+ext, "utf8"))
break
}
}
}
}
const jsDoc = files.get(modulePath)?.match(jsDocRE)
return jsDoc;
}
catch (err) {
throw new Error(err)
}
}

0 comments on commit 6ae7ef6

Please sign in to comment.