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

ctx[_] is not a function error with @const's named func referenced inside lambda expressions #8753

Closed
aspiringLich opened this issue Jun 18, 2023 · 0 comments · Fixed by #8840

Comments

@aspiringLich
Copy link

aspiringLich commented Jun 18, 2023

Describe the bug

An error that occurs with (as far as I can tell) lambda functions inside curly braces in the markup, that reference an {@const} named func. This causes a conflict between generating a function for the lambda (internally called func), and the variable func. It then (I assume) incorrectly thinks the lambda function is the same as the @const. It does not apply to regular variables named func, only {@const} variables.

Reproduction

I'll save you the painful process of cutting everything down: here's the simplest scenario I could find that showcases the weird error.

{#if true}
    {@const func = [12, 13, 14]}

    {(() => JSON.stringify(func))()} <!-- Error: ctx[0] is not a function -->
{/if}

It's specifically {@const func = _} that causes the error, every other identifier i tried worked AOK. Which was very fun to figure out. I think this is because when I look at the generated javascript:

// (1:0) {#if true}
function create_if_block(ctx) {
    let t_value = func() + "";
    let t;

    function func() {
        return /*func*/ ctx[0](/*func*/ ctx[0]);
    }
    // ...
}

ctx[0] shows up twice, once for the "const variable", and once for the function being referenced. So yes, ctx[0] is not a function its a variable. That makes sense I guess.

So to recap:

  • You need an @const variable named func
  • You need to reference it inside a lambda in the markup
  • The generated code will confuse the lambda and the @const variable, replacing the lambda with a call to the @const.

Things finally make sense you do not know how weird this error was when I first discovered it.

This error probably shouldn't be terribly high priority but it also should be pretty easy to fix I think.

Logs

No response

System Info

As far as I can tell it's completely platform independent, tested it on my machine and the online REPL, buuuut...


  System:
    OS: Linux 6.3 Fedora Linux 37 (KDE Plasma)
    CPU: (16) x64 12th Gen Intel(R) Core(TM) i7-1260P
    Memory: 4.90 GB / 15.32 GB
    Container: Yes
    Shell: 3.6.1 - /usr/bin/fish
  Binaries:
    Node: 18.16.0 - /usr/bin/node
    npm: 9.5.1 - /usr/local/bin/npm
  Browsers:
    Chromium: 114.0.5735.106
    Firefox: 114.0
  npmPackages:
    svelte: ^3.55.1 => 3.59.1 


### Severity

annoyance
@aspiringLich aspiringLich changed the title ctx[_] is undefined error with @const's named func referenced inside lambda expressions ctx[_] is not a function error with @const's named func referenced inside lambda expressions Jun 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant