Skip to content

Commit dc47fc7

Browse files
authored
Merge pull request #3306 from cvlab/master
Fix non-object value of spread attributes variable
2 parents e686216 + 5c5339e commit dc47fc7

File tree

11 files changed

+199
-1
lines changed

11 files changed

+199
-1
lines changed

src/compiler/compile/render_dom/wrappers/InlineComponent/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,11 @@ export default class InlineComponentWrapper extends Wrapper {
198198
const value = attr.expression.render(block);
199199
initial_props.push(value);
200200

201-
changes.push(condition ? `${condition} && ${value}` : value);
201+
let value_object = value
202+
if (attr.expression.node.type !== 'ObjectExpression') {
203+
value_object = `@get_spread_object(${value})`;
204+
}
205+
changes.push(condition ? `${condition} && ${value_object}` : value_object);
202206
} else {
203207
const obj = `{ ${quote_name_if_necessary(name)}: ${attr.get_value(block)} }`;
204208
initial_props.push(obj);

src/runtime/internal/spread.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,7 @@ export function get_spread_update(levels, updates) {
3535

3636
return update;
3737
}
38+
39+
export function get_spread_object(spread_props) {
40+
return typeof spread_props === 'object' && spread_props !== null ? spread_props : {};
41+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script>
2+
export let foo;
3+
export let baz;
4+
export let qux;
5+
export let corge;
6+
</script>
7+
8+
<p>foo: {foo}</p>
9+
<p>baz: {baz}</p>
10+
<p>qux: {qux}</p>
11+
<p>corge: {corge}</p>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
export default {
2+
props: {
3+
props: {
4+
foo: 'lol',
5+
baz: 40 + 2,
6+
}
7+
},
8+
9+
html: `
10+
<div><p>foo: lol</p>
11+
<p>baz: 42</p>
12+
<p>qux: named</p>
13+
<p>corge: b</p>
14+
`,
15+
16+
test({ assert, component, target }) {
17+
const html = `
18+
<div><p>foo: undefined</p>
19+
<p>baz: undefined</p>
20+
<p>qux: named</p>
21+
<p>corge: b</p>
22+
`;
23+
24+
// test undefined
25+
component.props = undefined;
26+
assert.htmlEqual(target.innerHTML, html);
27+
28+
// set object props
29+
component.props = this.props.props;
30+
assert.htmlEqual(target.innerHTML, this.html);
31+
32+
// test null
33+
component.props = null;
34+
assert.htmlEqual(target.innerHTML, html);
35+
36+
// set object props
37+
component.props = this.props.props;
38+
assert.htmlEqual(target.innerHTML, this.html);
39+
40+
// test boolean
41+
component.props = true;
42+
assert.htmlEqual(target.innerHTML, html);
43+
44+
// set object props
45+
component.props = this.props.props;
46+
assert.htmlEqual(target.innerHTML, this.html);
47+
48+
// test number
49+
component.props = 123;
50+
assert.htmlEqual(target.innerHTML, html);
51+
52+
}
53+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script>
2+
import Widget from './Widget.svelte';
3+
4+
export let props;
5+
6+
export let corge = false;
7+
export let a = 'a';
8+
export let b = 'b';
9+
</script>
10+
11+
<div>
12+
<Widget corge={corge ? a : b} {...props} qux="named"/>
13+
</div>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script>
2+
export let foo;
3+
export let baz;
4+
export let qux;
5+
</script>
6+
7+
<p>foo: {foo}</p>
8+
<p>baz: {baz}</p>
9+
<p>qux: {qux}</p>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
export default {
2+
props: {
3+
props: {
4+
foo: 'lol',
5+
baz: 40 + 2,
6+
}
7+
},
8+
9+
html: `
10+
<div><p>foo: lol</p>
11+
<p>baz: 42</p>
12+
<p>qux: named</p>
13+
`,
14+
15+
test({ assert, component, target }) {
16+
const html = `
17+
<div><p>foo: undefined</p>
18+
<p>baz: undefined</p>
19+
<p>qux: named</p>
20+
`;
21+
22+
// test undefined
23+
component.props = undefined;
24+
assert.htmlEqual(target.innerHTML, html);
25+
26+
// set object props
27+
component.props = this.props.props;
28+
assert.htmlEqual(target.innerHTML, this.html);
29+
30+
// test null
31+
component.props = null;
32+
assert.htmlEqual(target.innerHTML, html);
33+
34+
// set object props
35+
component.props = this.props.props;
36+
assert.htmlEqual(target.innerHTML, this.html);
37+
38+
// test boolean
39+
component.props = true;
40+
assert.htmlEqual(target.innerHTML, html);
41+
42+
// set object props
43+
component.props = this.props.props;
44+
assert.htmlEqual(target.innerHTML, this.html);
45+
46+
// test number
47+
component.props = 123;
48+
assert.htmlEqual(target.innerHTML, html);
49+
50+
}
51+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script>
2+
import Widget from './Widget.svelte';
3+
4+
export let props;
5+
</script>
6+
7+
<div>
8+
<Widget {...props} qux="named"/>
9+
</div>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<script>
2+
export let i;
3+
export let foo;
4+
export let qux;
5+
</script>
6+
7+
<p>i: {i}</p>
8+
<p>foo: {foo}</p>
9+
<p>qux: {qux}</p>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export default {
2+
props: {},
3+
4+
html: `
5+
<div><p>i: 1</p>
6+
<p>foo: foo</p>
7+
<p>qux: named</p>
8+
`,
9+
10+
test({ assert, component, target }) {
11+
component.foo = 'lol';
12+
13+
assert.htmlEqual(target.innerHTML, `
14+
<div><p>i: 2</p>
15+
<p>foo: lol</p>
16+
<p>qux: named</p>
17+
`);
18+
}
19+
};

0 commit comments

Comments
 (0)