Skip to content

Commit f262fd8

Browse files
committed
refactor(babel): use walkAST instead of @babel/traverse
1 parent f35e7e0 commit f262fd8

File tree

5 files changed

+31
-30
lines changed

5 files changed

+31
-30
lines changed

packages/babel/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@
4343
"@babel/core": "catalog:",
4444
"@babel/parser": "catalog:",
4545
"@babel/plugin-syntax-jsx": "catalog:",
46-
"@babel/traverse": "catalog:",
4746
"@babel/types": "catalog:",
4847
"@vue-jsx-vapor/compiler": "workspace:*",
48+
"ast-kit": "catalog:",
4949
"source-map-js": "catalog:"
5050
},
5151
"devDependencies": {
52+
"@babel/traverse": "catalog:",
5253
"@types/babel__core": "catalog:",
5354
"@types/babel__generator": "^7.27.0",
5455
"@types/babel__template": "^7.4.4",

packages/babel/src/transform.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import { parse } from '@babel/parser'
2-
import _traverse, { type VisitNodeFunction } from '@babel/traverse'
32
import { compile } from '@vue-jsx-vapor/compiler'
3+
import { walkAST } from 'ast-kit'
44
import { SourceMapConsumer } from 'source-map-js'
55
import { isJSXElement } from './utils'
66
import type { Options } from '.'
7-
import type { JSXElement, JSXFragment } from '@babel/types'
8-
9-
// @ts-ignore
10-
const traverse = (_traverse.default || _traverse) as typeof _traverse
7+
import type { VisitNodeFunction } from '@babel/traverse'
8+
import type { JSXElement, JSXFragment, Node } from '@babel/types'
119

1210
export const transformJSX: VisitNodeFunction<
1311
Options,
@@ -39,15 +37,19 @@ export const transformJSX: VisitNodeFunction<
3937

4038
if (map) {
4139
const consumer = new SourceMapConsumer(map)
42-
traverse(ast, {
43-
Identifier({ node: id }) {
44-
if (!id.loc) return
45-
const originalLoc = consumer.originalPositionFor(id.loc.start)
46-
if (originalLoc.column) {
47-
id.loc.start.line = originalLoc.line
48-
id.loc.start.column = originalLoc.column
49-
id.loc.end.line = originalLoc.line
50-
id.loc.end.column = originalLoc.column + id.name.length
40+
walkAST<Node>(ast, {
41+
enter(id) {
42+
if (
43+
(id.type === 'Identifier' || id.type === 'JSXIdentifier') &&
44+
id.loc
45+
) {
46+
const originalLoc = consumer.originalPositionFor(id.loc.start)
47+
if (originalLoc.column) {
48+
id.loc.start.line = originalLoc.line
49+
id.loc.start.column = originalLoc.column
50+
id.loc.end.line = originalLoc.line
51+
id.loc.end.column = originalLoc.column + id.name.length
52+
}
5153
}
5254
},
5355
})

packages/compiler/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"@babel/types": "catalog:",
4545
"@vue/compiler-dom": "catalog:",
4646
"@vue/shared": "catalog:",
47-
"ast-kit": "^2.1.1",
47+
"ast-kit": "catalog:",
4848
"source-map-js": "catalog:"
4949
}
5050
}

pnpm-lock.yaml

Lines changed: 11 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ catalog:
3636
ts-macro: *ts-macro
3737
unplugin: ^2.3.5
3838
unplugin-utils: ^0.2.4
39+
ast-kit: ^2.1.1
3940
vite: ^7.0.2
4041
vitest: ^3.2.4
4142
vue: https://pkg.pr.new/vue@13642

0 commit comments

Comments
 (0)