From cf44cc161c2e8effe98a3dd6587d6813486e9656 Mon Sep 17 00:00:00 2001 From: Nguyen Tran Date: Fri, 31 Mar 2023 22:22:38 -0400 Subject: [PATCH 01/16] Fix issue of casting a Node[] to a Node in rewrite_props in Component --- src/compiler/compile/Component.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/compiler/compile/Component.ts b/src/compiler/compile/Component.ts index e87cf6218af2..c09c1d96dfd0 100644 --- a/src/compiler/compile/Component.ts +++ b/src/compiler/compile/Component.ts @@ -1015,7 +1015,7 @@ export default class Component { let scope = instance_scope; walk(this.ast.instance.content, { - enter(node: Node) { + enter(node: Node, parent: Node, key, index) { if (regex_contains_term_function.test(node.type)) { return this.skip(); } @@ -1138,11 +1138,16 @@ export default class Component { } } - this.replace(b` - ${node.declarations.length ? node : null} - ${ props.length > 0 && b`let { ${props} } = $$props;`} - ${inserts} - ` as any); + if (inserts.length > 0) { + parent[key].splice(index + 1, 0, ...inserts); + } + if (props.length > 0) { + parent[key].splice(index + 1, 0, b`let { ${props} } = $$props;`); + } + if (node.declarations.length == 0) { + parent[key].splice(index, 1); + } + return this.skip(); } } From a98ece855ab35448ef95678d9d798289329a99fc Mon Sep 17 00:00:00 2001 From: Nguyen Tran Date: Fri, 31 Mar 2023 23:23:33 -0400 Subject: [PATCH 02/16] Add tests for var in blocks --- test/js/samples/var-in-block/expected.js | 46 +++++++++++++++++++ test/js/samples/var-in-block/input.svelte | 7 +++ test/runtime/samples/var-in-block/_config.js | 3 ++ test/runtime/samples/var-in-block/main.svelte | 9 ++++ .../samples/var-in-for-in-loop/_config.js | 3 ++ .../samples/var-in-for-in-loop/main.svelte | 10 ++++ .../samples/var-in-for-loop/_config.js | 3 ++ .../samples/var-in-for-loop/main.svelte | 10 ++++ .../samples/var-in-function/_config.js | 11 +++++ .../samples/var-in-function/main.svelte | 10 ++++ .../samples/var-in-if-block/_config.js | 13 ++++++ .../samples/var-in-if-block/main.svelte | 15 ++++++ .../samples/var-in-while-block/_config.js | 7 +++ .../samples/var-in-while-block/main.svelte | 9 ++++ 14 files changed, 156 insertions(+) create mode 100644 test/js/samples/var-in-block/expected.js create mode 100644 test/js/samples/var-in-block/input.svelte create mode 100644 test/runtime/samples/var-in-block/_config.js create mode 100644 test/runtime/samples/var-in-block/main.svelte create mode 100644 test/runtime/samples/var-in-for-in-loop/_config.js create mode 100644 test/runtime/samples/var-in-for-in-loop/main.svelte create mode 100644 test/runtime/samples/var-in-for-loop/_config.js create mode 100644 test/runtime/samples/var-in-for-loop/main.svelte create mode 100644 test/runtime/samples/var-in-function/_config.js create mode 100644 test/runtime/samples/var-in-function/main.svelte create mode 100644 test/runtime/samples/var-in-if-block/_config.js create mode 100644 test/runtime/samples/var-in-if-block/main.svelte create mode 100644 test/runtime/samples/var-in-while-block/_config.js create mode 100644 test/runtime/samples/var-in-while-block/main.svelte diff --git a/test/js/samples/var-in-block/expected.js b/test/js/samples/var-in-block/expected.js new file mode 100644 index 000000000000..53e0e43530ae --- /dev/null +++ b/test/js/samples/var-in-block/expected.js @@ -0,0 +1,46 @@ +/* generated by Svelte vX.Y.Z */ +import { + SvelteComponent, + detach, + init, + insert, + noop, + safe_not_equal, + text +} from "svelte/internal"; + +function create_fragment(ctx) { + let t; + + return { + c() { + t = text(/*one*/ ctx[0]); + }, + m(target, anchor) { + insert(target, t, anchor); + }, + p: noop, + i: noop, + o: noop, + d(detaching) { + if (detaching) detach(t); + } + }; +} + +function instance($$self) { + { + var one = 1; + } + + return [one]; +} + +class Component extends SvelteComponent { + constructor(options) { + super(); + init(this, options, instance, create_fragment, safe_not_equal, {}); + } +} + +export default Component; \ No newline at end of file diff --git a/test/js/samples/var-in-block/input.svelte b/test/js/samples/var-in-block/input.svelte new file mode 100644 index 000000000000..5aa45f0aa4ec --- /dev/null +++ b/test/js/samples/var-in-block/input.svelte @@ -0,0 +1,7 @@ + + +{one} diff --git a/test/runtime/samples/var-in-block/_config.js b/test/runtime/samples/var-in-block/_config.js new file mode 100644 index 000000000000..69523b27b45e --- /dev/null +++ b/test/runtime/samples/var-in-block/_config.js @@ -0,0 +1,3 @@ +export default { + html: '

12345

67890

' +} diff --git a/test/runtime/samples/var-in-block/main.svelte b/test/runtime/samples/var-in-block/main.svelte new file mode 100644 index 000000000000..ee3ca575a9aa --- /dev/null +++ b/test/runtime/samples/var-in-block/main.svelte @@ -0,0 +1,9 @@ + + +

{foo}

+

{bar}

diff --git a/test/runtime/samples/var-in-for-in-loop/_config.js b/test/runtime/samples/var-in-for-in-loop/_config.js new file mode 100644 index 000000000000..65ef73b63ff7 --- /dev/null +++ b/test/runtime/samples/var-in-for-in-loop/_config.js @@ -0,0 +1,3 @@ +export default { + html: '

0

2

4

6

8

' +} diff --git a/test/runtime/samples/var-in-for-in-loop/main.svelte b/test/runtime/samples/var-in-for-in-loop/main.svelte new file mode 100644 index 000000000000..8cc83d9305c7 --- /dev/null +++ b/test/runtime/samples/var-in-for-in-loop/main.svelte @@ -0,0 +1,10 @@ + + +{#each array as a} +

{a}

+{/each} diff --git a/test/runtime/samples/var-in-for-loop/_config.js b/test/runtime/samples/var-in-for-loop/_config.js new file mode 100644 index 000000000000..ba7a73f4efe2 --- /dev/null +++ b/test/runtime/samples/var-in-for-loop/_config.js @@ -0,0 +1,3 @@ +export default { + html: '

0

1

2

3

4

' +} diff --git a/test/runtime/samples/var-in-for-loop/main.svelte b/test/runtime/samples/var-in-for-loop/main.svelte new file mode 100644 index 000000000000..acf38eecaefe --- /dev/null +++ b/test/runtime/samples/var-in-for-loop/main.svelte @@ -0,0 +1,10 @@ + + +{#each array as a} +

{a}

+{/each} diff --git a/test/runtime/samples/var-in-function/_config.js b/test/runtime/samples/var-in-function/_config.js new file mode 100644 index 000000000000..f5c4a16cc046 --- /dev/null +++ b/test/runtime/samples/var-in-function/_config.js @@ -0,0 +1,11 @@ +export default { + html: '

0

', + + async test({ assert, target, window }) { + const button = target.querySelector('button'); + const clickEvent = new window.MouseEvent('click'); + + await button.dispatchEvent(clickEvent); + assert.htmlEqual(target.innerHTML, '

4

'); + } +}; diff --git a/test/runtime/samples/var-in-function/main.svelte b/test/runtime/samples/var-in-function/main.svelte new file mode 100644 index 000000000000..1dab3ff6b607 --- /dev/null +++ b/test/runtime/samples/var-in-function/main.svelte @@ -0,0 +1,10 @@ + + +