sveltejs / eslint-plugin-svelte

ESLint plugin for Svelte using AST
https://sveltejs.github.io/eslint-plugin-svelte/
MIT License
277 stars 30 forks source link

Rule Request: writable.update() should always return a value #682

Open tivac opened 4 months ago

tivac commented 4 months ago

Motivation

If you use the .update(...) method on a writable store and don't explicitly return a value the store will be set to undefined. That might be on purpose, but I'm willing to bet that more often than not it's accidental.

Description

The rule should look for .update() calls that match the signature of writable.update() and ensure that all code paths return some value. Doesn't have to the be the original value or anything specific, I just think being able to require the usage of return <thing>; to make it really explicit that a value is being set would be useful and prevent errors.

Examples

<script>
const store = writable(false);

// ✓ GOOD
store.update(($store) => true);

store.update(($store) => {
    return true;
});

store.update(($store) => {
    if(foo) {
        // ...
        return bar;
    }

    return true;
});

// ✗ BAD
store.update(($store) => {
    // ...
});

store.update(($store) => {
    if(foo) {
        // ...
        return bar;
    }
});
</script>

Additional comments

No response

ota-meshi commented 4 months ago

Thank you for posting rule suggestion! That rule sounds good to me!