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.
npm run import -- "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
/**
* 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;
async function getClassParams(code, classname)
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.
code
: The C# code snippet to be parsed.classname
: An optional parameter specifying a specific class to focus on. If omitted, the function will extract method parameters from all classes in the code.An array of arrays, where each inner array contains the method name and its corresponding parameters.
select
function from the antlr
library.classname
is provided. If so, it uses the select
function to extract method parameters from the specified class.classname
is provided, it uses the select
function to extract method parameters from all classes in the code.