Skip to content

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants