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

Reactive statement not found when used inside regular function #3044

Closed
arggh opened this issue Jun 18, 2019 · 2 comments · Fixed by #3101
Closed

Reactive statement not found when used inside regular function #3044

arggh opened this issue Jun 18, 2019 · 2 comments · Fixed by #3101
Labels

Comments

@arggh
Copy link
Contributor

arggh commented Jun 18, 2019

I've run into a situation, where a reactive statement, combined with .map(), is not found when used inside a regular function, but is found when used inside an arrow function:

const someOtherStuff = [1,2,3];

$: stuff = someOtherStuff;

function willFail() {
  stuff.map(i => i);
}

const willWork = () => {
  stuff.map(i => i);
}

When looking at the compiled output, it seems that the compiler hoists the regular function willFail outside of the instance function's scope - that's where the variable for the reactive statement $: stuff resides, and thus it becomes unreachable from the function willFail.

If the reactive statement is assigned on it's own line without chaining a .map, like so:

function willNOTFail() {
  const s = stuff;
  s.map(i => i);
}

...it works as expected and in the compiled output the willNOTFail function is now included inside the scope of instance.

Here is a reproduction in REPL: https://svelte.dev/repl/dde38fd1a7a24d5b84392f24fa933dc8?version=3.5.1

Stack trace

Uncaught ReferenceError: filteredHeros is not defined
at HTMLButtonElement.useFunctionAlert (eval at handle_message (about:srcdoc:13), :499:26)
useFunctionAlert @ VM741:499

@Rich-Harris
Copy link
Member

Fixed in 3.6.0, thanks

@arggh
Copy link
Contributor Author

arggh commented Jun 25, 2019

Great!

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.

3 participants