UNPKG

eslint-plugin-perfectionist

Version:

ESLint plugin for sorting various data such as objects, imports, types, enums, JSX props, etc.

44 lines (43 loc) 1.62 kB
import { passesAllNamesMatchPatternFilter } from '../../utils/context-matching/passes-all-names-match-pattern-filter.js' import { passesAstSelectorFilter } from '../../utils/context-matching/passes-ast-selector-filter.js' import { computeNodeName } from './compute-node-name.js' import { AST_NODE_TYPES } from '@typescript-eslint/utils' /** * Computes the matched context options for a given named import node. * * @param params - Parameters. * @param params.node - The named import node to compute the context options * for. * @param params.matchedAstSelectors - The matched AST selectors for a named * export node. * @param params.context - The rule context. * @returns The matched context options or undefined if none match. */ function computeMatchedContextOptions({ matchedAstSelectors, context, node }) { return context.options.find(options => isContextOptionMatching({ matchedAstSelectors, options, node, }), ) } function isContextOptionMatching({ matchedAstSelectors, options, node }) { if (!options.useConfigurationIf) { return true } let nodeNames = node.specifiers .filter(specifier => specifier.type === AST_NODE_TYPES.ImportSpecifier) .map(specifier => computeNodeName(specifier, !!options.ignoreAlias)) return ( passesAllNamesMatchPatternFilter({ allNamesMatchPattern: options.useConfigurationIf.allNamesMatchPattern, nodeNames, }) && passesAstSelectorFilter({ matchesAstSelector: options.useConfigurationIf.matchesAstSelector, matchedAstSelectors, }) ) } export { computeMatchedContextOptions }