mirror of https://github.com/sveltejs/svelte
commit
d8b4dd74fb
@ -0,0 +1,65 @@
|
||||
import Component from '../Component';
|
||||
import MagicString from 'magic-string';
|
||||
import { Node } from '../../interfaces';
|
||||
import { nodes_match } from '../../utils/nodes_match';
|
||||
import { Scope } from './scope';
|
||||
|
||||
export function invalidate(component: Component, scope: Scope, code: MagicString, node: Node, names: Set<string>) {
|
||||
const [head, ...tail] = Array.from(names).filter(name => {
|
||||
const owner = scope.find_owner(name);
|
||||
if (owner && owner !== component.instance_scope) return false;
|
||||
|
||||
const variable = component.var_lookup.get(name);
|
||||
|
||||
return variable && (
|
||||
!variable.hoistable &&
|
||||
!variable.global &&
|
||||
!variable.module &&
|
||||
(
|
||||
variable.referenced ||
|
||||
variable.is_reactive_dependency ||
|
||||
variable.export_name
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
if (head) {
|
||||
component.has_reactive_assignments = true;
|
||||
|
||||
if (node.operator === '=' && nodes_match(node.left, node.right) && tail.length === 0) {
|
||||
code.overwrite(node.start, node.end, component.invalidate(head));
|
||||
} else {
|
||||
let suffix = ')';
|
||||
|
||||
if (head[0] === '$') {
|
||||
code.prependRight(node.start, `${component.helper('set_store_value')}(${head.slice(1)}, `);
|
||||
} else {
|
||||
let prefix = `$$invalidate`;
|
||||
|
||||
const variable = component.var_lookup.get(head);
|
||||
if (variable.subscribable && variable.reassigned) {
|
||||
prefix = `$$subscribe_${head}($$invalidate`;
|
||||
suffix += `)`;
|
||||
}
|
||||
|
||||
code.prependRight(node.start, `${prefix}('${head}', `);
|
||||
}
|
||||
|
||||
const extra_args = tail.map(name => component.invalidate(name));
|
||||
|
||||
const pass_value = (
|
||||
extra_args.length > 0 ||
|
||||
(node.type === 'AssignmentExpression' && node.left.type !== 'Identifier') ||
|
||||
(node.type === 'UpdateExpression' && !node.prefix)
|
||||
);
|
||||
|
||||
if (pass_value) {
|
||||
extra_args.unshift(head);
|
||||
}
|
||||
|
||||
suffix = `${extra_args.map(arg => `, ${arg}`).join('')}${suffix}`;
|
||||
|
||||
code.appendLeft(node.end, suffix);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
export default {
|
||||
show: 1,
|
||||
html: `<button>false 0</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, `<button>true 1</button>`);
|
||||
|
||||
await button.dispatchEvent(click);
|
||||
assert.htmlEqual(target.innerHTML, `<button>false 1</button>`);
|
||||
|
||||
await button.dispatchEvent(click);
|
||||
assert.htmlEqual(target.innerHTML, `<button>true 2</button>`);
|
||||
}
|
||||
};
|
@ -0,0 +1,12 @@
|
||||
<script>
|
||||
let current = { active: false };
|
||||
let count = 0;
|
||||
|
||||
function toggle() {
|
||||
if (current.active = !current.active) {
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<button on:click={toggle}>{current.active} {count}</button>
|
Loading…
Reference in new issue