Merge pull request #1925 from sveltejs/gh-1924

Prevent modification of internal state
pull/1928/head
Rich Harris 6 years ago committed by GitHub
commit cfb6f52f2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -80,7 +80,7 @@ export default function dom(
$$invalidate('${component.meta.props_object}', ${component.meta.props_object}); $$invalidate('${component.meta.props_object}', ${component.meta.props_object});
`} `}
${props.map(prop => ${props.map(prop =>
`if ('${prop.as}' in $$props) ${prop.name} = $$props.${prop.as};`)} `if ('${prop.as}' in $$props) $$invalidate('${prop.name}', ${prop.name} = $$props.${prop.as});`)}
} }
` `
: null; : null;
@ -231,8 +231,7 @@ export default function dom(
} }
const args = ['$$self']; const args = ['$$self'];
if (component.props.length > 0 || component.has_reactive_assignments) args.push('$$props'); if (component.props.length > 0 || component.has_reactive_assignments) args.push('$$props', '$$invalidate');
if (component.has_reactive_assignments) args.push('$$invalidate');
builder.addBlock(deindent` builder.addBlock(deindent`
function create_fragment(${component.alias('component')}, ctx) { function create_fragment(${component.alias('component')}, ctx) {
@ -301,7 +300,7 @@ export default function dom(
${reactive_store_subscriptions} ${reactive_store_subscriptions}
${set && `$$self.$$.set = ${set};`} ${set && `$$self.$set = ${set};`}
${component.reactive_declarations.length > 0 && deindent` ${component.reactive_declarations.length > 0 && deindent`
$$self.$$.update = ($$dirty = { ${Array.from(all_reactive_dependencies).map(n => `${n}: 1`).join(', ')} }) => { $$self.$$.update = ($$dirty = { ${Array.from(all_reactive_dependencies).map(n => `${n}: 1`).join(', ')} }) => {

@ -151,17 +151,8 @@ if (typeof HTMLElement !== 'undefined') {
}; };
} }
$set(values) { $set() {
if (this.$$) { // overridden by instance, if it has props
const { ctx, set, not_equal } = this.$$;
set(values);
for (const key in values) {
if (not_equal(ctx[key], values[key])) {
ctx[key] = values[key];
make_dirty(this, key);
}
}
}
} }
} }
} }
@ -182,18 +173,8 @@ export class SvelteComponent {
}; };
} }
$set(values) { $set() {
if (this.$$) { // overridden by instance, if it has props
const { ctx, set, not_equal } = this.$$;
set(values);
for (const key in values) {
if (not_equal(ctx[key], values[key])) {
ctx[key] = values[key];
make_dirty(this, key);
}
}
}
} }
} }

