Skip to content

Commit

Permalink
[feat] improve error message for animate used in a non-keyed each blo…
Browse files Browse the repository at this point in the history
…ck (#6838)

* introdcued new compiler error

* making use of newly created compiler error

* updated test for animation not in keyed each

* removed unneeded conditions
  • Loading branch information
henninggross committed Apr 12, 2022
1 parent 0f94c89 commit 9276f85
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/compiler/compile/compiler_errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ export default {
code: 'invalid-animation',
message: 'An element that uses the animate directive must be the immediate child of a keyed each block'
},
invalid_animation_key: {
code: 'invalid-animation',
message: 'An element that uses the animate directive must be used inside a keyed each block. Did you forget to add a key to your each block?'
},
invalid_animation_sole: {
code: 'invalid-animation',
message: 'An element that uses the animate directive must be the sole child of a keyed each block'
Expand Down
7 changes: 6 additions & 1 deletion src/compiler/compile/nodes/Animation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@ export default class Animation extends Node {
}

const block = parent.parent;
if (!block || block.type !== 'EachBlock' || !block.key) {
if (!block || block.type !== 'EachBlock') {
// TODO can we relax the 'immediate child' rule?
component.error(this, compiler_errors.invalid_animation_immediate);
return;
}

if (!block.key) {
component.error(this, compiler_errors.invalid_animation_key);
return;
}

(block as EachBlock).has_animation = true;

this.expression = info.expression
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[{
"code": "invalid-animation",
"message": "An element that uses the animate directive must be the immediate child of a keyed each block",
"message": "An element that uses the animate directive must be used inside a keyed each block. Did you forget to add a key to your each block?",
"start": {
"line": 6,
"column": 6,
Expand Down

0 comments on commit 9276f85

Please sign in to comment.