The code defines a getParameters function that extracts function parameters from a given code string by using XPath queries to parse an Abstract Syntax Tree (AST). The function is exported as a module and can be used in other parts of the application to retrieve function parameters.
npm run import -- "get parameter names"var importer = require('../Core');
var {getExports, selectAst} = importer.import("select code tree",
"get exports from source")
var EXPORTS = `//AssignmentExpression[.//Identifier[@name=\"exports\"]]//Identifier`
var PARAMETERS = `((//FunctionDeclaration|//ArrowFunctionExpression)[
./Identifier[@parent-attr=\"id\" and contains('EXPORTS', @name)]
]/Identifier[@parent-attr=\"params\"]|(//FunctionDeclaration|//ArrowFunctionExpression)[
./Identifier[@parent-attr=\"id\" and contains('EXPORTS', @name)]
]/*/Identifier[@parent-attr=\"left\"])`
function getParameters(code) {
var fnName = getExports(code);
if(!fnName[0]) {
return [];
}
var params = selectAst([PARAMETERS.replaceAll('EXPORTS', fnName.join(' ')), './@name'], code)
//for (const attr of params[1].attributes) {
// console.log(`${attr.name} -> ${attr.value}\n`)
//}
return [fnName[0], ...params.filter(p => p) /*.filter((p, i, arr) => arr.indexOf(p) === i)*/]
}
module.exports = getParameters;
import { importCore } from '../Core';
import { getExports, selectAst } from importCore(['select code tree', 'get exports from source']);
const EXPORTS_SELECTOR = (fnName) => [
'//FunctionDeclaration|//ArrowFunctionExpression',
`./Identifier[@parent-attr="id" and contains("${fnName.join(' ')}", @name)]`,
`/*/Identifier[@parent-attr="left"]`,
'./@name'
];
function getParameters(code) {
const { exportName } = getExports(code);
if (!exportName[0]) {
return [];
}
const params = selectAst(EXPORTS_SELECTOR(exportName), code);
// Remove duplicates and empty values from the result
const trimmedParams = params.filter((p, i, arr) => arr.indexOf(p) === i && p);
return [exportName[0],...trimmedParams];
}
export default getParameters;var importer = require('../Core');
var {getExports, selectAst} = importer.import([
'select code tree', 'get exports from source'])
require('../Core') imports a module named Core from the parent directory.import method of the importer object is called with two module names: 'select code tree' and 'get exports from source'.getExports and selectAst functions are assigned to variables.var EXPORTS = `//AssignmentExpression[.//Identifier[@name="exports"]]//Identifier`
var PARAMETERS = `((//FunctionDeclaration|//ArrowFunctionExpression)[
./Identifier[@parent-attr="id" and contains('EXPORTS', @name)]
]/Identifier[@parent-attr="params"]|(//FunctionDeclaration|//ArrowFunctionExpression)[
./Identifier[@parent-attr="id" and contains('EXPORTS', @name)]
]/*/Identifier[@parent-attr="left"])`
EXPORTS and PARAMETERS. These queries are used to extract information from an Abstract Syntax Tree (AST).EXPORTS searches for an Identifier node with the name "exports" inside an AssignmentExpression.PARAMETERS searches for Identifier nodes that are children of a FunctionDeclaration or ArrowFunctionExpression, and have a parent node with the attribute "id" containing the value of the EXPORTS query.function getParameters(code) {
var fnName = getExports(code);
if(!fnName[0]) {
return [];
}
var params = selectAst([PARAMETERS.replaceAll('EXPORTS', fnName.join(' ')), './@name'], code)
return [fnName[0],...params.filter(p => p /*.filter((p, i, arr) => arr.indexOf(p) === i)*/)]
}
getParameters function takes a code string as input.getExports function to extract the function name from the code, and returns an empty array if no function name is found.EXPORTS placeholder in the PARAMETERS XPath query with the function name, and calls the selectAst function to extract the function parameters from the code.module.exports = getParameters;
getParameters function is exported as a module, making it available for use in other parts of the application.