solidjs-community / eslint-plugin-solid

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

Unnecessarily warning solid/reactivity for store setter function call #97

Closed sveinnthorarins closed 1 year ago

sveinnthorarins commented 1 year ago

Describe the bug

Linter warns about solid/reactivity when calling a SetStoreFunction function outside tracked scope.

To Reproduce

I'm using plugin:solid/typescript in ESLint config.

The code that produces the warning:

// other irrelevant types: GameLogLine, GameLogLineModifiers, GameLogAppender

type GameProps = {
    gameLogic: {
        executeGuessAsync: (value: string, callback?: (result: boolean) => void) => void
    };
    log: GameLogLine[];
    appendLog: GameLogAppender;
    setLog: SetStoreFunction<GameLogLine[]>;
};

export default function Game(props: GameProps) {
    let guessInput: HTMLInputElement | undefined;
    function guess() {
        if (!guessInput || !guessInput.value) return;
        const val = guessInput.value;
        const id = props.appendLog(val, GameLogLineModifiers.Private);
        props.gameLogic.executeGuessAsync(val, (res) => { // This function should be passed to a tracked scope... [solid/reactivity]
            const mod = res ? GameLogLineModifiers.Correct : GameLogLineModifiers.Incorrect;
            // props.setLog is of type SetStoreFunction<GameLogLine[]>
            props.setLog((line) => line.id === id, 'modifiers', mod);
        });
    }

    return (
        <>
            {/* other JSX removed for simplicity, including the elements displaying the log (props.log) */}
            <input type="text" ref={guessInput} />
            <button onClick={guess}>Guess</button>
        </>
    );
}

Expected behavior

No warning.

This is an unnecessary warning and instigates confusion. At least to my knowledge, it is okay to call setters outside tracked scope, the getters are the ones that need tracking. Please correct me if mistaken.

Environment (please complete the following information):

Additional context

sveinnthorarins commented 1 year ago

Ahh, the solid/reactivity warning happens because I'm accessing the setter through props and props are considered reactive. My mistake.