@ -43,15 +43,15 @@ function foo(node, callback) {
// code goes here // code goes here
} }
function instance($$self, $$props) { function instance($$self, $$props, $$invalidate) {
let { bar } = $$props; let { bar } = $$props;
function foo_function() { function foo_function() {
return handleFoo(bar); return handleFoo(bar);
} }
$$self.$$.set = $$props => { $$self.$set = $$props => {
if ('bar' in $$props) bar = $$props.bar; if ('bar' in $$props) $$invalidate('bar', bar = $$props.bar);
}; };
return { bar, foo_function }; return { bar, foo_function };

@ -46,9 +46,9 @@ function instance($$self, $$props, $$invalidate) {
$$invalidate('h', h); $$invalidate('h', h);
} }
$$self.$$.set = $$props => { $$self.$set = $$props => {
if ('w' in $$props) w = $$props.w; if ('w' in $$props) $$invalidate('w', w = $$props.w);
if ('h' in $$props) h = $$props.h; if ('h' in $$props) $$invalidate('h', h = $$props.h);
}; };
return { w, h, div_resize_handler }; return { w, h, div_resize_handler };

@ -45,11 +45,11 @@ function create_fragment(component, ctx) {
}; };
} }
function instance($$self, $$props) { function instance($$self, $$props, $$invalidate) {
let { foo = 42 } = $$props; let { foo = 42 } = $$props;
$$self.$$.set = $$props => { $$self.$set = $$props => {
if ('foo' in $$props) foo = $$props.foo; if ('foo' in $$props) $$invalidate('foo', foo = $$props.foo);
}; };
return { foo }; return { foo };

@ -14,7 +14,7 @@ function create_fragment(component, ctx) {
}; };
} }
function instance($$self, $$props) { function instance($$self, $$props, $$invalidate) {
let { x } = $$props; let { x } = $$props;
function a() { function a() {
@ -25,8 +25,8 @@ function instance($$self, $$props) {
return x * 3; return x * 3;
} }
$$self.$$.set = $$props => { $$self.$set = $$props => {
if ('x' in $$props) x = $$props.x; if ('x' in $$props) $$invalidate('x', x = $$props.x);
}; };
return { x, a, b }; return { x, a, b };

@ -54,11 +54,11 @@ function create_fragment(component, ctx) {
}; };
} }
function instance($$self, $$props) { function instance($$self, $$props, $$invalidate) {
let { name } = $$props; let { name } = $$props;
$$self.$$.set = $$props => { $$self.$set = $$props => {
if ('name' in $$props) name = $$props.name; if ('name' in $$props) $$invalidate('name', name = $$props.name);
}; };
return { name }; return { name };

@ -139,14 +139,14 @@ function create_fragment(component, ctx) {
}; };
} }
function instance($$self, $$props) { function instance($$self, $$props, $$invalidate) {
let { things, foo, bar, baz } = $$props; let { things, foo, bar, baz } = $$props;
$$self.$$.set = $$props => { $$self.$set = $$props => {
if ('things' in $$props) things = $$props.things; if ('things' in $$props) $$invalidate('things', things = $$props.things);
if ('foo' in $$props) foo = $$props.foo; if ('foo' in $$props) $$invalidate('foo', foo = $$props.foo);
if ('bar' in $$props) bar = $$props.bar; if ('bar' in $$props) $$invalidate('bar', bar = $$props.bar);
if ('baz' in $$props) baz = $$props.baz; if ('baz' in $$props) $$invalidate('baz', baz = $$props.baz);
}; };
return { things, foo, bar, baz }; return { things, foo, bar, baz };

@ -139,12 +139,12 @@ function create_fragment(component, ctx) {
}; };
} }
function instance($$self, $$props) { function instance($$self, $$props, $$invalidate) {
let { things, foo } = $$props; let { things, foo } = $$props;
$$self.$$.set = $$props => { $$self.$set = $$props => {
if ('things' in $$props) things = $$props.things; if ('things' in $$props) $$invalidate('things', things = $$props.things);
if ('foo' in $$props) foo = $$props.foo; if ('foo' in $$props) $$invalidate('foo', foo = $$props.foo);
}; };
return { things, foo }; return { things, foo };

@ -105,11 +105,11 @@ function create_fragment(component, ctx) {
}; };
} }
function instance($$self, $$props) { function instance($$self, $$props, $$invalidate) {
let { createElement } = $$props; let { createElement } = $$props;
$$self.$$.set = $$props => { $$self.$set = $$props => {
if ('createElement' in $$props) createElement = $$props.createElement; if ('createElement' in $$props) $$invalidate('createElement', createElement = $$props.createElement);
}; };
return { createElement }; return { createElement };

@ -15,15 +15,15 @@ function create_fragment(component, ctx) {
}; };
} }
function instance($$self, $$props) { function instance($$self, $$props, $$invalidate) {
let { foo = 'bar' } = $$props; let { foo = 'bar' } = $$props;
onMount(() => { onMount(() => {
alert(JSON.stringify(data())); alert(JSON.stringify(data()));
}); });
$$self.$$.set = $$props => { $$self.$set = $$props => {
if ('foo' in $$props) foo = $$props.foo; if ('foo' in $$props) $$invalidate('foo', foo = $$props.foo);
}; };
return { foo }; return { foo };

@ -57,8 +57,8 @@ function instance($$self, $$props, $$invalidate) {
let bar; let bar;
$$self.$$.set = $$props => { $$self.$set = $$props => {
if ('foo' in $$props) foo = $$props.foo; if ('foo' in $$props) $$invalidate('foo', foo = $$props.foo);
}; };
$$self.$$.update = ($$dirty = { foo: 1 }) => { $$self.$$.update = ($$dirty = { foo: 1 }) => {

@ -43,11 +43,11 @@ function create_fragment(component, ctx) {
}; };
} }
function instance($$self, $$props) { function instance($$self, $$props, $$invalidate) {
let { bar } = $$props; let { bar } = $$props;
$$self.$$.set = $$props => { $$self.$set = $$props => {
if ('bar' in $$props) bar = $$props.bar; if ('bar' in $$props) $$invalidate('bar', bar = $$props.bar);
}; };
return { bar }; return { bar };

@ -43,11 +43,11 @@ function create_fragment(component, ctx) {
}; };
} }
function instance($$self, $$props) { function instance($$self, $$props, $$invalidate) {
let { bar } = $$props; let { bar } = $$props;
$$self.$$.set = $$props => { $$self.$set = $$props => {
if ('bar' in $$props) bar = $$props.bar; if ('bar' in $$props) $$invalidate('bar', bar = $$props.bar);
}; };
return { bar }; return { bar };

@ -41,11 +41,11 @@ function create_fragment(component, ctx) {
}; };
} }
function instance($$self, $$props) { function instance($$self, $$props, $$invalidate) {
let { bar } = $$props; let { bar } = $$props;
$$self.$$.set = $$props => { $$self.$set = $$props => {
if ('bar' in $$props) bar = $$props.bar; if ('bar' in $$props) $$invalidate('bar', bar = $$props.bar);
}; };
return { bar }; return { bar };

@ -145,14 +145,14 @@ function create_fragment(component, ctx) {
}; };
} }
function instance($$self, $$props) { function instance($$self, $$props, $$invalidate) {
let { comments, elapsed, time, foo } = $$props; let { comments, elapsed, time, foo } = $$props;
$$self.$$.set = $$props => { $$self.$set = $$props => {
if ('comments' in $$props) comments = $$props.comments; if ('comments' in $$props) $$invalidate('comments', comments = $$props.comments);
if ('elapsed' in $$props) elapsed = $$props.elapsed; if ('elapsed' in $$props) $$invalidate('elapsed', elapsed = $$props.elapsed);
if ('time' in $$props) time = $$props.time; if ('time' in $$props) $$invalidate('time', time = $$props.time);
if ('foo' in $$props) foo = $$props.foo; if ('foo' in $$props) $$invalidate('foo', foo = $$props.foo);
}; };
return { comments, elapsed, time, foo }; return { comments, elapsed, time, foo };

@ -120,11 +120,11 @@ function foo(node, animation, params) {
}; };
} }
function instance($$self, $$props) { function instance($$self, $$props, $$invalidate) {
let { things } = $$props; let { things } = $$props;
$$self.$$.set = $$props => { $$self.$set = $$props => {
if ('things' in $$props) things = $$props.things; if ('things' in $$props) $$invalidate('things', things = $$props.things);
}; };
return { things }; return { things };

@ -90,11 +90,11 @@ function create_fragment(component, ctx) {
}; };
} }
function instance($$self, $$props) { function instance($$self, $$props, $$invalidate) {
let { things } = $$props; let { things } = $$props;
$$self.$$.set = $$props => { $$self.$set = $$props => {
if ('things' in $$props) things = $$props.things; if ('things' in $$props) $$invalidate('things', things = $$props.things);
}; };
return { things }; return { things };

@ -93,11 +93,11 @@ function create_fragment(component, ctx) {
}; };
} }
function instance($$self, $$props) { function instance($$self, $$props, $$invalidate) {
let { foo } = $$props; let { foo } = $$props;
$$self.$$.set = $$props => { $$self.$set = $$props => {
if ('foo' in $$props) foo = $$props.foo; if ('foo' in $$props) $$invalidate('foo', foo = $$props.foo);
}; };
return { foo }; return { foo };

@ -69,11 +69,11 @@ function create_fragment(component, ctx) {
}; };
} }
function instance($$self, $$props) { function instance($$self, $$props, $$invalidate) {
let { foo } = $$props; let { foo } = $$props;
$$self.$$.set = $$props => { $$self.$set = $$props => {
if ('foo' in $$props) foo = $$props.foo; if ('foo' in $$props) $$invalidate('foo', foo = $$props.foo);
}; };
return { foo }; return { foo };

@ -41,13 +41,13 @@ function create_fragment(component, ctx) {
}; };
} }
function instance($$self, $$props) { function instance($$self, $$props, $$invalidate) {
let { color, x, y } = $$props; let { color, x, y } = $$props;
$$self.$$.set = $$props => { $$self.$set = $$props => {
if ('color' in $$props) color = $$props.color; if ('color' in $$props) $$invalidate('color', color = $$props.color);
if ('x' in $$props) x = $$props.x; if ('x' in $$props) $$invalidate('x', x = $$props.x);
if ('y' in $$props) y = $$props.y; if ('y' in $$props) $$invalidate('y', y = $$props.y);
}; };
return { color, x, y }; return { color, x, y };

@ -36,11 +36,11 @@ function create_fragment(component, ctx) {
}; };
} }
function instance($$self, $$props) { function instance($$self, $$props, $$invalidate) {
let { data } = $$props; let { data } = $$props;
$$self.$$.set = $$props => { $$self.$set = $$props => {
if ('data' in $$props) data = $$props.data; if ('data' in $$props) $$invalidate('data', data = $$props.data);
}; };
return { data }; return { data };

@ -36,11 +36,11 @@ function create_fragment(component, ctx) {
}; };
} }
function instance($$self, $$props) { function instance($$self, $$props, $$invalidate) {
let { color } = $$props; let { color } = $$props;
$$self.$$.set = $$props => { $$self.$set = $$props => {
if ('color' in $$props) color = $$props.color; if ('color' in $$props) $$invalidate('color', color = $$props.color);
}; };
return { color }; return { color };

@ -47,13 +47,13 @@ function create_fragment(component, ctx) {
}; };
} }
function instance($$self, $$props) { function instance($$self, $$props, $$invalidate) {
let { style, key, value } = $$props; let { style, key, value } = $$props;
$$self.$$.set = $$props => { $$self.$set = $$props => {
if ('style' in $$props) style = $$props.style; if ('style' in $$props) $$invalidate('style', style = $$props.style);
if ('key' in $$props) key = $$props.key; if ('key' in $$props) $$invalidate('key', key = $$props.key);
if ('value' in $$props) value = $$props.value; if ('value' in $$props) $$invalidate('value', value = $$props.value);
}; };
return { style, key, value }; return { style, key, value };

@ -49,8 +49,8 @@ function instance($$self, $$props, $$invalidate) {
$$invalidate('files', files); $$invalidate('files', files);
} }
$$self.$$.set = $$props => { $$self.$set = $$props => {
if ('files' in $$props) files = $$props.files; if ('files' in $$props) $$invalidate('files', files = $$props.files);
}; };
return { files, input_input_handler }; return { files, input_input_handler };

@ -52,8 +52,8 @@ function instance($$self, $$props, $$invalidate) {
$$invalidate('value', value); $$invalidate('value', value);
} }
$$self.$$.set = $$props => { $$self.$set = $$props => {
if ('value' in $$props) value = $$props.value; if ('value' in $$props) $$invalidate('value', value = $$props.value);
}; };
return { value, input_change_input_handler }; return { value, input_change_input_handler };

