The getClassNames
function asynchronously extracts class names from C# code, optionally filtered by a specified namespace, and returns a sorted array of class names.
The getClassNames
function takes C# code and an optional namespace as input and returns a sorted array of class names. It uses an ANTLR parser to extract class definitions and filter them based on the provided namespace.
npm run import -- "list csharp classes"
async function getClassNames(code, namespace) {
const selectCode = await importer.import("select antlr tree")
let result
if(namespace) {
result = await selectCode(['//namespace_declaration[contains(./qualified_identifier/@strdata, "' + namespace + '")]//class_definition', {
start: './@start',
stop: './@stop',
name: '(./identifier/@strdata)[1]'
}], code, 'csharp')
} else {
result = await selectCode(['//class_definition', {
start: './@start',
stop: './@stop',
name: '(./identifier/@strdata)[1]'
}], code, 'csharp')
}
result.sort((a, b) => (b.stop - b.start) - (a.stop - a.start))
return result.map(r => r.name)
}
module.exports = getClassNames
/**
* Retrieves class names from a given code snippet, optionally filtered by namespace.
*
* @param {string} code The code snippet to parse.
* @param {string} [namespace] The namespace to filter by (optional).
* @returns {Promise<string[]>} A promise resolving to an array of class names.
*/
export async function getClassNames(code, namespace = null) {
// Import the select function from the antlr-tree package
const { select } = await import('antlr-tree');
// Define the AST node selectors for class definitions
const selectors = {
classDefinition: '//class_definition',
classDefinitionInNamespace: '//namespace_declaration[contains(./qualified_identifier/@strdata, $namespace)]//class_definition',
};
// Define the properties to extract from each node
const properties = {
start: './@start',
stop: './@stop',
name: './identifier[1]/@strdata',
};
// Determine which selector to use based on the namespace filter
const selector = namespace? selectors.classDefinitionInNamespace : selectors.classDefinition;
// Select the class definitions with the specified properties
const results = await select(code, selector, properties);
// Sort the results by stop position in descending order
results.sort((a, b) => b.stop - b.start - (a.stop - a.start));
// Return the class names
return results.map((result) => result.name);
}
getClassNames
Asynchronously extracts class names from C# code using an ANTLR parser.
code
: The C# code to parse.namespace
: An optional namespace to filter class names by.An array of class names sorted by their scope.
The function uses the importer
module to import the select
function from the antlr-tree
library. It then uses this function to extract class definitions from the provided C# code.
If a namespace
is provided, it selects class definitions within a namespace that matches the given namespace. Otherwise, it selects all class definitions.
The extracted class definitions are sorted by their scope and the class names are returned as an array.
const getClassNames = require('./getClassNames');
const code = '...'; // C# code
const result = await getClassNames(code, 'MyNamespace');