Skip to content

Commit fd5673b

Browse files
committed
fix: prevent handle identifies in v-slot
1 parent 86b2c2c commit fd5673b

File tree

8 files changed

+36
-7
lines changed

8 files changed

+36
-7
lines changed

src/core/options.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ export function resolveOption(options: Options): OptionsResolved {
1818
include: [REGEX_SUPPORTED_EXT],
1919
exclude: [REGEX_NODE_MODULES],
2020
...options,
21-
ignore: [...ignore, ...(options.ignore || []).map((str) => str.slice(1))],
21+
ignore: [
22+
...ignore,
23+
...(options.ignore || []).map((str) =>
24+
str.replace(/^\$?([^$]+)\$?$/, '$1'),
25+
),
26+
],
2227
}
2328
}

src/core/utils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,15 @@ export async function getOxcParser(browser = typeof window !== 'undefined') {
9595
}
9696
return parseSync
9797
}
98+
99+
export function isInVSlot(node?: Node & { parent?: Node }): boolean {
100+
return (
101+
(node?.type === 'JSXAttribute' &&
102+
(node.name.type === 'JSXIdentifier'
103+
? node.name.name
104+
: node.name.type === 'JSXNamespacedName'
105+
? node.name.namespace.name
106+
: '') === 'v-slot') ||
107+
!!(node?.parent && isInVSlot(node?.parent))
108+
)
109+
}

src/raw.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
getOxcParser,
1818
getReferences,
1919
getRequire,
20+
isInVSlot,
2021
transformFunctionReturn,
2122
} from './core/utils'
2223
import type { IdentifierName, Node } from 'oxc-parser'
@@ -181,6 +182,7 @@ export async function transformReactivityFunction(
181182
}
182183
if (!refs.includes(identifier)) {
183184
const parent = identifier.parent
185+
if (isInVSlot(parent)) continue
184186
if (parent?.type === 'Property' && parent.shorthand) {
185187
// { foo } => { foo: foo.value }
186188
s.appendLeft(identifier.start, `${id.name}: `)

src/volar.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
getOxcParser,
1212
getReferences,
1313
getRequire,
14+
isInVSlot,
1415
transformFunctionReturn,
1516
} from './core/utils'
1617
import type { IdentifierName, Node } from 'oxc-parser'
@@ -264,6 +265,8 @@ function transformReactivityFunction(options: {
264265
parent: Node
265266
}
266267
if (refs.includes(identifier)) {
268+
const parent = identifier.parent
269+
if (isInVSlot(parent)) continue
267270
replaceSourceRange(
268271
codes,
269272
source,
@@ -272,7 +275,6 @@ function transformReactivityFunction(options: {
272275
`(${id.name},${HELPER_PREFIX}refs_${id.name}.`,
273276
)
274277
replaceSourceRange(codes, source, identifier.end, identifier.end, ')')
275-
const parent = identifier.parent
276278
if (parent?.type === 'Property' && parent.shorthand) {
277279
replaceSourceRange(
278280
codes,

tests/__snapshots__/basic.test.ts.snap

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`fixtures > tests/fixtures/basic.tsx 1`] = `
44
"// basic.js
5-
import { ref, toRef, createPropsRestProxy, watch, createVNode } from 'vue';
5+
import { ref, toRef, createPropsRestProxy, watch, withDirectives, createVNode, resolveDirective } from 'vue';
66
77
function useApi(defaultName = ref('')) {
88
const id = ref(1);
@@ -51,12 +51,14 @@ console.log(title);
5151
const Comp = ({
5252
title
5353
}) => title.value;
54-
var basic = () => createVNode(Comp, {
54+
var basic = () => withDirectives(createVNode(Comp, {
5555
"title": title,
5656
"foo": title.value
5757
}, {
5858
default: () => [title.value]
59-
});
59+
}), [[resolveDirective("slot"), {
60+
title
61+
}, "slot"]]);
6062
6163
export { basic as default };
6264
"

tests/basic.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'node:path'
22
import { rollupBuild, RollupVueJsx, testFixtures } from '@vue-macros/test-utils'
33
import { describe } from 'vitest'
4-
import VueReactivityFunction from '../dist/rollup'
4+
import VueReactivityFunction from '../src/rollup'
55

66
describe('fixtures', async () => {
77
await testFixtures(

tests/fixtures/basic.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const title = $ref<string>('title')
4747
console.log($$(title))
4848
const Comp = ({ title }: { title: Ref<string>; foo: string }) => title.value
4949
export default () => (
50-
<Comp title$={title} foo={title}>
50+
<Comp title$={title} foo={title} v-slot:slot={{ title }}>
5151
{title}
5252
</Comp>
5353
)

vitest.config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { defineConfig } from 'vitest/config'
2+
export default defineConfig({
3+
define: {
4+
__ESM__: 'true',
5+
},
6+
})

0 commit comments

Comments
 (0)