Skip to content

Latest commit

 

History

History
104 lines (80 loc) · 3.15 KB

no-restricted-floating-promises.md

File metadata and controls

104 lines (80 loc) · 3.15 KB

Enforce Promises with specified syntax to be handled appropriately (no-restricted-floating-promises)

A "floating" Promise is one that is created without any code set up to handle any errors it might throw.

Floating Promises can cause several issues, such as improperly sequenced operations, ignored Promise rejections, and more.

This rule is similar to @typescript-eslint/no-floating-promises, except that it only checks Promise expressions with the specified syntax.

Options

This rule has a list of string or object options. Each option can be:

  • An AST Selector string
  • An object with the following properties:
    • type: Predefined selector, such as vuex-action, element-message-box or vant-dialog
    • selector: An AST Selector string
    • paths: An array of ignore pattern, meaning that promises generated by functions imported from specified modules will be checked
    • message: Custom reporting message

Fail

/* eslint galaxy/no-restricted-floating-promises: ["error", "CallExpression[callee.name='foo']"] */
foo()
/* eslint galaxy/no-restricted-floating-promises: ["error", "CallExpression[callee.name='foo']"] */
Promise.all([foo()])
/* eslint galaxy/no-restricted-floating-promises: ["error", "CallExpression[callee.name='foo']"] */
Promise.resolve(foo())
/* eslint galaxy/no-restricted-floating-promises: ["error", "CallExpression[callee.name='foo']"] */
foo().then(() => {})
/* eslint galaxy/no-restricted-floating-promises: ["error", { "type": "vuex-action" }] */
this.$store.dispatch('submit')
/* eslint galaxy/no-restricted-floating-promises: ["error", { "type": "element-message-box" }] */
this.$confirm({})
/* eslint galaxy/no-restricted-floating-promises: ["error", { "type": "vant-dialog" }] */
this.$dialog.confirm({})
/* eslint galaxy/no-restricted-floating-promises: ["error", { "names": ["foo"] }] */
foo()
/* eslint galaxy/no-restricted-floating-promises: ["error", { "paths": ["foo"] }] */
import { foo } from 'foo'
foo()

Pass

/* eslint galaxy/no-restricted-floating-promises: ["error", "CallExpression[callee.name='foo']"] */
bar()
/* eslint galaxy/no-restricted-floating-promises: ["error", "CallExpression[callee.name='foo']"] */
foo().catch(() => {})
/* eslint galaxy/no-restricted-floating-promises: ["error", "CallExpression[callee.name='foo']"] */
foo().then(() => {}, () => {})
/* eslint galaxy/no-restricted-floating-promises: ["error", "CallExpression[callee.name='foo']"] */
await foo()
/* eslint galaxy/no-restricted-floating-promises: ["error", "CallExpression[callee.name='foo']"] */
wrap(foo())
/* eslint galaxy/no-restricted-floating-promises: ["error", { "names": ["foo"] }] */
bar()
/* eslint galaxy/no-restricted-floating-promises: ["error", { "paths": ["foo"] }] */
import { foo } from 'bar'
foo()