Created
March 25, 2023 01:53
-
-
Save rmbrntt/c8e4f501d8c16eb6999476ea2b72166a to your computer and use it in GitHub Desktop.
JavaScript to extract functions and classes.
This file contains 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
const fs = require('fs'); | |
const path = require('path'); | |
const glob = require('glob'); | |
const { parse } = require('@babel/parser'); | |
function extractFunctions(node, filepath, functions) { | |
if ( | |
(node.type === 'FunctionDeclaration' || | |
(node.type === 'VariableDeclarator' && node.init && node.init.type === 'FunctionExpression') || | |
(node.type === 'VariableDeclarator' && node.init && node.init.type === 'ArrowFunctionExpression') || | |
(node.type === 'ClassMethod') || | |
(node.type === 'ObjectMethod') || | |
(node.type === 'ObjectProperty' && node.value.type === 'FunctionExpression') || | |
(node.type === 'ObjectProperty' && node.value.type === 'ArrowFunctionExpression')) | |
) { | |
const functionName = node.id ? node.id.name : node.key ? node.key.name : null; | |
if (functionName) { | |
functions.push({ code: '', functionName, filepath }); | |
} | |
} else if (node.type === 'ClassDeclaration' && node.superClass && node.superClass.name === 'Component') { | |
const componentName = node.id ? node.id.name : null; | |
if (componentName) { | |
functions.push({ code: '', functionName: componentName, filepath }); | |
} | |
} | |
if (node.body) { | |
if (Array.isArray(node.body)) { | |
node.body.forEach((childNode) => extractFunctions(childNode, filepath, functions)); | |
} else { | |
extractFunctions(node.body, filepath, functions); | |
} | |
} | |
} | |
// function extractFunctions(node, filepath, functions) { | |
// if ( | |
// (node.type === 'FunctionDeclaration' || | |
// (node.type === 'VariableDeclarator' && node.init && node.init.type === 'FunctionExpression') || | |
// (node.type === 'VariableDeclarator' && node.init && node.init.type === 'ArrowFunctionExpression') || | |
// (node.type === 'ClassMethod') || | |
// (node.type === 'ObjectMethod') || | |
// (node.type === 'ObjectProperty' && node.value.type === 'FunctionExpression') || | |
// (node.type === 'ObjectProperty' && node.value.type === 'ArrowFunctionExpression')) | |
// ) { | |
// const functionName = node.id ? node.id.name : node.key ? node.key.name : null; | |
// if (functionName) { | |
// functions.push({ code: '', functionName, filepath }); | |
// } | |
// } | |
// if (node.body) { | |
// if (Array.isArray(node.body)) { | |
// node.body.forEach((childNode) => extractFunctions(childNode, filepath, functions)); | |
// } else { | |
// extractFunctions(node.body, filepath, functions); | |
// } | |
// } | |
// } | |
function getFunctions(filepath) { | |
const code = fs.readFileSync(filepath, 'utf8'); | |
const ast = parse(code, { sourceType: 'module' }); | |
const functions = []; | |
extractFunctions(ast.program, filepath, functions); | |
return functions; | |
} | |
// function getFunctionName(code) { | |
// const regex = /function\s+(\w+)\s*\(/; | |
// const match = code.match(regex); | |
// return match ? match[1] : null; | |
// } | |
// function getUntilNoSpace(allLines, i) { | |
// const ret = [allLines[i]]; | |
// for (let j = i + 1; j < i + 10000; j++) { | |
// if (j < allLines.length) { | |
// if (allLines[j] === '' || allLines[j][0].match(/[ \t\)]/)) { | |
// ret.push(allLines[j]); | |
// } else { | |
// break; | |
// } | |
// } | |
// } | |
// return ret.join('\n'); | |
// } | |
// function getFunctions(filepath) { | |
// const wholeCode = fs.readFileSync(filepath, 'utf8').replace(/\r/g, '\n'); | |
// const allLines = wholeCode.split('\n'); | |
// const functions = []; | |
// for (let i = 0; i < allLines.length; i++) { | |
// const line = allLines[i]; | |
// if (line.startsWith('function ')) { | |
// const code = getUntilNoSpace(allLines, i); | |
// const functionName = getFunctionName(code); | |
// if (functionName) { | |
// functions.push({ code, functionName, filepath }); | |
// } | |
// } | |
// } | |
// return functions; | |
// } | |
// get user root directory | |
const rootDir = require('os').homedir(); | |
// path to code repository directory | |
const codeRoot = path.join(rootDir, 'your-js-code-repo'); | |
glob(path.join(codeRoot, '**/*.js'), (err, codeFiles) => { | |
if (err) { | |
console.error('Error:', err); | |
return; | |
} | |
console.log('Total number of js files:', codeFiles.length); | |
if (codeFiles.length === 0) { | |
console.log('Double check that you have downloaded the your-js-code-repo and set the codeRoot variable correctly.'); | |
} | |
const allFuncs = []; | |
for (const codeFile of codeFiles) { | |
const funcs = getFunctions(codeFile); | |
allFuncs.push(...funcs); | |
} | |
console.log('Total number of functions extracted:', allFuncs.length); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment