syntax | Cell 6 | test parameter names | Search

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.

Run example

npm run import -- "get parameter names"

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;

What the code could have been:

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;

Code Breakdown

Importing Modules

var importer = require('../Core');
var {getExports, selectAst} = importer.import([
   'select code tree', 'get exports from source'])

XPath Queries

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"])`

getParameters Function

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)*/)]
}

Module Exports

module.exports = getParameters;