| 1 |
import { FLIPPED_ALIAS_KEYS, ALIAS_KEYS } from "../definitions"; |
| 2 |
import type * as t from ".."; |
| 3 |
|
| 4 |
export default function isType<T extends t.Node["type"]>( |
| 5 |
nodeType: string, |
| 6 |
targetType: T, |
| 7 |
): nodeType is T; |
| 8 |
|
| 9 |
export default function isType( |
| 10 |
nodeType: string | null | undefined, |
| 11 |
targetType: string, |
| 12 |
): boolean; |
| 13 |
|
| 14 |
/** |
| 15 |
* Test if a `nodeType` is a `targetType` or if `targetType` is an alias of `nodeType`. |
| 16 |
*/ |
| 17 |
export default function isType(nodeType: string, targetType: string): boolean { |
| 18 |
if (nodeType === targetType) return true; |
| 19 |
|
| 20 |
// This is a fast-path. If the test above failed, but an alias key is found, then the |
| 21 |
// targetType was a primary node type, so there's no need to check the aliases. |
| 22 |
// @ts-expect-error targetType may not index ALIAS_KEYS |
| 23 |
if (ALIAS_KEYS[targetType]) return false; |
| 24 |
|
| 25 |
const aliases: Array<string> | undefined = FLIPPED_ALIAS_KEYS[targetType]; |
| 26 |
if (aliases) { |
| 27 |
if (aliases[0] === nodeType) return true; |
| 28 |
|
| 29 |
for (const alias of aliases) { |
| 30 |
if (nodeType === alias) return true; |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
return false; |
| 35 |
} |