Skip to content

Commit e6cee1a

Browse files
committed
feat: don't resolve directives in scope
1 parent 2f75d2f commit e6cee1a

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

Diff for: packages/babel-plugin-jsx/src/parseDirectives.ts

+5
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ const resolveDirective = (
184184
}
185185
return modelToUse;
186186
}
187+
const referenceName =
188+
'v' + directiveName[0].toUpperCase() + directiveName.slice(1);
189+
if (path.scope.references[referenceName]) {
190+
return t.identifier(referenceName);
191+
}
187192
return t.callExpression(createIdentifier(state, 'resolveDirective'), [
188193
t.stringLiteral(directiveName),
189194
]);

Diff for: packages/babel-plugin-jsx/src/transform-vue-jsx.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,15 @@ const transformJSXElement = (
407407

408408
// #541 - directives can't be resolved in optimized slots
409409
// all parents should be deoptimized
410-
if (directives.length) {
410+
if (
411+
directives.length &&
412+
directives.some(
413+
(d) =>
414+
d.elements?.[0]?.type === 'CallExpression' &&
415+
d.elements[0].callee.type === 'Identifier' &&
416+
d.elements[0].callee.name === '_resolveDirective'
417+
)
418+
) {
411419
let currentPath = path;
412420
while (currentPath.parentPath?.isJSXElement()) {
413421
currentPath = currentPath.parentPath;

Diff for: packages/babel-plugin-jsx/test/__snapshots__/snapshot.test.ts.snap

+20
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ _createVNode(_Fragment, null, [_withDirectives(_createVNode(_resolveComponent("A
6363
}]])]);"
6464
`;
6565
66+
exports[`directive in scope > directive in scope 1`] = `
67+
"import { resolveComponent as _resolveComponent, createVNode as _createVNode, withDirectives as _withDirectives } from "vue";
68+
const vXxx = {};
69+
_withDirectives(_createVNode(_resolveComponent("A"), null, null, 512), [[vXxx]]);"
70+
`;
71+
6672
exports[`disable object slot syntax with defaultSlot > defaultSlot 1`] = `
6773
"import { resolveComponent as _resolveComponent, createVNode as _createVNode } from "vue";
6874
_createVNode(_resolveComponent("Badge"), null, {
@@ -164,6 +170,20 @@ _createVNode(_Fragment, null, [_createVNode(_resolveComponent("A"), null, {
164170
})]);"
165171
`;
166172
173+
exports[`passing object slots via JSX children directive in slot, in scope > directive in slot, in scope 1`] = `
174+
"import { Fragment as _Fragment, createVNode as _createVNode, withDirectives as _withDirectives, resolveComponent as _resolveComponent } from "vue";
175+
const vXxx = {};
176+
_createVNode(_Fragment, null, [_createVNode(_resolveComponent("A"), null, {
177+
default: () => [_withDirectives(_createVNode("div", null, null, 512), [[vXxx]]), foo],
178+
_: 1
179+
}), _createVNode(_resolveComponent("A"), null, {
180+
default: () => [_createVNode(_resolveComponent("B"), null, {
181+
default: () => [_withDirectives(_createVNode("div", null, null, 512), [[vXxx]]), foo],
182+
_: 1
183+
})]
184+
})]);"
185+
`;
186+
167187
exports[`passing object slots via JSX children multiple expressions > multiple expressions 1`] = `
168188
"import { resolveComponent as _resolveComponent, createVNode as _createVNode } from "vue";
169189
_createVNode(_resolveComponent("A"), null, {

Diff for: packages/babel-plugin-jsx/test/snapshot.test.ts

+19
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@ const transpile = (source: string, options: VueJSXPluginOptions = {}) =>
155155
</>
156156
`,
157157
},
158+
{
159+
name: 'directive in scope',
160+
from: `
161+
const vXxx = {};
162+
<A v-xxx />
163+
`,
164+
},
158165
{
159166
name: 'vModels',
160167
from: '<C v-models={[[foo, ["modifier"]], [bar, "bar", ["modifier1", "modifier2"]]]} />',
@@ -277,6 +284,18 @@ const slotsTests: Test[] = [
277284
</>
278285
`,
279286
},
287+
{
288+
name: 'directive in slot, in scope',
289+
from: `
290+
const vXxx = {};
291+
<>
292+
<A><div v-xxx />{foo}</A>
293+
<A>
294+
<B><div v-xxx />{foo}</B>
295+
</A>
296+
</>
297+
`,
298+
},
280299
];
281300

282301
slotsTests.forEach(({ name, from }) => {

0 commit comments

Comments
 (0)