8000 fix: add warning when you read object property within attachments in legacy mode by paoloricciuti · Pull Request #15948 · sveltejs/svelte · GitHub
[go: up one dir, main page]

Skip to content

fix: add warning when you read object property within attachments in legacy mode #15948

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions .changeset/breezy-pears-punch.md
8000
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: add warning when you read object property within attachments in legacy mode
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,12 @@ Elements with ARIA roles must use a valid, non-abstract ARIA role. A reference t
<div role="toooltip"></div>
```

### attachment_legacy_member_access

```
Using `@attach` with a function from an object in legacy mode can cause unnecessary reruns of the attachment function if you mutate that object.
```

### attribute_avoid_is

```
Expand Down
4 changes: 4 additions & 0 deletions packages/svelte/messages/compile-warnings/template.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## attachment_legacy_member_access

> Using `@attach` with a function from an object in legacy mode can cause unnecessary reruns of the attachment function if you mutate that object.

## attribute_avoid_is

> The "is" attribute is not supported cross-browser and should be avoided
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,23 @@
/** @import { Context } from '../types' */

import { mark_subtree_dynamic } from './shared/fragment.js';
import * as w from '../../../warnings.js';

/**
* @param {AST.AttachTag} node
* @param {Context} context
*/
export function AttachTag(node, context) {
mark_subtree_dynamic(context.path);

context.next({ ...context.state, expression: node.metadata.expression });

if (!context.state.analysis.runes) {
for (const dep of node.metadata.expression.dependencies) {
if (dep.mutated) {
w.attachment_legacy_member_access(node.expression);
break;
}
}
}
}
9 changes: 9 additions & 0 deletions packages/svelte/src/compiler/warnings.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export const codes = [
'state_referenced_locally',
'store_rune_conflict',
'css_unused_selector',
'attachment_legacy_member_access',
'attribute_avoid_is',
'attribute_global_event_reference',
'attribute_illegal_colon',
Expand Down Expand Up @@ -677,6 +678,14 @@ export function css_unused_selector(node, name) {
w(node, 'css_unused_selector', `Unused CSS selector "${name}"\nhttps://svelte.dev/e/css_unused_selector`);
}

/**
* Using `@attach` with a function from an object in legacy mode can cause unnecessary reruns of the attachment function if you mutate that object.
* @param {null | NodeLike} node
*/
export function attachment_legacy_member_access(node) {
w(node, 'attachment_legacy_member_access', `Using \`@attach\` with a function from an object in legacy mode can cause unnecessary reruns of the attachment function if you mutate that object.\nhttps://svelte.dev/e/attachment_legacy_member_access`);
}

/**
* The "is" attribute is not supported cross-browser and should be avoided
* @param {null | NodeLike} node
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<script>
// mutated, so should warn
let state = {
count: 0,
attachment(){}
};

// unmutated, so should not warn
let attachments = {
foo() {},
bar() {}
};
</script>

<button onclick={()=>{
state.count++;
}}>{state.count}</button>

<div {@attach state.attachment}></div>

<div {@attach attachments.foo}></div>
<div {@attach attachments.bar}></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"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": 19,
"column": 14
< 46E9 /td> },
"end": {
"line": 19,
"column": 30
}
}
]
0