Skip to content

feat(rule): add no-new-statics rule #82

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

Merged
merged 3 commits into from
Feb 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,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": "error",
"promise/no-return-in-finally": "warn"
}
}
Expand All @@ -87,6 +88,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 | :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 +121,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
36 changes: 36 additions & 0 deletions __tests__/no-new-statics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict'

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

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: "Avoid calling 'new' on 'Promise.resolve()'" }]
},
{
code: 'new Promise.reject()',
errors: [{ message: "Avoid calling 'new' on 'Promise.reject()'" }]
},
{
code: 'new Promise.all()',
errors: [{ message: "Avoid calling 'new' on 'Promise.all()'" }]
},
{
code: 'new Promise.race()',
errors: [{ message: "Avoid calling 'new' on 'Promise.race()'" }]
}
]
})
32 changes: 32 additions & 0 deletions docs/rules/no-new-statics.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Avoid calling `new` on a Promise static method (no-new-statics)

Calling a Promise static method with `new` is invalid, resulting in a
`TypeError` at runtime.

## Rule Details

This rule is aimed at flagging instances where a Promise static method is called
with `new`.

Examples for **incorrect** code for this rule:

```js
new Promise.resolve(value)
new Promise.reject(error)
new Promise.race([p1, p2])
new Promise.all([p1, p2])
```

Examples for **correct** code for this rule:

```js
Promise.resolve(value)
Promise.reject(error)
Promise.race([p1, p2])
Promise.all([p1, p2])
```

## When Not To Use It

If you do not want to be notified when calling `new` on a Promise static method,
you can safely disable this rule.
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']
29 changes: 29 additions & 0 deletions rules/no-new-statics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'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 'Promise.{{ name }}()'",
data: { name: node.callee.property.name }
})
}
}
}
}
}