csharp | list csharp classes | | Search

The getClassParams function retrieves method parameters from a given C# code snippet using the antlr library's select function, and returns the results as an array of arrays containing method names and parameters. The function can optionally focus on a specific class, or extract parameters from all classes in the code if no class name is provided.

Run example

npm run import -- "get csharp params"

get csharp params




async function getClassParams(code, classname) {
  const selectCode = await importer.import("select antlr tree")

  let result
  if(classname) {
    result = await selectCode(['//class_definition[./identifier[contains(@strdata, "' + classname + '")]]//method_declaration', {
      start: './@start',
      stop: './@stop',
      name: '(./method_member_name/@strdata)[1]',
      params: ['.//fixed_parameter/@strdata']
    }], code, 'csharp')
      
  } else {

    result = await selectCode(['//method_declaration', {
      start: './@start',
      stop: './@stop',
      name: '(./method_member_name/@strdata)[1]',
      params: ['.//fixed_parameter/@strdata']
    }], code, 'csharp')
  }

  //result.sort((a, b) => (b.stop - b.start) - (a.stop - a.start))
  return result.map(r => [r.name].concat(r.params))
}

module.exports = getClassParams

What the code could have been:

/**
 * Extracts class method parameters from a given C# code snippet.
 * 
 * @param {string} code - The C# code snippet to extract parameters from.
 * @param {string} [classname] - The name of the class to extract parameters for (optional).
 * @returns {Promise<string[][]>} A promise resolving to a 2D array of method parameters.
 */
async function getClassParams(code, classname) {
  // Import the select antlr tree function and await its resolution
  const { selectCode } = await importer.import('select antlr tree');

  // Construct the XPath query based on the provided classname
  const query = classname
   ? `//class_definition[./identifier[contains(@strdata, '${classname}')]]//method_declaration`
    : '//method_declaration';

  // Define the path mappings for the XPath query
  const pathMappings = {
    start: './@start',
    stop: './@stop',
    name: '(./method_member_name/@strdata)[1]',
    params: ['.//fixed_parameter/@strdata'],
  };

  // Execute the XPath query and await its resolution
  const result = await selectCode([query, pathMappings], code, 'csharp');

  // Return the extracted method parameters as a 2D array
  return result.map(r => [r.name,...r.params.split(', ')]);
}

module.exports = getClassParams;

getClassParams Function

Function Signature

async function getClassParams(code, classname)

Description

The getClassParams function retrieves method parameters from a given C# code snippet. It utilizes the select function from the antlr library to parse the code and extract the required information.

Parameters

Return Value

An array of arrays, where each inner array contains the method name and its corresponding parameters.

Implementation

  1. The function imports the select function from the antlr library.
  2. It checks if a classname is provided. If so, it uses the select function to extract method parameters from the specified class.
  3. If no classname is provided, it uses the select function to extract method parameters from all classes in the code.
  4. The extracted data is then mapped to an array of arrays, with each inner array containing the method name and its parameters.
  5. The function returns the resulting array.