-
-
Notifications
You must be signed in to change notification settings - Fork 639
/
Copy pathgetElementType.js
38 lines (29 loc) · 1.12 KB
/
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
29
30
31
32
33
34
35
36
37
38
/**
* @flow
*/
import type { JSXOpeningElement } from 'ast-types-flow';
import hasOwn from 'hasown';
import includes from 'array-includes';
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 polymorphicAllowList = settings['jsx-a11y']?.polymorphicAllowList;
const componentMap = settings['jsx-a11y']?.components;
return (node: JSXOpeningElement): string => {
const polymorphicProp = polymorphicPropName ? getLiteralPropValue(getProp(node.attributes, polymorphicPropName)) : undefined;
let rawType = elementType(node);
if (
polymorphicProp
&& (!polymorphicAllowList || includes(polymorphicAllowList, rawType))
) {
rawType = polymorphicProp;
}
if (!componentMap) {
return rawType;
}
return hasOwn(componentMap, rawType) ? componentMap[rawType] : rawType;
};
};
export default getElementType;