Skip to content

Commit

Permalink
disallow binding variables declared in await and catch
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau authored and Conduitry committed Jan 17, 2020
1 parent 1438994 commit 527ddea
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 0 deletions.
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}

0 comments on commit 527ddea

Please sign in to comment.