Skip to content

Commit 2154986

Browse files
committed
feat(compiler): emit lazy getter for scoped slot props
1 parent b7a3356 commit 2154986

File tree

16 files changed

+475
-17
lines changed

16 files changed

+475
-17
lines changed

packages-private/template-explorer/src/options.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const defaultOptions: CompilerOptions = {
2222
setupConst: BindingTypes.SETUP_CONST,
2323
setupLet: BindingTypes.SETUP_LET,
2424
setupMaybeRef: BindingTypes.SETUP_MAYBE_REF,
25+
setupComputed: BindingTypes.SETUP_COMPUTED,
2526
setupProp: BindingTypes.PROPS,
2627
vMySetupDir: BindingTypes.SETUP_CONST,
2728
},

packages/compiler-core/__tests__/transforms/__snapshots__/transformExpressions.spec.ts.snap

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

33
exports[`compiler: expression transform > bindingMetadata > inline mode 1`] = `
44
"(_ctx, _cache) => {
5-
return (_openBlock(), _createElementBlock("div", null, _toDisplayString(__props.props) + " " + _toDisplayString(_unref(setup)) + " " + _toDisplayString(setupConst) + " " + _toDisplayString(_ctx.data) + " " + _toDisplayString(_ctx.options) + " " + _toDisplayString(isNaN.value), 1 /* TEXT */))
5+
return (_openBlock(), _createElementBlock("div", null, _toDisplayString(__props.props) + " " + _toDisplayString(_unref(setup)) + " " + _toDisplayString(setupConst) + " " + _toDisplayString(_ctx.data) + " " + _toDisplayString(_ctx.options) + " " + _toDisplayString(isNaN.value) + " " + _toDisplayString(setupComputed.value), 1 /* TEXT */))
66
}"
77
`;
88

99
exports[`compiler: expression transform > bindingMetadata > non-inline mode 1`] = `
1010
"const { toDisplayString: _toDisplayString, openBlock: _openBlock, createElementBlock: _createElementBlock } = Vue
1111
1212
return function render(_ctx, _cache, $props, $setup, $data, $options) {
13-
return (_openBlock(), _createElementBlock("div", null, _toDisplayString($props.props) + " " + _toDisplayString($setup.setup) + " " + _toDisplayString($data.data) + " " + _toDisplayString($options.options) + " " + _toDisplayString($setup.isNaN), 1 /* TEXT */))
13+
return (_openBlock(), _createElementBlock("div", null, _toDisplayString($props.props) + " " + _toDisplayString($setup.setup) + " " + _toDisplayString($data.data) + " " + _toDisplayString($options.options) + " " + _toDisplayString($setup.isNaN) + " " + _toDisplayString($setup.setupComputed), 1 /* TEXT */))
1414
}"
1515
`;
1616

packages/compiler-core/__tests__/transforms/transformElement.spec.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,27 @@ describe('compiler: element transform', () => {
110110
expect(node.tag).toBe(`_unref(Example)`)
111111
})
112112

