From 5d70e4c07bbcde4f042a5580ad6839590cc0a02b Mon Sep 17 00:00:00 2001 From: Mikhail Korepanov Date: Sat, 16 Mar 2019 19:24:51 +0300 Subject: [PATCH] Add test case for #2115 and possible fix --- src/compile/nodes/Attribute.ts | 6 +- .../render-dom/wrappers/Element/Attribute.ts | 2 +- .../render-dom/wrappers/Element/index.ts | 4 +- .../wrappers/InlineComponent/index.ts | 6 +- .../samples/attribute-expression/expected.js | 97 +++++++++++++++++++ .../samples/attribute-expression/input.svelte | 3 + 6 files changed, 109 insertions(+), 9 deletions(-) create mode 100644 test/js/samples/attribute-expression/expected.js create mode 100644 test/js/samples/attribute-expression/input.svelte diff --git a/src/compile/nodes/Attribute.ts b/src/compile/nodes/Attribute.ts index 1071be902f..f17d53c497 100644 --- a/src/compile/nodes/Attribute.ts +++ b/src/compile/nodes/Attribute.ts @@ -86,14 +86,14 @@ export default class Attribute extends Node { return Array.from(dependencies); } - getValue() { + getValue(block) { if (this.isTrue) return true; if (this.chunks.length === 0) return `""`; if (this.chunks.length === 1) { return this.chunks[0].type === 'Text' ? stringify(this.chunks[0].data) - : this.chunks[0].render(); + : this.chunks[0].render(block); } return (this.chunks[0].type === 'Text' ? '' : `"" + `) + @@ -117,4 +117,4 @@ export default class Attribute extends Node { ? this.chunks[0].data : ''; } -} \ No newline at end of file +} diff --git a/src/compile/render-dom/wrappers/Element/Attribute.ts b/src/compile/render-dom/wrappers/Element/Attribute.ts index b098f68248..37eaf36457 100644 --- a/src/compile/render-dom/wrappers/Element/Attribute.ts +++ b/src/compile/render-dom/wrappers/Element/Attribute.ts @@ -179,7 +179,7 @@ export default class AttributeWrapper { ); } } else { - const value = this.node.getValue(); + const value = this.node.getValue(block); const statement = ( isLegacyInputType diff --git a/src/compile/render-dom/wrappers/Element/index.ts b/src/compile/render-dom/wrappers/Element/index.ts index 3920c72c63..33b5d5b7d7 100644 --- a/src/compile/render-dom/wrappers/Element/index.ts +++ b/src/compile/render-dom/wrappers/Element/index.ts @@ -579,7 +579,7 @@ export default class ElementWrapper extends Wrapper { updates.push(condition ? `${condition} && ${snippet}` : snippet); } else { - const snippet = `{ ${quoteNameIfNecessary(attr.name)}: ${attr.getValue()} }`; + const snippet = `{ ${quoteNameIfNecessary(attr.name)}: ${attr.getValue(block)} }`; initialProps.push(snippet); updates.push(condition ? `${condition} && ${snippet}` : snippet); @@ -846,4 +846,4 @@ export default class ElementWrapper extends Wrapper { ); } } -} \ No newline at end of file +} diff --git a/src/compile/render-dom/wrappers/InlineComponent/index.ts b/src/compile/render-dom/wrappers/InlineComponent/index.ts index 327eee59cf..fa42eb3540 100644 --- a/src/compile/render-dom/wrappers/InlineComponent/index.ts +++ b/src/compile/render-dom/wrappers/InlineComponent/index.ts @@ -118,7 +118,7 @@ export default class InlineComponentWrapper extends Wrapper { const attributeObject = usesSpread ? stringifyProps(slot_props) : stringifyProps( - this.node.attributes.map(attr => `${quoteNameIfNecessary(attr.name)}: ${attr.getValue()}`).concat(slot_props) + this.node.attributes.map(attr => `${quoteNameIfNecessary(attr.name)}: ${attr.getValue(block)}`).concat(slot_props) ); if (this.node.attributes.length || this.node.bindings.length || slot_props.length) { @@ -190,7 +190,7 @@ export default class InlineComponentWrapper extends Wrapper { changes.push(condition ? `${condition} && ${value}` : value); } else { - const obj = `{ ${quoteNameIfNecessary(name)}: ${attr.getValue()} }`; + const obj = `{ ${quoteNameIfNecessary(name)}: ${attr.getValue(block)} }`; initialProps.push(obj); changes.push(condition ? `${condition} && ${obj}` : obj); @@ -224,7 +224,7 @@ export default class InlineComponentWrapper extends Wrapper { updates.push(deindent` if (${[...attribute.dependencies] .map(dependency => `changed.${dependency}`) - .join(' || ')}) ${name_changes}${quotePropIfNecessary(attribute.name)} = ${attribute.getValue()}; + .join(' || ')}) ${name_changes}${quotePropIfNecessary(attribute.name)} = ${attribute.getValue(block)}; `); } }); diff --git a/test/js/samples/attribute-expression/expected.js b/test/js/samples/attribute-expression/expected.js new file mode 100644 index 0000000000..ee87efaeca --- /dev/null +++ b/test/js/samples/attribute-expression/expected.js @@ -0,0 +1,97 @@ +/* generated by Svelte vX.Y.Z */ +import { SvelteComponent as SvelteComponent_1, createComment, createElement, destroyEach, detachNode, init, insert, noop, safe_not_equal, setAttribute } from "svelte/internal"; + +function get_each_context(ctx, list, i) { + const child_ctx = Object.create(ctx); + child_ctx.item = list[i]; + return child_ctx; +} + +// (1:0) {#each Array(1) as item} +function create_each_block(ctx) { + var boo; + + function func() { + return ctx.func(ctx); + } + + return { + c() { + boo = createElement("boo"); + setAttribute(boo, "cb", func); + }, + + m(target, anchor) { + insert(target, boo, anchor); + }, + + p(changed, new_ctx) { + ctx = new_ctx; + }, + + d(detach) { + if (detach) { + detachNode(boo); + } + } + }; +} + +function create_fragment(ctx) { + var each_anchor; + + var each_value = Array(1); + + var each_blocks = []; + + for (var i = 0; i < each_value.length; i += 1) { + each_blocks[i] = create_each_block(get_each_context(ctx, each_value, i)); + } + + return { + c() { + for (var i = 0; i < each_blocks.length; i += 1) { + each_blocks[i].c(); + } + + each_anchor = createComment(); + }, + + m(target, anchor) { + for (var i = 0; i < each_blocks.length; i += 1) { + each_blocks[i].m(target, anchor); + } + + insert(target, each_anchor, anchor); + }, + + p: noop, + i: noop, + o: noop, + + d(detach) { + destroyEach(each_blocks, detach); + + if (detach) { + detachNode(each_anchor); + } + } + }; +} + +function instance($$self) { + function func({ item }) { + return item; + } + + return { func }; +} + +class SvelteComponent extends SvelteComponent_1 { + constructor(options) { + super(); + init(this, options, instance, create_fragment, safe_not_equal); + } +} + +export default SvelteComponent; diff --git a/test/js/samples/attribute-expression/input.svelte b/test/js/samples/attribute-expression/input.svelte new file mode 100644 index 0000000000..50be0bee3f --- /dev/null +++ b/test/js/samples/attribute-expression/input.svelte @@ -0,0 +1,3 @@ +{#each Array(1) as item} + +{/each}