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

anonymous function as a statement crashes compiler #4617

Closed
getkey opened this issue Mar 31, 2020 · 4 comments · Fixed by #5000
Closed

anonymous function as a statement crashes compiler #4617

getkey opened this issue Mar 31, 2020 · 4 comments · Fixed by #5000
Labels

Comments

@getkey
Copy link
Contributor

getkey commented Mar 31, 2020

Describe the bug
When an anonymous function is created without being assigned, just as a single statement, it crashes the compiler.

() => {}; // like this
function() {}; // or that

This issue appeared in 3.18.2.

Logs

Stack trace
[!] (plugin svelte) TypeError: Cannot read property 'type' of undefined
src/templates/SettingsPopup.svelte
TypeError: Cannot read property 'type' of undefined
    at ExpressionStatement (/mnt/data/devel/xplozzle/node_modules/svelte/node_modules/code-red/dist/code-red.mjs:340:48)
    at handle (/mnt/data/devel/xplozzle/node_modules/svelte/node_modules/code-red/dist/code-red.mjs:70:17)
    at nodes.map.statement (/mnt/data/devel/xplozzle/node_modules/svelte/node_modules/code-red/dist/code-red.mjs:248:18)
    at Array.map (<anonymous>)
    at handle_body (/mnt/data/devel/xplozzle/node_modules/svelte/node_modules/code-red/dist/code-red.mjs:247:21)
    at handlers.BlockStatement.scoped (/mnt/data/devel/xplozzle/node_modules/svelte/node_modules/code-red/dist/code-red.mjs:324:7)
    at /mnt/data/devel/xplozzle/node_modules/svelte/node_modules/code-red/dist/code-red.mjs:226:10
    at handle (/mnt/data/devel/xplozzle/node_modules/svelte/node_modules/code-red/dist/code-red.mjs:70:17)
    at handlers.ArrowFunctionExpression.scoped (/mnt/data/devel/xplozzle/node_modules/svelte/node_modules/code-red/dist/code-red.mjs:837:19)
    at /mnt/data/devel/xplozzle/node_modules/svelte/node_modules/code-red/dist/code-red.mjs:226:10

To Reproduce
https://svelte.dev/repl/574ed4913495492bb1c9580e1b203de8?version=3.20.1

Expected behavior
This useless function must be created but since it's not called, nothing special should happen.

Severity
Honestly it's not very severe. I noticed this issue because I mistakenly forgot to return my anonymous function. I don't really see any reason anyone would want to create an anonymous function and never call it.

However, it's valid JavaScript so it should either be supported or fail with a more explanatory error: "You are making an anonymous function and never calling it. Are you really sure?". But I would argue this is a linter's job, not svelte.
So I think svelte should just support it.

@arxpoetica arxpoetica added bug good first issue A small, encapsulated issue, ideal for a new contributor to Svelte to tackle. labels Mar 31, 2020
@arxpoetica
Copy link
Member

arxpoetica commented Mar 31, 2020

I can confirm this is broken. It appears the breaking change happened between v3.18.1 and v3.18.2:

That should help narrow down what happened between the two versions.

Version compare for ease: 46634e8...c323282

@matthewtole
Copy link

I've been looking into this, and I've tracked down the cause to #4385 which upgraded the version of https://github.com/Rich-Harris/code-red.

I believe the crash is happening in code-red, and specifically this commit is what caused it: Rich-Harris/code-red@2ad7c7a

I don't understand enough about code-red works to fix it, but hoping that this information will help someone else out.

@Conduitry Conduitry removed the good first issue A small, encapsulated issue, ideal for a new contributor to Svelte to tackle. label Apr 3, 2020
@Conduitry
Copy link
Member

Yup, looks like a code-red issue. I've opened Rich-Harris/code-red#38

@Conduitry
Copy link
Member

This is fixed now in 3.23.2 - https://svelte.dev/repl/574ed4913495492bb1c9580e1b203de8?version=3.23.2

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.

4 participants