Skip to content
New issue

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

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

Already on GitHub? # to your account

feat(rule): add prefer-promise-static-methods rule #473

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

NotWoods
Copy link

What is the purpose of this pull request?

  • Documentation update
  • Bug fix
  • New rule
  • Changes an existing rule
  • Add autofixing to a rule
  • Other, please explain:

What changes did you make? (Give an overview)
Closes #448

Rule to recommend using Promise static resolve/reject methods over new Promise, which saves a microtick and is more readable. See issue for more details on the rule.

function reportIfIsPromiseCall(callNode, constructorNode, parameterNames) {
if (
callNode.callee.type === 'Identifier' &&
callNode.arguments.length <= 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about new Promise(resolve => resolve(itCanThrowAnError())) (the case of Promise.try)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I'm not sure what the best approach there would be. (Also I didn't realize new Promise is same tick, I must've misread the spec.) Perhaps just show a suggestion instead of an autofix?

Copy link
Contributor

@zloirock zloirock May 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that cases that could throw an error should be ignored (however, it's too many possible cases - I think that it's OK to ignore only call and new). Also, can be added an option to check such cases, without autofixes.

@voxpelli voxpelli changed the title Add prefer-promise-static-methods rule feat: add prefer-promise-static-methods rule Jul 24, 2024
@voxpelli voxpelli changed the title feat: add prefer-promise-static-methods rule feat(rule): add prefer-promise-static-methods rule Jul 24, 2024
@voxpelli voxpelli changed the title feat(rule): add prefer-promise-static-methods rule feat(rule): add prefer-promise-static-methods rule Jul 24, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New Rule: Prefer Promise.resolve(foo) to new Promise((resolve) => resolve(foo))
3 participants