Skip to content

Instantly share code, notes, and snippets.

@benfoxall
Last active January 29, 2022 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benfoxall/cedba7e3d4b663375748be2f4e968559 to your computer and use it in GitHub Desktop.
Save benfoxall/cedba7e3d4b663375748be2f4e968559 to your computer and use it in GitHub Desktop.
Extract functions from js source code
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