eslint-plugin-vue icon indicating copy to clipboard operation
eslint-plugin-vue copied to clipboard

Rule proposal: Allow only one return inside of the `setup`

Open pikax opened this issue 3 years ago • 1 comments

Please describe what the rule should do:

What category should the rule belong to?

[ ] Enforces code style (layout) [x] Warns about a potential error (problem) [ ] Suggests an alternate way of doing something (suggestion) [ ] Other (please specify:)

Provide 2-3 code examples that this rule should warn about:

defineComponent({
 setup(){
   // code
   If (something) {
     // do something
     return; // <- Should warn about this  
   }
    return { /* ... */ }
  }
});

defineComponent({
 setup(){
   // code
 
   If (something) {
     // do something
     return {
       other: 1
     }; // <-- Should warn about this 
   }
   
    return {
      // ...
    }
  }
})

Additional context

If we return multiple times on the setup typescript will infer the setup type to be possible all the returns, the only case where multiple returns might make sense if on SSR (to prevent reactive objects), but even in SSR I would not multiple return.

pikax avatar May 09 '22 15:05 pikax

Yes, you are right that an early return should be avoided in the setup function. It's a potential problem and (even if used correctly) can make the code harder to read.

Actually, it should also be avoided in the data function and Nuxt's asyncData function.

Thus, I suggest the name vue/no-early-return.

FloEdelmann avatar May 13 '22 10:05 FloEdelmann