eslint-plugin-perfectionist
Version:
ESLint plugin for sorting various data such as objects, imports, types, enums, JSX props, etc.
26 lines (25 loc) • 918 B
JavaScript
import { UnreachableCaseError } from '../../utils/unreachable-case-error.js'
import { AST_NODE_TYPES } from '@typescript-eslint/utils'
/**
* Extracts the name of an import attribute for sorting purposes.
*
* For identifier keys, returns the identifier name. For literal keys, returns
* the string value. Falls back to source code text if needed.
*
* @param attribute - The import attribute AST node.
* @param sourceCode - The ESLint source code object.
* @returns The attribute name to use for sorting.
*/
function computeNodeName(attribute, sourceCode) {
let { key } = attribute
switch (key.type) {
case AST_NODE_TYPES.Identifier:
return key.name
case AST_NODE_TYPES.Literal:
return key.value?.toString() ?? sourceCode.getText(attribute)
/* v8 ignore next 2 -- @preserve Exhaustive guard. */
default:
throw new UnreachableCaseError(key)
}
}
export { computeNodeName }