Skip to content

Commit 3ae1418

Browse files
committed
fix: shouldn't transform callExpression without variableDeclaration
1 parent 5e8ae0c commit 3ae1418

File tree

4 files changed

+13
-4
lines changed

4 files changed

+13
-4
lines changed

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,11 @@ function transformReactivityFunction(
101101

102102
for (const { ast, offset } of asts) {
103103
walkAST<t.Node>(ast, {
104-
enter(node) {
104+
enter(node, parent) {
105105
if (node.type !== 'CallExpression') return
106106

107107
if (
108+
parent?.type === 'VariableDeclarator' &&
108109
new RegExp(`^\\$(?!(\\$|${ignore.join('|')})?$)`).test(
109110
s.sliceNode(node.callee, { offset }),
110111
)

src/volar.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,13 @@ function transform({
7373
}
7474
}
7575

76-
function walkReactivityFunction(node: import('typescript').Node) {
76+
function walkReactivityFunction(
77+
node: import('typescript').Node,
78+
parent: import('typescript').Node,
79+
) {
7780
if (ts.isCallExpression(node)) {
7881
if (
82+
ts.isVariableDeclaration(parent) &&
7983
new RegExp(`^\\$(?!(\\$|${ignore.join('|')})?$)`).test(
8084
getText(node.expression, ast, ts),
8185
)
@@ -104,10 +108,10 @@ function transform({
104108
}
105109

106110
ts.forEachChild(node, (child) => {
107-
walkReactivityFunction(child)
111+
walkReactivityFunction(child, node)
108112
})
109113
}
110-
ts.forEachChild(ast, walkReactivityFunction)
114+
ts.forEachChild(ast, (node) => walkReactivityFunction(node, ast))
111115
}
112116

113117
const plugin = createPlugin<{ ignore?: string[] } | undefined>(

tests/__snapshots__/basic.test.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var _sfc_main = /* @__PURE__ */ defineComponent({
1919
const { id, name } = $(useApi());
2020
id === 1;
2121
useApi($$(name));
22+
console.log($useApi());
2223
watch($$(name), () => {
2324
});
2425
defineExpose({

tests/fixtures/basic.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ const { id, name } = $useApi()
1414
id === 1
1515
useApi$(name)
1616
17+
// @ts-expect-error
18+
console.log($useApi())
19+
1720
watch$(name, () => {})
1821
1922
defineExpose$({

0 commit comments

Comments
 (0)