From 604e3007ed342c4a3368b75320c116f8dbd4c2ae Mon Sep 17 00:00:00 2001 From: Mathis Pinsault Date: Wed, 19 Jun 2024 18:58:42 +0200 Subject: [PATCH] fix: visitor overlapping types (#8) --- package-lock.json | 4 ++-- package.json | 2 +- src/baseVisitor.ts | 19 +++++++++++++------ src/walk.ts | 2 +- test/baseVisitors.test.ts | 1 + test/utils.ts | 2 +- 6 files changed, 19 insertions(+), 11 deletions(-) diff --git a/package-lock.json b/package-lock.json index d9a5839..557b61f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "swc-walk", - "version": "1.0.0-rc.0", + "version": "1.0.0-rc.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "swc-walk", - "version": "1.0.0-rc.0", + "version": "1.0.0-rc.1", "license": "MIT", "dependencies": { "acorn-walk": "^8.3.2" diff --git a/package.json b/package.json index cfc61fd..26646de 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/baseVisitor.ts b/src/baseVisitor.ts index 2aeaed8..373337f 100644 --- a/src/baseVisitor.ts +++ b/src/baseVisitor.ts @@ -4,7 +4,7 @@ import type { Node, Callback, RecursiveVisitors } from './types.js' const ignore = (_n: Node, _st: S, _cb: Callback) => {} -export class BaseVisitor implements Required> { +export class BaseVisitor implements Required> { ArrayExpression(n: swc.ArrayExpression, st: S, cb: Callback) { for (const el of n.elements) { if (el) { @@ -348,9 +348,8 @@ export class BaseVisitor implements Required> { cb(n.body, st) } } - Identifier(n: swc.Identifier, st: T, cb: Callback) { + Identifier(n: swc.Identifier | swc.BindingIdentifier, st: S, cb: Callback) { if ('typeAnnotation' in n && n.typeAnnotation) { - // @ts-expect-error -- typeAnnotation is not typed in Identifier cb(n.typeAnnotation, st) } } @@ -647,13 +646,21 @@ export class BaseVisitor implements Required> { } } TemplateElement = ignore - TemplateLiteral(n: swc.TemplateLiteral, st: S, cb: Callback) { + TemplateLiteral(n: swc.TemplateLiteral | swc.TsTemplateLiteralType, st: S, cb: Callback) { 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 diff --git a/src/walk.ts b/src/walk.ts index 5c1e74e..07be1da 100644 --- a/src/walk.ts +++ b/src/walk.ts @@ -7,7 +7,7 @@ import type { SimpleVisitors } from './types.js' export function simple( ast: Node, visitors: SimpleVisitors, - baseVisitor = new BaseVisitor(), + baseVisitor = new BaseVisitor(), state?: T, ) { // @ts-expect-error (acorn-walk ast nodes have start/end instead of span) diff --git a/test/baseVisitors.test.ts b/test/baseVisitors.test.ts index 322a565..f82e651 100644 --- a/test/baseVisitors.test.ts +++ b/test/baseVisitors.test.ts @@ -121,6 +121,7 @@ const tests: Array = [ { type: 'TaggedTemplateExpression', code: 'foo`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) {}' }, diff --git a/test/utils.ts b/test/utils.ts index 57d876f..500f0de 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -7,7 +7,7 @@ import { simple } from '../src/walk.js' import { BaseVisitor } from '../src/baseVisitor.js' export type Test = { - type: keyof BaseVisitor + type: keyof BaseVisitor skip?: string code: string times?: number