Skip to content

Commit

Permalink
feat(rule): add no-new-statics rule
Browse files Browse the repository at this point in the history
  • Loading branch information
macklinu committed Feb 23, 2018
1 parent 3e674c3 commit 7b84d75
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 3 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,21 @@ Enforce best practices for JavaScript promises.

* [Installation](#installation)
* [Usage](#usage)
* [Rules](#rules)
* [Rules](#rules) <<<<<<< HEAD =======
* [`catch-or-return`](#catch-or-return)
* [`no-return-wrap`](#no-return-wrap)
* [`param-names`](#param-names)
* [`always-return`](#always-return)
* [`no-native`](#no-native)
* [`no-nesting`](#no-nesting)
* [`no-promise-in-callback`](#no-promise-in-callback)
* [`no-callback-in-promise`](#no-callback-in-promise)
* [`avoid-new`](#avoid-new)
* [`no-new-statics`](#no-new-statics)
* [`no-return-in-finally`](#no-return-in-finally)
* [`prefer-await-to-then`](#prefer-await-to-then)
* [`prefer-await-to-callbacks`](#prefer-await-to-callbacks)
> > > > > > > feat(rule): add no-new-statics rule
* [Maintainers](#maintainers)
* [License](#license)

Expand Down Expand Up @@ -61,6 +75,7 @@ Then configure the rules you want to use under the rules section.
"promise/no-promise-in-callback": "warn",
"promise/no-callback-in-promise": "warn",
"promise/avoid-new": "warn",
"promise/no-new-statics": "warn",
"promise/no-return-in-finally": "warn"
}
}
Expand All @@ -87,6 +102,7 @@ or start with the recommended rule set
| [`no-promise-in-callback`][no-promise-in-callback] | Avoid using promises inside of callbacks | :warning: | |
| [`no-callback-in-promise`][no-callback-in-promise] | Avoid calling `cb()` inside of a `then()` (use [nodeify][] instead) | :warning: | |
| [`avoid-new` ][avoid-new] | Avoid creating `new` promises outside of utility libs (use [pify][] instead) | :warning: | |
| [`no-new-statics`][no-new-statics] | Avoid calling `new` on a Promise static method (e.g. `new Promise.resolve()`) | :bangbang: | |
| [`no-return-in-finally`][no-return-in-finally] | Disallow return statements in `finally()` | :warning: | |
| [`prefer-await-to-then`][prefer-await-to-then] | Prefer `await` to `then()` for reading Promise values | :seven: | |
| [`prefer-await-to-callbacks`][prefer-await-to-callbacks] | Prefer async/await to the callback pattern | :seven: | |
Expand Down Expand Up @@ -119,6 +135,7 @@ or start with the recommended rule set
[no-promise-in-callback]: docs/rules/no-promise-in-callback.md
[no-callback-in-promise]: docs/rules/no-callback-in-promise.md
[avoid-new]: docs/rules/avoid-new.md
[no-new-statics]: docs/rules/no-new-statics.md
[no-return-in-finally]: docs/rules/no-return-in-finally.md
[prefer-await-to-then]: docs/rules/prefer-await-to-then.md
[prefer-await-to-callbacks]: docs/rules/prefer-await-to-callbacks.md
Expand Down
1 change: 1 addition & 0 deletions docs/rules/no-new-statics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Avoid calling `new` on a Promise static method (e.g. `new Promise.resolve()`) (no-new-statics)
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
'no-promise-in-callback': require('./rules/no-promise-in-callback'),
'no-nesting': require('./rules/no-nesting'),
'avoid-new': require('./rules/avoid-new'),
'no-new-statics': require('./rules/no-new-statics'),
'no-return-in-finally': require('./rules/no-return-in-finally')
},
rulesConfig: {
Expand All @@ -34,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': 'error',
'promise/no-return-in-finally': 'warn'
}
}
Expand Down
4 changes: 2 additions & 2 deletions rules/lib/is-promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
'use strict'

const STATIC_METHODS = ['all', 'race', 'reject', 'resolve']
const PROMISE_STATICS = require('./promise-statics')

function isPromise(expression) {
return (
Expand All @@ -29,7 +29,7 @@ function isPromise(expression) {
expression.callee.type === 'MemberExpression' &&
expression.callee.object.type === 'Identifier' &&
expression.callee.object.name === 'Promise' &&
STATIC_METHODS.indexOf(expression.callee.property.name) !== -1)
PROMISE_STATICS.indexOf(expression.callee.property.name) !== -1)
)
}

Expand Down
3 changes: 3 additions & 0 deletions rules/lib/promise-statics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict'

module.exports = ['all', 'race', 'reject', 'resolve']
28 changes: 28 additions & 0 deletions rules/no-new-statics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict'

const PROMISE_STATICS = require('./lib/promise-statics')
const getDocsUrl = require('./lib/get-docs-url')

module.exports = {
meta: {
docs: {
url: getDocsUrl('no-new-statics')
}
},
create(context) {
return {
NewExpression(node) {
if (
node.callee.type === 'MemberExpression' &&
node.callee.object.name === 'Promise' &&
PROMISE_STATICS.indexOf(node.callee.property.name) > -1
) {
context.report({
node,
message: 'Avoid calling new on a Promise static method'
})
}
}
}
}
}
26 changes: 26 additions & 0 deletions test/no-new-statics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict'

const rule = require('../rules/no-new-statics')
const RuleTester = require('eslint').RuleTester
const ruleTester = new RuleTester()

const message = 'Avoid calling new on a Promise static method'

ruleTester.run('no-new-statics', rule, {
valid: [
'Promise.resolve()',
'Promise.reject()',
'Promise.all()',
'Promise.race()',
'new Promise(function (resolve, reject) {})',
'new SomeClass()',
'SomeClass.resolve()',
'new SomeClass.resolve()'
],
invalid: [
{ code: 'new Promise.resolve()', errors: [{ message }] },
{ code: 'new Promise.reject()', errors: [{ message }] },
{ code: 'new Promise.all()', errors: [{ message }] },
{ code: 'new Promise.race()', errors: [{ message }] }
]
})

0 comments on commit 7b84d75

Please sign in to comment.