Skip to content

Commit

Permalink
fix: visitor overlapping types (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
maastrich authored Jun 19, 2024
1 parent 91b1947 commit 604e300
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 11 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swc-walk",
"version": "1.0.0-rc.0",
"version": "1.0.0-rc.1",
"description": "Walk an AST from SWC and visit each node type.",
"main": "dist/esm/walk.js",
"type": "module",
Expand Down
19 changes: 13 additions & 6 deletions src/baseVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Node, Callback, RecursiveVisitors } from './types.js'

const ignore = <S>(_n: Node, _st: S, _cb: Callback<S>) => {}

export class BaseVisitor<T> implements Required<RecursiveVisitors<T>> {
export class BaseVisitor implements Required<RecursiveVisitors<unknown>> {
ArrayExpression<S>(n: swc.ArrayExpression, st: S, cb: Callback<S>) {
for (const el of n.elements) {
if (el) {
Expand Down Expand Up @@ -348,9 +348,8 @@ export class BaseVisitor<T> implements Required<RecursiveVisitors<T>> {
cb(n.body, st)
}
}
Identifier(n: swc.Identifier, st: T, cb: Callback<T>) {
Identifier<S>(n: swc.Identifier | swc.BindingIdentifier, st: S, cb: Callback<S>) {
if ('typeAnnotation' in n && n.typeAnnotation) {
// @ts-expect-error -- typeAnnotation is not typed in Identifier
cb(n.typeAnnotation, st)
}
}
Expand Down Expand Up @@ -647,13 +646,21 @@ export class BaseVisitor<T> implements Required<RecursiveVisitors<T>> {
}
}
TemplateElement = ignore
TemplateLiteral<S>(n: swc.TemplateLiteral, st: S, cb: Callback<S>) {
TemplateLiteral<S>(n: swc.TemplateLiteral | swc.TsTemplateLiteralType, st: S, cb: Callback<S>) {
for (const quasis of n.quasis) {
cb(quasis, st)
}

for (const expressions of n.expressions) {
cb(expressions, st)
if ('expressions' in n) {
for (const expressions of n.expressions) {
cb(expressions, st)
}
}

if ('types' in n) {
for (const types of n.types) {
cb(types, st)
}
}
}
ThisExpression = ignore
Expand Down
2 changes: 1 addition & 1 deletion src/walk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { SimpleVisitors } from './types.js'
export function simple<T = unknown>(
ast: Node,
visitors: SimpleVisitors<T>,
baseVisitor = new BaseVisitor<T>(),
baseVisitor = new BaseVisitor(),
state?: T,
) {
// @ts-expect-error (acorn-walk ast nodes have start/end instead of span)
Expand Down
1 change: 1 addition & 0 deletions test/baseVisitors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ const tests: Array<Test> = [
{ type: 'TaggedTemplateExpression', code: 'foo<T>`bar ${baz}`' },
{ type: 'TemplateElement', code: 'foo`bar`' },
{ type: 'TemplateLiteral', code: 'foo`bar`' },
{ type: 'TemplateLiteral', code: 'type foo = `${bar}`' },
{ type: 'ThisExpression', code: 'this' },
{ type: 'ThrowStatement', code: 'throw 1' },
{ type: 'TryStatement', code: 'try {} catch(e) {}' },
Expand Down
2 changes: 1 addition & 1 deletion test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { simple } from '../src/walk.js'
import { BaseVisitor } from '../src/baseVisitor.js'

export type Test = {
type: keyof BaseVisitor<unknown>
type: keyof BaseVisitor
skip?: string
code: string
times?: number
Expand Down

0 comments on commit 604e300

Please sign in to comment.