cat\src\script.js [code] There a certain things to tune from this module. Your task is…
- User
cat\src\script.js
/** * Script Interpreter Module * * This module interprets the parsed JavaScript AST, categorizing the code into * variables, methods, events, and function calls. It reconstructs the JavaScript * code from the AST and organizes it into a structured `script` object. */ const escodegen = require('escodegen'); /** * Helper function to generate code from AST nodes * @param {Object} node - AST node * @returns {string} - Generated JavaScript code as string */ function generateCode(node) { return escodegen.generate(node); } /** * Extract global variable declarations * @param {Array} astBody - Array of AST nodes from the script * @returns {Array} - Array of variable declaration strings */ function extractVariables(astBody) { const variables = []; astBody.forEach(node => { if (node.type === 'VariableDeclaration' && node.kind === 'var') { node.declarations.forEach(decl => { variables.push(generateCode(decl)); }); } else if (node.type === 'VariableDeclaration' && node.kind === 'let') { node.declarations.forEach(decl => { variables.push(generateCode(decl)); }); } else if (node.type === 'VariableDeclaration' && node.kind === 'const') { node.declarations.forEach(decl => { variables.push(generateCode(decl)); }); } }); return variables; } /** * Extract global function declarations and function expressions * @param {Array} astBody - Array of AST nodes from the script * @returns {Array} - Array of method declaration strings */ function extractMethods(astBody) { const methods = []; astBody.forEach(node => { if (node.type === 'FunctionDeclaration') { methods.push(generateCode(node)); } else if ( node.type === 'VariableDeclaration' && node.declarations.some( decl => decl.init && (decl.init.type === 'FunctionExpression' || decl.init.type === 'ArrowFunctionExpression') ) ) { node.declarations.forEach(decl => { if ( decl.init && (decl.init.type === 'FunctionExpression' || decl.init.type === 'ArrowFunctionExpression') ) { methods.push(generateCode(decl)); } }); } }); return methods; } /** * Extract global event listeners or event-related code * @param {Array} astBody - Array of AST nodes from the script * @returns {Array} - Array of event-related strings */ function extractEvents(astBody) { const events = []; astBody.forEach(node => { if ( node.type === 'ExpressionStatement' && node.expression.type === 'CallExpression' ) { const callee = node.expression.callee; // Detect addEventListener or similar event methods if ( (callee.type === 'MemberExpression' && callee.property && ['addEventListener', 'on'].includes(callee.property.name)) || (callee.type === 'Identifier' && ['addEventListener', 'on'].includes(callee.name)) ) { events.push(generateCode(node)); } } }); return events; } /** * Extract global function calls * @param {Array} astBody - Array of AST nodes from the script * @returns {Array} - Array of function call strings */ function extractCalls(astBody) { const calls = []; astBody.forEach(node => { if ( node.type === 'ExpressionStatement' && node.expression.type === 'CallExpression' ) { calls.push(generateCode(node)); } }); return calls; } /** * Main function to interpret the script AST and categorize the code * @param {Array} scriptAST - Parsed JavaScript AST body * @returns {Object} - Object containing categorized script code */ function interpretScript(scriptAST) { const variables = extractVariables(scriptAST); const methods = extractMethods(scriptAST); const events = extractEvents(scriptAST); const calls = extractCalls(scriptAST); return { variables, methods, events, calls }; } module.exports = { interpretScript }; - Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.