Merge pull request #3306 from cvlab/master

Fix non-object value of spread attributes variable
pull/3540/head
Rich Harris 5 years ago committed by GitHub
commit dc47fc741a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -198,7 +198,11 @@ export default class InlineComponentWrapper extends Wrapper {
const value = attr.expression.render(block);
initial_props.push(value);
changes.push(condition ? `${condition} && ${value}` : value);
let value_object = value
if (attr.expression.node.type !== 'ObjectExpression') {
value_object = `@get_spread_object(${value})`;
}
changes.push(condition ? `${condition} && ${value_object}` : value_object);
} else {
const obj = `{ ${quote_name_if_necessary(name)}: ${attr.get_value(block)} }`;
initial_props.push(obj);

@ -35,3 +35,7 @@ export function get_spread_update(levels, updates) {
return update;
}
export function get_spread_object(spread_props) {
return typeof spread_props === 'object' && spread_props !== null ? spread_props : {};
}

@ -0,0 +1,11 @@
<script>
export let foo;
export let baz;
export let qux;
export let corge;
</script>
<p>foo: {foo}</p>
<p>baz: {baz}</p>
<p>qux: {qux}</p>
<p>corge: {corge}</p>

@ -0,0 +1,53 @@
export default {
props: {
props: {
foo: 'lol',
baz: 40 + 2,
}
},
html: `
<div><p>foo: lol</p>
<p>baz: 42</p>
<p>qux: named</p>
<p>corge: b</p>
`,
test({ assert, component, target }) {
const html = `
<div><p>foo: undefined</p>
<p>baz: undefined</p>
<p>qux: named</p>
<p>corge: b</p>
`;
// test undefined
component.props = undefined;
assert.htmlEqual(target.innerHTML, html);
// set object props
component.props = this.props.props;
assert.htmlEqual(target.innerHTML, this.html);
// test null
component.props = null;
assert.htmlEqual(target.innerHTML, html);
// set object props
component.props = this.props.props;
assert.htmlEqual(target.innerHTML, this.html);
// test boolean
component.props = true;
assert.htmlEqual(target.innerHTML, html);
// set object props
component.props = this.props.props;
assert.htmlEqual(target.innerHTML, this.html);
// test number
component.props = 123;
assert.htmlEqual(target.innerHTML, html);
}
};

@ -0,0 +1,13 @@
<script>
import Widget from './Widget.svelte';
export let props;
export let corge = false;
export let a = 'a';
export let b = 'b';
</script>
<div>
<Widget corge={corge ? a : b} {...props} qux="named"/>
</div>

@ -0,0 +1,9 @@
<script>
export let foo;
export let baz;
export let qux;
</script>
<p>foo: {foo}</p>
<p>baz: {baz}</p>
<p>qux: {qux}</p>

@ -0,0 +1,51 @@
export default {
props: {
props: {
foo: 'lol',
baz: 40 + 2,
}
},
html: `
<div><p>foo: lol</p>
<p>baz: 42</p>
<p>qux: named</p>
`,
test({ assert, component, target }) {
const html = `
<div><p>foo: undefined</p>
<p>baz: undefined</p>
<p>qux: named</p>
`;
// test undefined
component.props = undefined;
assert.htmlEqual(target.innerHTML, html);
// set object props
component.props = this.props.props;
assert.htmlEqual(target.innerHTML, this.html);
// test null
component.props = null;
assert.htmlEqual(target.innerHTML, html);
// set object props
component.props = this.props.props;
assert.htmlEqual(target.innerHTML, this.html);
// test boolean
component.props = true;
assert.htmlEqual(target.innerHTML, html);
// set object props
component.props = this.props.props;
assert.htmlEqual(target.innerHTML, this.html);
// test number
component.props = 123;
assert.htmlEqual(target.innerHTML, html);
}
};

@ -0,0 +1,9 @@
<script>
import Widget from './Widget.svelte';
export let props;
</script>
<div>
<Widget {...props} qux="named"/>
</div>

@ -0,0 +1,9 @@
<script>
export let i;
export let foo;
export let qux;
</script>
<p>i: {i}</p>
<p>foo: {foo}</p>
<p>qux: {qux}</p>

@ -0,0 +1,19 @@
export default {
props: {},
html: `
<div><p>i: 1</p>
<p>foo: foo</p>
<p>qux: named</p>
`,
test({ assert, component, target }) {
component.foo = 'lol';
assert.htmlEqual(target.innerHTML, `
<div><p>i: 2</p>
<p>foo: lol</p>
<p>qux: named</p>
`);
}
};

@ -0,0 +1,16 @@
<script>
import Widget from './Widget.svelte';
export let foo = 'foo';
let i = 0
const getProps = (foo) => {
i += 1;
return { foo, i };
}
</script>
<div>
<Widget {...getProps(foo)} qux="named"/>
</div>
Loading…
Cancel
Save