From 347774af637535d56d9432c880166b8a30d47049 Mon Sep 17 00:00:00 2001 From: tanhauhau Date: Thu, 23 Feb 2023 08:36:02 +0800 Subject: [PATCH] feat: dynamic $$slots --- src/compiler/compile/Component.ts | 6 ++-- test/runtime/samples/$$slot-dynamic/A.svelte | 28 +++++++++++++++++ .../runtime/samples/$$slot-dynamic/_config.js | 23 ++++++++++++++ .../samples/$$slot-dynamic/main.svelte | 30 +++++++++++++++++++ .../Foo.svelte | 3 ++ .../_config.js | 13 ++++++++ .../main.svelte | 16 ++++++++++ 7 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 test/runtime/samples/$$slot-dynamic/A.svelte create mode 100644 test/runtime/samples/$$slot-dynamic/_config.js create mode 100644 test/runtime/samples/$$slot-dynamic/main.svelte create mode 100644 test/runtime/samples/component-conditional-slot-10-default/Foo.svelte create mode 100644 test/runtime/samples/component-conditional-slot-10-default/_config.js create mode 100644 test/runtime/samples/component-conditional-slot-10-default/main.svelte diff --git a/src/compiler/compile/Component.ts b/src/compiler/compile/Component.ts index c0d703892b61..e4b42e005c6a 100644 --- a/src/compiler/compile/Component.ts +++ b/src/compiler/compile/Component.ts @@ -219,7 +219,8 @@ export default class Component { this.add_var(node, { name, injected: true, - referenced: true + referenced: true, + reassigned: name === '$$slots' }); } else if (name[0] === '$') { this.add_var(node, { @@ -720,7 +721,8 @@ export default class Component { } else if (is_reserved_keyword(name)) { this.add_var(node, { name, - injected: true + injected: true, + reassigned: name === '$$slots' }); } else if (name[0] === '$') { if (name === '$' || name[1] === '$') { diff --git a/test/runtime/samples/$$slot-dynamic/A.svelte b/test/runtime/samples/$$slot-dynamic/A.svelte new file mode 100644 index 000000000000..8b73875de244 --- /dev/null +++ b/test/runtime/samples/$$slot-dynamic/A.svelte @@ -0,0 +1,28 @@ + + + + + +$$slots: {toString($$slots)} {stringified} + +{#if $$slots.b} +
+ +
+{:else} + Slot b is not available +{/if} \ No newline at end of file diff --git a/test/runtime/samples/$$slot-dynamic/_config.js b/test/runtime/samples/$$slot-dynamic/_config.js new file mode 100644 index 000000000000..2a3cc2bc9c54 --- /dev/null +++ b/test/runtime/samples/$$slot-dynamic/_config.js @@ -0,0 +1,23 @@ +export default { + html: ` + byedefault + hello a + $$slots: {"a":true,"default":true} {"a":true,"default":true} + Slot b is not available + `, + + async test({ assert, component, target }) { + assert.equal(component.getData(), '{"a":true,"default":true}'); + + component.show_b = true; + assert.htmlEqual(target.innerHTML, ` + byedefault + hello a + $$slots: {"a":true,"b":true,"default":true} {"a":true,"b":true,"default":true} +
hello b
+ `); + assert.equal(component.getData(), '{"a":true,"b":true,"default":true}'); + + component.show_default = false; + } +}; diff --git a/test/runtime/samples/$$slot-dynamic/main.svelte b/test/runtime/samples/$$slot-dynamic/main.svelte new file mode 100644 index 000000000000..bcaeafd16749 --- /dev/null +++ b/test/runtime/samples/$$slot-dynamic/main.svelte @@ -0,0 +1,30 @@ + + + + {#if show_a} + + hello a + + {/if} + {#if show_default} + + bye + default + + {/if} + {#if show_b} + + hello b + + {/if} + diff --git a/test/runtime/samples/component-conditional-slot-10-default/Foo.svelte b/test/runtime/samples/component-conditional-slot-10-default/Foo.svelte new file mode 100644 index 000000000000..7ffa56838602 --- /dev/null +++ b/test/runtime/samples/component-conditional-slot-10-default/Foo.svelte @@ -0,0 +1,3 @@ +
+ default fallback +
diff --git a/test/runtime/samples/component-conditional-slot-10-default/_config.js b/test/runtime/samples/component-conditional-slot-10-default/_config.js new file mode 100644 index 000000000000..6a107285eac7 --- /dev/null +++ b/test/runtime/samples/component-conditional-slot-10-default/_config.js @@ -0,0 +1,13 @@ +export default { + html: ` +
+
default fallback
+ `, + test({ assert, component, target }) { + component.condition = true; + assert.htmlEqual(target.innerHTML, ` +
hello #1
+
hello #2
+ `); + } +}; diff --git a/test/runtime/samples/component-conditional-slot-10-default/main.svelte b/test/runtime/samples/component-conditional-slot-10-default/main.svelte new file mode 100644 index 000000000000..a940df602c87 --- /dev/null +++ b/test/runtime/samples/component-conditional-slot-10-default/main.svelte @@ -0,0 +1,16 @@ + + + + {#if condition} + hello #1 + {/if} + + + + {#if condition} + hello #2 + {/if} +