diff --git a/packages/core/babel/core/unwrap-node.ts b/packages/core/babel/core/unwrap-node.ts index 8446952..827edb4 100644 --- a/packages/core/babel/core/unwrap-node.ts +++ b/packages/core/babel/core/unwrap-node.ts @@ -1,3 +1,4 @@ +import type { NodePath } from '@babel/traverse'; import type * as t from '@babel/types'; type TypeFilter = (node: t.Node) => node is K; @@ -28,6 +29,13 @@ function isNestedExpression(node: t.Node): node is NestedExpression { } } +export function isPathValid( + path: unknown, + key: TypeFilter, +): path is NodePath { + return key((path as NodePath).node); +} + export default function unwrapNode boolean>( node: t.Node, key: K, @@ -40,3 +48,22 @@ export default function unwrapNode boolean>( } return undefined; } + +export function getProperParentPath boolean>( + path: NodePath, + key: K, +): NodePath> | undefined { + let parent = path.parentPath; + + while (parent) { + if (isNestedExpression(parent.node)) { + parent = parent.parentPath; + } else if (key(parent.node)) { + return parent as NodePath>; + } else { + return undefined; + } + } + + return undefined; +}