8000 only warn if the object is mutated · sveltejs/svelte@7d1e9f1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7d1e9f1

Browse files
committed
only warn if the object is mutated
1 parent e5b0136 commit 7d1e9f1

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/** @import { AST } from '#compiler' */
22
/** @import { Context } from '../types' */
33

4-
import { walk } from 'zimmerframe';
54
import { mark_subtree_dynamic } from './shared/fragment.js';
65
import * as w from '../../../warnings.js';
76

@@ -11,16 +10,15 @@ import * as w from '../../../warnings.js';
1110
*/
1211
export function AttachTag(node, context) {
1312
mark_subtree_dynamic(context.path);
13+
14+
context.next({ ...context.state, expression: node.metadata.expression });
15+
1416
if (!context.state.analysis.runes) {
15-
walk(
16-
node.expression,
17-
{},
18-
{
19-
MemberExpression(node) {
20-
w.attachment_legacy_member_access(node);
21-
}
17+
for (const dep of node.metadata.expression.dependencies) {
18+
if (dep.mutated) {
19+
w.attachment_legacy_member_access(node.expression);
20+
break;
2221
}
23-
);
22+
}
2423
}
25-
context.next({ ...context.state, expression: node.metadata.expression });
2624
}
Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
<script>
2+
// mutated, so should warn
23
let state = {
34
count: 0,
4-
attachment(){
5-
6-
}
5+
attachment(){}
6+
};
7+
8+
// unmutated, so should not warn
9+
let attachments = {
10+
foo() {},
11+
bar() {}
712
};
813
</script>
914

1015
<button onclick={()=>{
1116
state.count++;
1217
}}>{state.count}</button>
1318

14-
<div {@attach state.attachment}></div>
19+
<div {@attach state.attachment}></div>
20+
21+
<div {@attach attachments.foo}></div>
22+
<div {@attach attachments.bar}></div>

packages/svelte/tests/validator/samples/attachment-legacy-member-access/warnings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
"code": "attachment_legacy_member_access",
44
"message": "Using `@attach` with a function from an object in legacy mode can cause unnecessary reruns of the attachment function if you mutate that object.",
55
"start": {
6-
"line": 14,
6+
"line": 19,
77
"column": 14
88
},
99
"end": {
10-
"line": 14,
10+
"line": 19,
1111
"column": 30
1212
}
1313
}

0 commit comments

Comments
 (0)
0