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(rule): add no-new-statics rule #82

Merged
merged 3 commits into from
Feb 25, 2018

Conversation

macklinu
Copy link
Contributor

@macklinu macklinu commented Feb 5, 2018

Resolves #75

Hoping to continue discussion from #75 to solidify this feature.

For now, I've created a new rule named no-new-statics, which warns if you call any of the Promise static methods with new. I'm open to new naming or including it in another rule, if that would be more appropriate. Feedback appreciated. 😄

Copy link
Contributor Author

@macklinu macklinu left a comment

Choose a reason for hiding this comment

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

Some thoughts 🗣

index.js Outdated
@@ -32,6 +35,7 @@ module.exports = {
'promise/no-promise-in-callback': 'warn',
'promise/no-callback-in-promise': 'warn',
'promise/avoid-new': 'warn',
'promise/no-new-statics': 'warn',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added this rule to the recommended configuration as a warning - does this need adjustment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm thinking this should actually be an error. Playing around in a Node REPL:

❯ node
> new Promise.resolve()
TypeError: Promise.resolve is not a constructor

@@ -2,37 +2,35 @@
* Library: isPromise
* Makes sure that an Expression node is part of a promise.
*/
var STATIC_METHODS = [
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I extracted this array into its own module so that I could use it in multiple files.


'use strict'

const PROMISE_STATICS = require('./promise-statics')
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Open to a better file/module name than promise-statics if you have suggestions!

) {
context.report({
node,
message: 'Avoid calling new on a Promise static method'
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a generic message right now that helps get the point across (I think). Improvement suggestions welcome.

@macklinu macklinu force-pushed the no-new-statics branch 7 times, most recently from 32ac83a to d7ab89e Compare February 12, 2018 14:49
@macklinu macklinu force-pushed the no-new-statics branch 2 times, most recently from 7b84d75 to 40796b5 Compare February 23, 2018 17:15
@macklinu
Copy link
Contributor Author

Ready to merge if CI passes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant