Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lint): support object method in noFloatingPromises #5103

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

kaykdm
Copy link
Contributor

@kaykdm kaykdm commented Feb 12, 2025

Summary

#4956
This pull request introduces the support for object methods in noFloatingPromises

Example of invalid code:

calling an async method in the same object using this

const obj = {
  returnsPromiseArrowFunction: async (): Promise<string> => {
    return "value";
  },

  returnsPromiseFunction: async function (): Promise<string> {
    return "value";
  },

  async returnsPromiseMethod(): Promise<string> {
    return "value";
  },

  someMethod() {
    this.returnsPromiseArrowFunction();  // diagnostic here
    this.returnsPromiseFunction().then(() => {});  // diagnostic here
    this.returnsPromiseMethod();  // diagnostic here
  }
};

calling an async method from outside

const obj = {
  returnsPromiseArrowFunction: async (): Promise<string> => {
    return "value";
  },

  returnsPromiseFunction: async function (): Promise<string> {
    return "value";
  },

  async returnsPromiseMethod(): Promise<string> {
    return "value";
  },
};

obj.returnsPromiseArrowFunction();  // diagnostic here
obj.returnsPromiseFunction().then(() => {});  // diagnostic here
obj['returnsPromiseMethod']();  // diagnostic here

Example of valid code:

const obj = {
  returnsPromiseArrowFunction: async (): Promise<string> => {
    return "value";
  },

  returnsPromiseFunction: async function (): Promise<string> {
    return "value";
  },

  async returnsPromiseMethod(): Promise<string> {
    return "value";
  },

  async someMethod() {
    await this.returnsPromiseArrowFunction();
    this.returnsPromiseFunction().then(() => {}, () => {});
    this.returnsPromiseMethod().catch(() => {});
  }
};

await obj.returnsPromiseArrowFunction();
await obj['returnsPromiseFunction']();

@ematipico ematipico changed the base branch from next to main February 12, 2025 11:47
@github-actions github-actions bot added A-Linter Area: linter L-JavaScript Language: JavaScript and super languages labels Feb 12, 2025
@kaykdm kaykdm force-pushed the support-object-method branch from fce2c2a to 63d315a Compare February 12, 2025 12:03
@kaykdm kaykdm marked this pull request as ready for review February 12, 2025 12:09
Copy link

codspeed-hq bot commented Feb 12, 2025

CodSpeed Performance Report

Merging #5103 will not alter performance

Comparing kaykdm:support-object-method (43745b2) with main (bca10f5)

Summary

✅ 94 untouched benchmarks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Linter Area: linter L-JavaScript Language: JavaScript and super languages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant