eslint-community / eslint-plugin-promise

Enforce best practices for JavaScript promises

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Regex patterns for param-names rule

bisubus opened this issue ยท comments

Description

The suggestion is to provide regex pattern options for param-names rule and support underscored names by default which are conventional for unused parameters.

Steps to Reproduce

The cases that I've met more than once is unused resolve and a deferred that needs to avoid shadowing when exposing resolve and reject to parent scope:

/*eslint promise/param-names: "error" */
let timeoutPromise = new Promise((_resolve, reject) => setTimeout(reject));
/*eslint promise/param-names: ["error", { resolvePattern: 'resolve', rejectPattern: 'reject' }] */
let defer = () => {
  let resolve, reject;
  let promise = new Promise((resolveFn, resolveFn) =>{
    resolve = resolveFn;
    reject = rejectFn;
  });
  return { promise, resolve, reject };
};

Expected behavior:

resolve and reject can be named differently for a reason and this should be taken into account by a linter without drawing attention to it.

Actual behavior:

Parameter names are strictly compared against resolve and reject strings, and the rule accepts no additional options to change this.

Is there a chance to get that solved?

In TypeScript, _ is used to indicate unused parameters:
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-0.html#flag-unused-declarations-with---nounusedparameters-and---nounusedlocals

For that use-case, it must be possible to atleast allow "_resolve".

Another use case:
To avoid shadowing I suffix the names with what promise they resolve/reject:

return new Promise((resolve) => {
    const server = app.listen(7576, () => {
      console.log('Trying to start proxy on localhost:7576.....')
      resolve(function teardownArtifactsServer() {
        return new Promise((resolveTeardown, rejectTeardown) => {
          server.close((error) => {
            if (error) {
              rejectTeardown(error)
            } else {
              resolveTeardown()
            }
          })
        })
      })
    })
  })

๐ŸŽ‰ This issue has been resolved in version 6.1.0 ๐ŸŽ‰

The release is available on:

Your semantic-release bot ๐Ÿ“ฆ๐Ÿš€