feat: Reactive delete statements

pull/6826/head
Bob Fanger 5 years ago
parent a5968dd2d0
commit f542ac9ab8

@ -844,7 +844,7 @@ export default class Component {
scope = map.get(node); scope = map.get(node);
} }
if (node.type === 'AssignmentExpression' || node.type === 'UpdateExpression') { if (node.type === 'AssignmentExpression' || node.type === 'UpdateExpression' || (node.type === 'UnaryExpression' && node.operator === 'delete')) {
const assignee = node.type === 'AssignmentExpression' ? node.left : node.argument; const assignee = node.type === 'AssignmentExpression' ? node.left : node.argument;
const names = extract_names(assignee as Node); const names = extract_names(assignee as Node);
@ -1303,7 +1303,7 @@ export default class Component {
if (node.operator !== '=') { if (node.operator !== '=') {
dependencies.add(left.name); dependencies.add(left.name);
} }
} else if (node.type === 'UpdateExpression') { } else if (node.type === 'UpdateExpression' || (node.type === 'UnaryExpression' && node.operator === 'delete')) {
const identifier = get_object(node.argument); const identifier = get_object(node.argument);
assignees.add(identifier.name); assignees.add(identifier.name);
} else if (is_reference(node as NodeWithPropertyDefinition, parent as NodeWithPropertyDefinition)) { } else if (is_reference(node as NodeWithPropertyDefinition, parent as NodeWithPropertyDefinition)) {

@ -125,7 +125,7 @@ export default class Expression {
if (node.type === 'AssignmentExpression') { if (node.type === 'AssignmentExpression') {
deep = node.left.type === 'MemberExpression'; deep = node.left.type === 'MemberExpression';
names = extract_names(deep ? get_object(node.left) : node.left); names = extract_names(deep ? get_object(node.left) : node.left);
} else if (node.type === 'UpdateExpression') { } else if (node.type === 'UpdateExpression' || (node.type === 'UnaryExpression' && node.operator === 'delete')) {
names = extract_names(get_object(node.argument)); names = extract_names(get_object(node.argument));
} }
} }
@ -343,7 +343,7 @@ export default class Expression {
} }
} }
if (node.type === 'AssignmentExpression' || node.type === 'UpdateExpression') { if (node.type === 'AssignmentExpression' || node.type === 'UpdateExpression' || (node.type === 'UnaryExpression' && node.operator === 'delete')) {
const assignee = node.type === 'AssignmentExpression' ? node.left : node.argument; const assignee = node.type === 'AssignmentExpression' ? node.left : node.argument;
const object_name = get_object(assignee).name; const object_name = get_object(assignee).name;

@ -283,7 +283,7 @@ export default function dom(
execution_context = null; execution_context = null;
} }
if (node.type === 'AssignmentExpression' || node.type === 'UpdateExpression') { if (node.type === 'AssignmentExpression' || node.type === 'UpdateExpression' || (node.type === 'UnaryExpression' && node.operator === 'delete')) {
const assignee = node.type === 'AssignmentExpression' ? node.left : node.argument; const assignee = node.type === 'AssignmentExpression' ? node.left : node.argument;
// normally (`a = 1`, `b.c = 2`), there'll be a single name // normally (`a = 1`, `b.c = 2`), there'll be a single name

@ -58,7 +58,8 @@ export function invalidate(renderer: Renderer, scope: Scope, node: Node, names:
const pass_value = ( const pass_value = (
extra_args.length > 0 || extra_args.length > 0 ||
(node.type === 'AssignmentExpression' && node.left.type !== 'Identifier') || (node.type === 'AssignmentExpression' && node.left.type !== 'Identifier') ||
(node.type === 'UpdateExpression' && (!node.prefix || node.argument.type !== 'Identifier')) (node.type === 'UpdateExpression' && (!node.prefix || node.argument.type !== 'Identifier')) ||
(node.type === 'UnaryExpression' && node.operator === 'delete' && (!node.prefix || node.argument.type !== 'Identifier'))
); );
if (pass_value) { if (pass_value) {
extra_args.unshift({ extra_args.unshift({

@ -0,0 +1,31 @@
/* generated by Svelte vX.Y.Z */
import { SvelteComponent, init, safe_not_equal } from "svelte/internal";
function instance($$self, $$props, $$invalidate) {
const a = { b: true };
function removeB() {
$$invalidate(1, delete a.b, a);
}
$$self.$$.update = () => {
if ($$self.$$.dirty & /*a*/ 2) {
$: console.log(a.b);
}
};
return [removeB, a];
}
class Component extends SvelteComponent {
constructor(options) {
super();
init(this, options, instance, null, safe_not_equal, { removeB: 0 });
}
get removeB() {
return this.$$.ctx[0];
}
}
export default Component;

@ -0,0 +1,9 @@
<script>
const a = { b: true };
export function removeB() {
delete a.b;
}
$: console.log(a.b);
</script>
Loading…
Cancel
Save