Skip to content

Commit

Permalink
feat(rule): add no-new-statics rule
Browse files Browse the repository at this point in the history
Resolves #75
  • Loading branch information
macklinu committed Feb 12, 2018
1 parent c0c662a commit d7ab89e
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Enforce best practices for JavaScript promises.
* [`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)
Expand Down Expand Up @@ -73,6 +74,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 @@ -99,6 +101,7 @@ or start with the recommended rule set
| `no-promise-in-callback` | Avoid using promises inside of callbacks | :warning: | |
| `no-callback-in-promise` | Avoid calling `cb()` inside of a `then()` (use [nodeify][] instead) | :warning: | |
| `avoid-new` | Avoid creating `new` promises outside of utility libs (use [pify][] instead) | :warning: | |
| `no-new-statics` | Avoid calling `new` on a Promise static method (e.g. `new Promise.resolve()`) | :bangbang: | |
| `no-return-in-finally` | Disallow return statements in `finally()` | :warning: | |
| `prefer-await-to-then` | Prefer `await` to `then()` for reading Promise values | :seven: | |
| `prefer-await-to-callbacks` | Prefer async/await to the callback pattern | :seven: | |
Expand Down Expand Up @@ -309,6 +312,10 @@ Avoid calling `cb()` inside of a `then()` (use [nodeify][] instead)

Avoid creating `new` promises outside of utility libs (use [pify][] instead)

### `no-new-statics`

Avoid calling `new` on a Promise static method (e.g. `new Promise.resolve()`)

### `no-return-in-finally`

Disallow return statements inside a callback passed to `finally()`, since
Expand Down
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']
27 changes: 27 additions & 0 deletions rules/no-new-statics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict'

const PROMISE_STATICS = require('./lib/promise-statics')

module.exports = {
meta: {
docs: {
url: 'https://github.com/xjamundx/eslint-plugin-promise#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 d7ab89e

Please sign in to comment.