only warn if the object is mutated

warn-attachments-legacy-member-access
Rich Harris 4 months ago
parent e5b0136ad1
commit 7d1e9f168e

@ -1,7 +1,6 @@
/** @import { AST } from '#compiler' */
/** @import { Context } from '../types' */
import { walk } from 'zimmerframe';
import { mark_subtree_dynamic } from './shared/fragment.js';
import * as w from '../../../warnings.js';
@ -11,16 +10,15 @@ import * as w from '../../../warnings.js';
*/
export function AttachTag(node, context) {
mark_subtree_dynamic(context.path);
context.next({ ...context.state, expression: node.metadata.expression });
if (!context.state.analysis.runes) {
walk(
node.expression,
{},
{
MemberExpression(node) {
w.attachment_legacy_member_access(node);
}
for (const dep of node.metadata.expression.dependencies) {
if (dep.mutated) {
w.attachment_legacy_member_access(node.expression);
break;
}
);
}
}
context.next({ ...context.state, expression: node.metadata.expression });
}

@ -1,9 +1,14 @@
<script>
// mutated, so should warn
let state = {
count: 0,
attachment(){
attachment(){}
};
}
// unmutated, so should not warn
let attachments = {
foo() {},
bar() {}
};
</script>
@ -12,3 +17,6 @@
}}>{state.count}</button>
<div {@attach state.attachment}></div>
<div {@attach attachments.foo}></div>
<div {@attach attachments.bar}></div>

@ -3,11 +3,11 @@
"code": "attachment_legacy_member_access",
"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.",
"start": {
"line": 14,
"line": 19,
"column": 14
},
"end": {
"line": 14,
"line": 19,
"column": 30
}
}

Loading…
Cancel
Save