Skip to content

Commit a087de3

Browse files
authored
Skip visiting non-node children (#443)
1 parent 1abb2ef commit a087de3

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/utils.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ type Visitor<T, Meta extends Record<string, unknown>> = (
3030

3131
type Visitors<T, Meta extends Record<string, unknown>> = Record<string, Visitor<T, Meta>>
3232

33+
function isNodeLike(value: any): value is { type: string } {
34+
return typeof value?.type === 'string'
35+
}
36+
3337
// https://lihautan.com/manipulating-ast-with-javascript/
3438
export function visit<T extends {}, Meta extends Record<string, unknown>>(
3539
ast: T,
@@ -51,7 +55,7 @@ export function visit<T extends {}, Meta extends Record<string, unknown>>(
5155
const child = node[keys[i]]
5256
if (Array.isArray(child)) {
5357
for (let j = 0; j < child.length; j++) {
54-
if (child[j] !== null) {
58+
if (isNodeLike(child[j])) {
5559
let newMeta = { ...meta }
5660
let newPath = [
5761
{
@@ -67,7 +71,7 @@ export function visit<T extends {}, Meta extends Record<string, unknown>>(
6771
_visit(child[j], newPath, newMeta)
6872
}
6973
}
70-
} else if (typeof child?.type === 'string') {
74+
} else if (isNodeLike(child)) {
7175
let newMeta = { ...meta }
7276
let newPath = [
7377
{

0 commit comments

Comments
 (0)