113+
test('resolve component from setup bindings (SETUP_COMPUTED)', () => {
114+
const { root, node } = parseWithElementTransform(`<Example/>`, {
115+
bindingMetadata: {
116+
Example: BindingTypes.SETUP_COMPUTED,
117+
},
118+
})
119+
expect(root.helpers).not.toContain(RESOLVE_COMPONENT)
120+
expect(node.tag).toBe(`$setup["Example"]`)
121+
})
122+
123+
test('resolve component from setup bindings (SETUP_COMPUTED inline)', () => {
124+
const { root, node } = parseWithElementTransform(`<Example/>`, {
125+
inline: true,
126+
bindingMetadata: {
127+
Example: BindingTypes.SETUP_COMPUTED,
128+
},
129+
})
130+
expect(root.helpers).not.toContain(RESOLVE_COMPONENT)
131+
expect(node.tag).toBe(`_unref(Example)`)
132+
})
133+
113134
test('resolve component from setup bindings (inline const)', () => {
114135
const { root, node } = parseWithElementTransform(`<Example/>`, {
115136
inline: true,
@@ -1028,6 +1049,30 @@ describe('compiler: element transform', () => {
10281049
expect(node.patchFlag).toBe(PatchFlags.NEED_PATCH)
10291050
})
10301051

1052+
test('script setup inline mode template ref (SETUP_COMPUTED binding)', () => {
1053+
const { node } = parseWithElementTransform(`<input ref="input"/>`, {
1054+
inline: true,
1055+
bindingMetadata: {
1056+
input: BindingTypes.SETUP_COMPUTED,
1057+
},
1058+
})
1059+
expect(node.props).toMatchObject({
1060+
type: NodeTypes.JS_OBJECT_EXPRESSION,
1061+
properties: [
1062+
{
1063+
type: NodeTypes.JS_PROPERTY,
1064+
key: { content: 'ref_key', isStatic: true },
1065+
value: { content: 'input', isStatic: true },
1066+
},
1067+
{
1068+
type: NodeTypes.JS_PROPERTY,
1069+
key: { content: 'ref', isStatic: true },
1070+
value: { content: 'input', isStatic: false },
1071+
},
1072+
],
1073+
})
1074+
})
1075+
10311076
test('script setup inline mode template ref (binding exists)', () => {
10321077
const { node } = parseWithElementTransform(`<input ref="input"/>`, {
10331078
inline: true,

packages/compiler-core/__tests__/transforms/transformExpressions.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,7 @@ describe('compiler: expression transform', () => {
618618
reactive: BindingTypes.SETUP_REACTIVE_CONST,
619619
literal: BindingTypes.LITERAL_CONST,
620620
isNaN: BindingTypes.SETUP_REF,
621+
setupComputed: BindingTypes.SETUP_COMPUTED,
621622
}
622623

623624
function compileWithBindingMetadata(
@@ -633,11 +634,12 @@ describe('compiler: expression transform', () => {
633634

634635
test('non-inline mode', () => {
635636
const { code } = compileWithBindingMetadata(
636-
`<div>{{ props }} {{ setup }} {{ data }} {{ options }} {{ isNaN }}</div>`,
637+
`<div>{{ props }} {{ setup }} {{ data }} {{ options }} {{ isNaN }} {{ setupComputed }}</div>`,
637638
)
638639
expect(code).toMatch(`$props.props`)
639640
expect(code).toMatch(`$setup.setup`)
640641
expect(code).toMatch(`$setup.isNaN`)
642+
expect(code).toMatch(`$setup.setupComputed`)
641643
expect(code).toMatch(`$data.data`)
642644
expect(code).toMatch(`$options.options`)
643645
expect(code).toMatch(`_ctx, _cache, $props, $setup, $data, $options`)
@@ -646,7 +648,7 @@ describe('compiler: expression transform', () => {
646648

647649
test('inline mode', () => {
648650
const { code } = compileWithBindingMetadata(
649-
`<div>{{ props }} {{ setup }} {{ setupConst }} {{ data }} {{ options }} {{ isNaN }}</div>`,
651+
`<div>{{ props }} {{ setup }} {{ setupConst }} {{ data }} {{ options }} {{ isNaN }} {{ setupComputed }}</div>`,
650652
{ inline: true },
651653
)
652654
expect(code).toMatch(`__props.props`)
@@ -655,6 +657,7 @@ describe('compiler: expression transform', () => {
655657
expect(code).toMatch(`_ctx.data`)
656658
expect(code).toMatch(`_ctx.options`)
657659
expect(code).toMatch(`isNaN.value`)
660+
expect(code).toMatch(`setupComputed.value`)
658661
expect(code).toMatchSnapshot()
659662
})
660663

0 commit comments

Comments
 (0)