eslint-plugin-perfectionist
Version:
ESLint plugin for sorting various data such as objects, imports, types, enums, JSX props, etc.
45 lines (44 loc) • 1.28 kB
JavaScript
/**
* Extracts the name of a decorator from its AST node.
*
* Processes the decorator text to extract just the name portion, removing the
* '@' prefix if present and any parameters or arguments that follow the name.
* This is useful for sorting and matching decorators by their base name.
*
* @example
*
* ```ts
* // Simple decorator
* getDecoratorName({ sourceCode, decorator: @Component });
* // Returns: 'Component'
* ```
*
* @example
*
* ```ts
* // Decorator with parameters
* getDecoratorName({ sourceCode, decorator: @Injectable({ providedIn: 'root' }) });
* // Returns: 'Injectable'
* ```
*
* @example
*
* ```ts
* // Namespaced decorator
* getDecoratorName({ sourceCode, decorator: @angular.Component() });
* // Returns: 'angular.Component'
* ```
*
* @param params - Parameters object.
* @param params.sourceCode - ESLint source code object for text extraction.
* @param params.decorator - Decorator AST node to extract name from.
* @returns The decorator name without '@' prefix and parameters.
*/
function getDecoratorName({ sourceCode, decorator }) {
let fullName = sourceCode.getText(decorator)
if (fullName.startsWith('@')) {
fullName = fullName.slice(1)
}
return fullName.split('(')[0]
}
export { getDecoratorName }