@ -48,8 +48,8 @@ function instance($$self, $$props, $$invalidate) {
$$invalidate('foo', foo); $$invalidate('foo', foo);
} }
$$self.$$.set = $$props => { $$self.$set = $$props => {
if ('foo' in $$props) foo = $$props.foo; if ('foo' in $$props) $$invalidate('foo', foo = $$props.foo);
}; };
return { foo, input_change_handler }; return { foo, input_change_handler };

@ -93,14 +93,14 @@ function instance($$self, $$props, $$invalidate) {
$$invalidate('volume', volume); $$invalidate('volume', volume);
} }
$$self.$$.set = $$props => { $$self.$set = $$props => {
if ('buffered' in $$props) buffered = $$props.buffered; if ('buffered' in $$props) $$invalidate('buffered', buffered = $$props.buffered);
if ('seekable' in $$props) seekable = $$props.seekable; if ('seekable' in $$props) $$invalidate('seekable', seekable = $$props.seekable);
if ('played' in $$props) played = $$props.played; if ('played' in $$props) $$invalidate('played', played = $$props.played);
if ('currentTime' in $$props) currentTime = $$props.currentTime; if ('currentTime' in $$props) $$invalidate('currentTime', currentTime = $$props.currentTime);
if ('duration' in $$props) duration = $$props.duration; if ('duration' in $$props) $$invalidate('duration', duration = $$props.duration);
if ('paused' in $$props) paused = $$props.paused; if ('paused' in $$props) $$invalidate('paused', paused = $$props.paused);
if ('volume' in $$props) volume = $$props.volume; if ('volume' in $$props) $$invalidate('volume', volume = $$props.volume);
}; };
return { return {

@ -63,11 +63,11 @@ function create_fragment(component, ctx) {
}; };
} }
function instance($$self, $$props) { function instance($$self, $$props, $$invalidate) {
let { current } = $$props; let { current } = $$props;
$$self.$$.set = $$props => { $$self.$set = $$props => {
if ('current' in $$props) current = $$props.current; if ('current' in $$props) $$invalidate('current', current = $$props.current);
}; };
return { current }; return { current };

@ -22,11 +22,11 @@ function create_fragment(component, ctx) {
}; };
} }
function instance($$self, $$props) { function instance($$self, $$props, $$invalidate) {
let { custom } = $$props; let { custom } = $$props;
$$self.$$.set = $$props => { $$self.$set = $$props => {
if ('custom' in $$props) custom = $$props.custom; if ('custom' in $$props) $$invalidate('custom', custom = $$props.custom);
}; };
return { custom }; return { custom };

@ -249,15 +249,15 @@ function create_fragment(component, ctx) {
}; };
} }
function instance($$self, $$props) { function instance($$self, $$props, $$invalidate) {
let { a, b, c, d, e } = $$props; let { a, b, c, d, e } = $$props;
$$self.$$.set = $$props => { $$self.$set = $$props => {
if ('a' in $$props) a = $$props.a; if ('a' in $$props) $$invalidate('a', a = $$props.a);
if ('b' in $$props) b = $$props.b; if ('b' in $$props) $$invalidate('b', b = $$props.b);
if ('c' in $$props) c = $$props.c; if ('c' in $$props) $$invalidate('c', c = $$props.c);
if ('d' in $$props) d = $$props.d; if ('d' in $$props) $$invalidate('d', d = $$props.d);
if ('e' in $$props) e = $$props.e; if ('e' in $$props) $$invalidate('e', e = $$props.e);
}; };
return { a, b, c, d, e }; return { a, b, c, d, e };

@ -63,8 +63,8 @@ function instance($$self, $$props, $$invalidate) {
y = window.pageYOffset; $$invalidate('y', y); y = window.pageYOffset; $$invalidate('y', y);
} }
$$self.$$.set = $$props => { $$self.$set = $$props => {
if ('y' in $$props) y = $$props.y; if ('y' in $$props) $$invalidate('y', y = $$props.y);
}; };
return { y, onwindowscroll }; return { y, onwindowscroll };

@ -0,0 +1,5 @@
<script>
let internal = 1;
</script>
<p>internal: {internal}</p>

@ -0,0 +1,18 @@
export default {
html: `
<p>internal: 1</p>
<button>click me</button>
`,
async test({ assert, target, window }) {
const button = target.querySelector('button');
const click = new window.MouseEvent('click');
await button.dispatchEvent(click);
assert.htmlEqual(target.innerHTML, `
<p>internal: 1</p>
<button>click me</button>
`);
}
};

@ -0,0 +1,9 @@
<script>
import Foo from './Foo.html';
let x = 2;
</script>
<Foo internal={x}/>
<button on:click="{() => x += 1}">click me</button>
Loading…
Cancel
Save