Last active
January 29, 2022 11:08
-
-
Save benfoxall/cedba7e3d4b663375748be2f4e968559 to your computer and use it in GitHub Desktop.
Extract functions from js source code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { opendir, readFile } from 'fs/promises'; | |
import { join } from 'path' | |
import * as parser from "@babel/parser"; | |
import _traverse from "@babel/traverse"; | |
const traverse = _traverse.default; | |
for await (const file of allFiles('javascript')) { | |
if (file.endsWith('.js')) { | |
const buffer = await readFile(file) | |
const code = buffer.toString() | |
for (const fn of extractFunctions(code)) { | |
// Print function to each line | |
console.log(JSON.stringify(fn)) | |
// You could upload to a db here instead | |
} | |
} | |
} | |
/** recursively get all files */ | |
async function* allFiles(directory) { | |
for await (const file of await opendir(directory)) { | |
const path = join(directory, file.name) | |
if (file.isFile()) { | |
yield path | |
} | |
if (file.isDirectory()) { | |
for await (const entry of allFiles(path)) { | |
yield entry; | |
} | |
} | |
} | |
} | |
/** Find function definitions from a js file string */ | |
function extractFunctions(code) { | |
try { | |
const fns = [] | |
const ast = parser.parse(code, { sourceType: "module" }) | |
traverse(ast, { | |
enter({ node: { type, start, end } }) { | |
if (type === 'FunctionDeclaration') { | |
fns.push(code.slice(start, end)) | |
} | |
} | |
}) | |
return fns; | |
} catch { | |
return [] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment