Skip to content

Commit 6b9b8af

Browse files
fix: ignore comments while comparing nodes in node_match (#9197)
related to issue #9088 it doesn't solve the main problem of dependencies getting invalidated whenever value of a variable gets changed. but it fixes the behavior difference between the code with and without comments
1 parent c1b7262 commit 6b9b8af

File tree

6 files changed

+46
-4
lines changed

6 files changed

+46
-4
lines changed

.changeset/thirty-steaks-dream.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: ignore trailing comments when comparing nodes

packages/svelte/src/compiler/compile/render_dom/invalidate.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export function invalidate(renderer, scope, node, names, main_execution_context
5050
if (
5151
node.type === 'AssignmentExpression' &&
5252
node.operator === '=' &&
53-
nodes_match(node.left, node.right) &&
53+
nodes_match(node.left, node.right, ['trailingComments','leadingComments']) &&
5454
tail.length === 0
5555
) {
5656
return get_invalidated(head, node);

packages/svelte/src/compiler/utils/nodes_match.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export function nodes_match(a, b) {
1+
export function nodes_match(a, b, ignoreKeys=[]) {
22
if (!!a !== !!b) return false;
33
if (Array.isArray(a) !== Array.isArray(b)) return false;
44

@@ -8,8 +8,8 @@ export function nodes_match(a, b) {
88
return a.every((child, i) => nodes_match(child, b[i]));
99
}
1010

11-
const a_keys = Object.keys(a).sort();
12-
const b_keys = Object.keys(b).sort();
11+
const a_keys = Object.keys(a).sort().filter(key => !ignoreKeys.includes(key));
12+
const b_keys = Object.keys(b).sort().filter(key => !ignoreKeys.includes(key));
1313

1414
if (a_keys.length !== b_keys.length) return false;
1515

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<script>
2+
export let objectProp;
3+
let count = 0;
4+
$: objectPropCopy = [...objectProp];
5+
6+
$: incrementCount(), console.log('propDependencyChanged', objectProp);
7+
const incrementCount = () => {
8+
count++;
9+
};
10+
const clickAction = () => {
11+
//note that this file shouldn't be formatted and the trailing comment must be rightnext to the variable name
12+
// prettier-ignore
13+
objectPropCopy = objectPropCopy// trailing comment
14+
};
15+
</script>
16+
17+
<button on:click={clickAction}>click me!</button>
18+
19+
{objectPropCopy}
20+
<div id="render-count">{count}</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export default {
2+
async test({ assert, target, window }) {
3+
const incrementButton = target.querySelector('button');
4+
5+
assert.equal(target.querySelector('#render-count').innerHTML, '1');
6+
await incrementButton.dispatchEvent(new window.MouseEvent('click'));
7+
assert.equal(target.querySelector('#render-count').innerHTML, '2');
8+
}
9+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<script>
2+
import SomeComponent from './SomeComponent.svelte';
3+
let objectProp = ['name', 'age'];
4+
5+
// look in SomeComponent.svelte for explaination
6+
</script>
7+
8+
<SomeComponent {objectProp} />

0 commit comments

Comments
 (0)