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

New Rule: check for only one next in router guards

Open NeonWizard opened this issue 4 years ago • 0 comments

Please describe what the rule should do:

From the wiki page:

Make sure that the next function is called exactly once in any given pass through the navigation guard. It can appear more than once, but only if the logical paths have no overlap, otherwise the hook will never be resolved or produce errors.

The linter should make sure that for every beforeEnter, beforeEach, etc, next() can only be called once and is protected by if statements. This will need to be included in Priority A: Essential (Error Prevention).

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:

// BAD
router.beforeEach((to, from, next) => {
  if (to.name !== 'Login' && !isAuthenticated) {
    next({ name: 'Login' })
  }
  // if the user is not authenticated, `next` is called twice
  next()
})
// GOOD
router.beforeEach((to, from, next) => {
  if (to.name !== 'Login' && !isAuthenticated) {
    next({ name: 'Login' })
  } else {
    next()
  }
})

Additional context

Vue wiki page on routers

NeonWizard avatar Mar 11 '21 21:03 NeonWizard