solidjs-community / eslint-plugin-solid

Solid-specific linting rules for ESLint.
MIT License
206 stars 24 forks source link

Is it correct that 'export default createSignal(0)' shows the solid/reactivity warning? #126

Closed Dima-369 closed 6 months ago

Dima-369 commented 6 months ago

I used the code in this tutorial: https://www.solidjs.com/tutorial/stores_nocontext?solved

export default createSignal(0);

It shows this warning: ESLint: For proper analysis, array destructuring should be used to capture the first result of this function call.(solid/reactivity)

I wondered if the warning is correct.

Here is another case where the warning is shown:

function create() {
    return createStore<>([]);
}
export default createRoot(create);

And it goes away like this: (bad code, those aren't the same)

function create() {
    const [get, set] = createStore<>([]);
    return [get, set]
}
export default createRoot(create);

Sorry, if this has been asked before, there is a lot of info on the reactivity warning πŸ˜‰

Dima-369 commented 6 months ago

This code does not trigger the warning. Is this the preferred way of doing this? I am asking since it doesn't use createRoot() which the tutorial sometimes uses.

export const [setTest, getTest] = createSignal('');
joshwilsonvu commented 6 months ago

For proper analysis, array destructuring should be used to capture the first result of this function call.(solid/reactivity)

This is just letting you know that the reactivity rule can't analyze how the signal returned from createSignal is used. Currently, the rule's analysis relies on variables, so returning directly prevents it from working.

This doesn't mean there's anything wrong with your codeβ€”it's just trying to help you make the most out of using the reactivity rule. By pushing you to write code like

function create() {
    const [signal, setSignal] = createSignal(0);
    return [signal, setSignal];
}

you're getting better static analysis in exchange for a little bit more verbosity. In the future, I'm hoping to remove this restriction and handle returning reactive expressions directly.

Dima-369 commented 6 months ago

you're getting better static analysis in exchange for a little bit more verbosity.

Thanks for your answer, then the verbosity is surely worth it!