Skip to content
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/sharp-rings-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

generate error when $props.id() is after an await
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ Cannot use `%rune%()` more than once
### props_id_invalid_placement

```
`$props.id()` can only be used at the top level of components as a variable declaration initializer
`$props.id()` can only be used at the top level of components as a variable declaration initializer, and before any `await` expression.
```

### props_illegal_name
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/messages/compile-errors/script.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ This turned out to be buggy and unpredictable, particularly when working with de

## props_id_invalid_placement

> `$props.id()` can only be used at the top level of components as a variable declaration initializer
> `$props.id()` can only be used at the top level of components as a variable declaration initializer, and before any `await` expression.

## props_illegal_name

Expand Down
4 changes: 2 additions & 2 deletions packages/svelte/src/compiler/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,12 @@ export function props_duplicate(node, rune) {
}

/**
* `$props.id()` can only be used at the top level of components as a variable declaration initializer
* `$props.id()` can only be used at the top level of components as a variable declaration initializer, and before any `await` expression.
* @param {null | number | NodeLike} node
* @returns {never}
*/
export function props_id_invalid_placement(node) {
e(node, 'props_id_invalid_placement', `\`$props.id()\` can only be used at the top level of components as a variable declaration initializer\nhttps://svelte.dev/e/props_id_invalid_placement`);
e(node, 'props_id_invalid_placement', `\`$props.id()\` can only be used at the top level of components as a variable declaration initializer, and before any \`await\` expression.\nhttps://svelte.dev/e/props_id_invalid_placement`);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ export function CallExpression(node, context) {
e.props_id_invalid_placement(node);
}

// $props.id() should not be on the async part of the body :
if (context.state.analysis.instance_body.async.find((a) => a.node === parent)) {
e.props_id_invalid_placement(node);
}

if (node.arguments.length > 0) {
e.rune_invalid_arguments(node, rune);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { test } from '../../test';

export default test({
error: {
code: 'props_id_invalid_placement',
message:
'`$props.id()` can only be used at the top level of components as a variable declaration initializer, and before any `await` expression.'
},
async: true
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<script>

async function getData() {
return 0;
}

const v = await getData();
const uid = $props.id();
</script>
Loading