Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #3634 compound operators in reactive statement #3640

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/compiler/compile/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ childKeys.EachBlock = childKeys.IfBlock = ['children', 'else'];
childKeys.Attribute = ['value'];
childKeys.ExportNamedDeclaration = ['declaration', 'specifiers'];

// There is no datatype that describes only the compound operators so we create this list here
const compoundAssignmentOperators = ["+=" , "-=" ,"*=", "/=" ,"%=" ,"**=", "<<=" ,">>=", ">>>=", "|=" ,"^=" ,"&="];

function remove_node(
code: MagicString,
start: number,
Expand Down Expand Up @@ -1265,10 +1268,18 @@ export default class Component {
}

if (node.type === 'AssignmentExpression') {
extract_identifiers(get_object(node.left)).forEach(node => {
const left = get_object(node.left);

extract_identifiers(left).forEach(node => {
assignee_nodes.add(node);
assignees.add(node.name);
});



if (compoundAssignmentOperators.findIndex(op => op === node.operator) !== -1) {
blackwolf12333 marked this conversation as resolved.
Show resolved Hide resolved
dependencies.add(left.name);
}
} else if (node.type === 'UpdateExpression') {
const identifier = get_object(node.argument);
assignees.add(identifier.name);
Expand Down
27 changes: 27 additions & 0 deletions test/runtime/samples/reactive-compound-operator/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export default {
html: `
<button>+1</button>
<p>count:0</p>
`,

async test({ assert, component, target, window }) {
const click = new window.MouseEvent('click');
const button = target.querySelector('button');

await button.dispatchEvent(click);

assert.equal(component.x, 2);
assert.htmlEqual(target.innerHTML, `
<button>+1</button>
<p>count: 2</p>
`);

await button.dispatchEvent(click);

assert.equal(component.x, 6);
assert.htmlEqual(target.innerHTML, `
<button>+1</button>
<p>count: 6</p>
`);
}
};
8 changes: 8 additions & 0 deletions test/runtime/samples/reactive-compound-operator/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script>
export let x = 0;

$: x *= 2;
</script>

<button on:click='{() => x += 1}'>+1</button>
<p>count: {x}</p>
29 changes: 29 additions & 0 deletions test/runtime/samples/reactive-update-expression/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export default {
html: `
<button>+1</button>
<p>count:1</p>
`,

async test({ assert, component, target, window }) {
const click = new window.MouseEvent('click');
const button = target.querySelector('button');

assert.equal(component.x, 1);

await button.dispatchEvent(click);

assert.equal(component.x, 3);
assert.htmlEqual(target.innerHTML, `
<button>+1</button>
<p>count: 3</p>
`);

await button.dispatchEvent(click);

assert.equal(component.x, 5);
assert.htmlEqual(target.innerHTML, `
<button>+1</button>
<p>count: 5</p>
`);
}
};
12 changes: 12 additions & 0 deletions test/runtime/samples/reactive-update-expression/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<script>
export let x = 0;

$: x++;

function onClick() {
x += 1;
}
</script>

<button on:click='{() => x += 1}'>+1</button>
<p>count: {x}</p>