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

"Unexpected token" when expression with function involving return statement is assigned to non-local variable #3038

Closed
trbrc opened this issue Jun 17, 2019 · 1 comment · Fixed by #3158
Labels

Comments

@trbrc
Copy link
Contributor

trbrc commented Jun 17, 2019

I got "Unexpected token (Note that you need plugins to import files that are not JavaScript)" when I did something like this:

<button on:click={event => {
  list = list.map(item => {
    const newItem = bunchaCodeGoesHere();
    return newItem;
  });
}}>
   ...
</button>

After some experimentation to reduce it, the error seems to happen when an event handler has:

  1. an assignment to a non-local variable, and
  2. the assigned expression includes a function, and
  3. the function includes a "return" statement

Example:

https://svelte.dev/repl/a10f29765022492085e8c2805fd095ef?version=3.5.1

Some variations that work:

on:click={() => {
	// No return statement
	f = function(item) {
		// return item + 1;
	};
}}
on:click={() => {
	// No return statement
	f = item => item + 1;
}}
on:click={() => {
	// Function is not part of assigned expression
	function x(item) {
		return item + 1;
	};
	f = x;
}}
on:click={() => {
	// Assignment to local variable
	let f;
	f = function(item) {
		return item + 1;
	};
}}
<script>
	// Hoisted out of template
	let f = function noop() {}
	const click = () => {
		f = function(item) {
			return item + 1;
		};
	}
</script>

<button on:click={click}>
	Assign function
</button>
@trbrc trbrc changed the title "Unexpected token" when assigning of expression with function with return statement to non-local variable "Unexpected token" when expression with function involving return statement is assigned to non-local variable Jun 17, 2019
@Conduitry Conduitry added the bug label Jun 21, 2019
@btk5h
Copy link
Contributor

btk5h commented Jun 22, 2019

Found another requirement for reproducing this error, the return statement must be terminated with a semicolon (wtf?) REPL

This issue is probably related as well, doesn't result in a parse error, but there's also some weirdness with how the $$invalidate call is generated. REPL

Rich-Harris added a commit that referenced this issue Jul 3, 2019
Fix assignments inside inline function expressions
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