Svelte: Function hoisting is too aggressive

Created on 27 Jun 2019  Â·  4Comments  Â·  Source: sveltejs/svelte

Functions that contain instance data are hoisted; see REPL.

Note that the DOM update code is generated even though the variable never gets invalidated.

Tangentially related to #2731.

bug has pr

Most helpful comment

I don't understand your point. it's working as it should, I mean, you're reassigning get_answer to another function... even with hoisting you're overriding it

All 4 comments

I don't understand your point. it's working as it should, I mean, you're reassigning get_answer to another function... even with hoisting you're overriding it

Yeah, we're still overriding something — but it's the global function, not the per-instance one which is what should happen.

Granted, I believe this is a very low priority bug, but we should eventually get to it.

Plain javascript version to clarify what I mean:

// this is what we write
function component_init(props) {
    function secret() {
        return secret.value;
    }

    secret.value = props.value;
    return secret;
}

// this is how svelte currently hoists functions
function secret() {
    return secret.value;
}

function component_init_transformed(props) {
    secret.value = props.value;
    return secret;
}

const a = component_init(10);
const b = component_init(20);
const c = component_init_transformed(30);
const d = component_init_transformed(40);

console.log(a(), b(), c(), d()); // 10 20 40 40 instead of 10 20 30 40

It looks like this was fixed unless I'm misunderstanding.

REPL

It's now indeed not as aggressive, but when the value escapes our code through function calls it should still be deemed mutable: repl.

And in order to be compliant, getters also need to be considered unsafe, even though that's more of a fringe aspect.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

plumpNation picture plumpNation  Â·  3Comments

1u0n picture 1u0n  Â·  3Comments

thoughtspile picture thoughtspile  Â·  3Comments

bestguy picture bestguy  Â·  3Comments

sskyy picture sskyy  Â·  3Comments