Skip to content
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

disallow binding variables declared in await and catch #4275

Merged
Merged
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
7 changes: 7 additions & 0 deletions src/compiler/compile/nodes/Binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ export default class Binding extends Node {
message: 'Cannot bind to a variable declared with the let: directive'
});
} else if (this.is_contextual) {
if (scope.is_await(name)) {
component.error(this, {
code: 'invalid-binding',
message: 'Cannot bind to a variable declared with {#await ... then} or {:catch} blocks'
});
}

scope.dependencies_for_name.get(name).forEach(name => {
const variable = component.var_lookup.get(name);
if (variable) {
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/compile/nodes/shared/TemplateScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ export default class TemplateScope {
const owner = this.get_owner(name);
return owner && (owner.type === 'Element' || owner.type === 'InlineComponent');
}

is_await(name: string) {
const owner = this.get_owner(name);
return owner && (owner.type === 'ThenBlock' || owner.type === 'CatchBlock');
}
}
9 changes: 9 additions & 0 deletions test/validator/samples/binding-await-catch/errors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"code": "invalid-binding",
"message": "Cannot bind to a variable declared with {#await ... then} or {:catch} blocks",
"pos": 79,
"start": { "line": 6, "column": 9, "character": 79 },
"end": { "line": 6, "column": 27, "character": 97 }
}
]
7 changes: 7 additions & 0 deletions test/validator/samples/binding-await-catch/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
let promise = 0;
</script>
{#await promise}
{:catch error}
<input bind:value={error} />
{/await}
9 changes: 9 additions & 0 deletions test/validator/samples/binding-await-then-2/errors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"code": "invalid-binding",
"message": "Cannot bind to a variable declared with {#await ... then} or {:catch} blocks",
"pos": 78,
"start": { "line": 6, "column": 9, "character": 78 },
"end": { "line": 6, "column": 19, "character": 88 }
}
]
7 changes: 7 additions & 0 deletions test/validator/samples/binding-await-then-2/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
let promise = 0;
</script>
{#await promise}
{:then value}
<input bind:value />
{/await}
9 changes: 9 additions & 0 deletions test/validator/samples/binding-await-then/errors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"code": "invalid-binding",
"message": "Cannot bind to a variable declared with {#await ... then} or {:catch} blocks",
"pos": 75,
"start": { "line": 5, "column": 9, "character": 75 },
"end": { "line": 5, "column": 19, "character": 85 }
}
]
6 changes: 6 additions & 0 deletions test/validator/samples/binding-await-then/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<script>
let promise = 0;
</script>
{#await promise then value}
<input bind:value />
{/await}