UNPKG

1.16 kBPlain TextView Raw
1import * as ts from 'typescript';
2import * as implicitRoles from './implicitRoles';
3import { isJsxElement, isJsxSelfClosingElement, isJsxOpeningElement } from './TypeGuard';
4
5/**
6 * @returns { string } the implicit role or undefined if no corresponding role for a
7 * JsxElement, JsxSelfClosingElement or JsxOpeningElement.
8 * The implementation is inspired and re-implemented from
9 * https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/src/util/getImplicitRole.js
10 * A reference about implicit role: https://www.w3.org/TR/html-aria/#sec-strong-native-semantics.
11 * A reference about no corresponding role: https://www.w3.org/TR/html-aria/#dfn-no-corresponding-role.
12 */
13export function getImplicitRole(node: ts.Node): string {
14 let tagName: string;
15
16 if (isJsxElement(node)) {
17 tagName = node.openingElement.tagName.getText();
18 } else if (isJsxSelfClosingElement(node)) {
19 tagName = node.tagName.getText();
20 } else if (isJsxOpeningElement(node)) {
21 tagName = node.tagName.getText();
22 } else {
23 tagName = undefined;
24 }
25
26 return tagName && implicitRoles[tagName] && implicitRoles[tagName](node);
27}