Skip to content

Commit fc896a7

Browse files
committed
fix: add warning when you read object property within attachments in legacy mode
1 parent 42e7e81 commit fc896a7

File tree

7 files changed

+65
-0
lines changed

7 files changed

+65
-0
lines changed

.changeset/breezy-pears-punch.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: add warning when you read object property within attachments in legacy mode

documentation/docs/98-reference/.generated/compile-warnings.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,12 @@ Elements with ARIA roles must use a valid, non-abstract ARIA role. A reference t
556556
<div role="toooltip"></div>
557557
```
558558

559+
### attachment_legacy_member_access
560+
561+
```
562+
Using `@attach` with a function from an object in legacy mode can cause unnecessary reruns of the attachment function if you mutate that object.
563+
```
564+
559565
### attribute_avoid_is
560566

561567
```

packages/svelte/messages/compile-warnings/template.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## attachment_legacy_member_access
2+
3+
> Using `@attach` with a function from an object in legacy mode can cause unnecessary reruns of the attachment function if you mutate that object.
4+
15
## attribute_avoid_is
26

37
> The "is" attribute is not supported cross-browser and should be avoided
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
/** @import { AST } from '#compiler' */
22
/** @import { Context } from '../types' */
33

4+
import { walk } from 'zimmerframe';
45
import { mark_subtree_dynamic } from './shared/fragment.js';
6+
import * as w from '../../../warnings.js';
57

68
/**
79
* @param {AST.AttachTag} node
810
* @param {Context} context
911
*/
1012
export function AttachTag(node, context) {
1113
mark_subtree_dynamic(context.path);
14+
if (!context.state.analysis.runes) {
15+
walk(
16+
node.expression,
17+
{},
18+
{
19+
MemberExpression(node) {
20+
w.attachment_legacy_member_access(node);
21+
}
22+
}
23+
);
24+
}
1225
context.next({ ...context.state, expression: node.metadata.expression });
1326
}

packages/svelte/src/compiler/warnings.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ export const codes = [
106106
'state_referenced_locally',
107107
'store_rune_conflict',
108108
'css_unused_selector',
109+
'attachment_legacy_member_access',
109110
'attribute_avoid_is',
110111
'attribute_global_event_reference',
111112
'attribute_illegal_colon',
@@ -677,6 +678,14 @@ export function css_unused_selector(node, name) {
677678
w(node, 'css_unused_selector', `Unused CSS selector "${name}"\nhttps://svelte.dev/e/css_unused_selector`);
678679
}
679680

681+
/**
682+
* Using `@attach` with a function from an object in legacy mode can cause unnecessary reruns of the attachment function if you mutate that object.
683+
* @param {null | NodeLike} node
684+
*/
685+
export function attachment_legacy_member_access(node) {
686+
w(node, 'attachment_legacy_member_access', `Using \`@attach\` with a function from an object can cause unnecessary reruns of the attachment function if you mutate that object in legacy mode.\nhttps://svelte.dev/e/attachment_legacy_member_access`);
687+
}
688+
680689
/**
681690
* The "is" attribute is not supported cross-browser and should be avoided
682691
* @param {null | NodeLike} node
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<script>
2+
let state = {
3+
count: 0,
4+
attachment(){
5+
6+
}
7+
};
8+
</script>
9+
10+
<button onclick={()=>{
11+
state.count++;
12+
}}>{state.count}</button>
13+
14+
<div {@attach state.attachment}></div>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"code": "attachment_legacy_member_access",
4+
"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.",
5+
"start": {
6+
"line": 14,
7+
"column": 14
8+
},
9+
"end": {
10+
"line": 14,
11+
"column": 30
12+
}
13+
}
14+
]

0 commit comments

Comments
 (0)