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

References to each index in keying expression seen as undeclared #3274

Closed
Conduitry opened this issue Jul 21, 2019 · 1 comment · Fixed by #3336
Closed

References to each index in keying expression seen as undeclared #3274

Conduitry opened this issue Jul 21, 2019 · 1 comment · Fixed by #3336
Labels

Comments

@Conduitry
Copy link
Member

From sveltejs/eslint-plugin-svelte3#37

Something like

{#each foo as bar, i (i)}hey{/each}

produces an erroneous warning that i is not defined. The key seems to be created correctly from the each block's ctx.i - there's just an incorrect warning from the compiler about it.

@Conduitry Conduitry added the bug label Jul 21, 2019
@Conduitry
Copy link
Member Author

Conduitry commented Aug 3, 2019

My first stab at a solution for this was to simply add the EachBlock's index to its scope before creating the Expression for its key:

diff --git a/src/compiler/compile/nodes/EachBlock.ts b/src/compiler/compile/nodes/EachBlock.ts
index adaa46b8..12e6cccb 100644
--- a/src/compiler/compile/nodes/EachBlock.ts
+++ b/src/compiler/compile/nodes/EachBlock.ts
@@ -83,16 +83,16 @@ export default class EachBlock extends AbstractBlock {
 			this.scope.add(context.key.name, this.expression.dependencies, this);
 		});
 
-		this.key = info.key
-			? new Expression(component, this, this.scope, info.key)
-			: null;
-
 		if (this.index) {
 			// index can only change if this is a keyed each block
 			const dependencies = this.key ? this.expression.dependencies : new Set([]);
 			this.scope.add(this.index, dependencies, this);
 		}
 
+		this.key = info.key
+			? new Expression(component, this, this.scope, info.key)
+			: null;
+
 		this.has_animation = false;
 
 		this.children = map_children(component, this, this.scope, info.children);

but this is making the each-block-keyed and each-block-keyed-object-identity runtime tests fail and I'm not sure why.


Edit: Whoops, the this.index handling was looking at this.key to see whether there was a key set. With the above change, this of course needs to be looking at info.key instead. With that, there are no failing tests.

Conduitry added a commit to Conduitry/svelte that referenced this issue Aug 3, 2019
Rich-Harris added a commit that referenced this issue Aug 3, 2019
don't warn when using each index in key
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant