forked from jsx-eslint/eslint-plugin-jsx-a11y
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetElementType.js
28 lines (21 loc) · 890 Bytes
/
getElementType.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* @flow
*/
import type { JSXOpeningElement } from 'ast-types-flow';
import has from 'has';
import { elementType, getProp, getLiteralPropValue } from 'jsx-ast-utils';
import type { ESLintContext } from '../../flow/eslint';
const getElementType = (context: ESLintContext): ((node: JSXOpeningElement) => string) => {
const { settings } = context;
const polymorphicPropName = settings['jsx-a11y']?.polymorphicPropName;
const componentMap = settings['jsx-a11y']?.components;
return (node: JSXOpeningElement): string => {
const polymorphicProp = polymorphicPropName ? getLiteralPropValue(getProp(node.attributes, polymorphicPropName)) : undefined;
const rawType = polymorphicProp ?? elementType(node);
if (!componentMap) {
return rawType;
}
return has(componentMap, rawType) ? componentMap[rawType] : rawType;
};
};
export default getElementType;