Skip to content

Commit d7ab89e

Browse files
committed
feat(rule): add no-new-statics rule
Resolves eslint-community#75
1 parent c0c662a commit d7ab89e

File tree

6 files changed

+67
-2
lines changed

6 files changed

+67
-2
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Enforce best practices for JavaScript promises.
2323
* [`no-promise-in-callback`](#no-promise-in-callback)
2424
* [`no-callback-in-promise`](#no-callback-in-promise)
2525
* [`avoid-new`](#avoid-new)
26+
* [`no-new-statics`](#no-new-statics)
2627
* [`no-return-in-finally`](#no-return-in-finally)
2728
* [`prefer-await-to-then`](#prefer-await-to-then)
2829
* [`prefer-await-to-callbacks`](#prefer-await-to-callbacks)
@@ -73,6 +74,7 @@ Then configure the rules you want to use under the rules section.
7374
"promise/no-promise-in-callback": "warn",
7475
"promise/no-callback-in-promise": "warn",
7576
"promise/avoid-new": "warn",
77+
"promise/no-new-statics": "warn",
7678
"promise/no-return-in-finally": "warn"
7779
}
7880
}
@@ -99,6 +101,7 @@ or start with the recommended rule set
99101
| `no-promise-in-callback` | Avoid using promises inside of callbacks | :warning: | |
100102
| `no-callback-in-promise` | Avoid calling `cb()` inside of a `then()` (use [nodeify][] instead) | :warning: | |
101103
| `avoid-new` | Avoid creating `new` promises outside of utility libs (use [pify][] instead) | :warning: | |
104+
| `no-new-statics` | Avoid calling `new` on a Promise static method (e.g. `new Promise.resolve()`) | :bangbang: | |
102105
| `no-return-in-finally` | Disallow return statements in `finally()` | :warning: | |
103106
| `prefer-await-to-then` | Prefer `await` to `then()` for reading Promise values | :seven: | |
104107
| `prefer-await-to-callbacks` | Prefer async/await to the callback pattern | :seven: | |
@@ -309,6 +312,10 @@ Avoid calling `cb()` inside of a `then()` (use [nodeify][] instead)
309312

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

315+
### `no-new-statics`
316+
317+
Avoid calling `new` on a Promise static method (e.g. `new Promise.resolve()`)
318+
312319
### `no-return-in-finally`
313320

314321
Disallow return statements inside a callback passed to `finally()`, since

index.js

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module.exports = {
1313
'no-promise-in-callback': require('./rules/no-promise-in-callback'),
1414
'no-nesting': require('./rules/no-nesting'),
1515
'avoid-new': require('./rules/avoid-new'),
16+
'no-new-statics': require('./rules/no-new-statics'),
1617
'no-return-in-finally': require('./rules/no-return-in-finally')
1718
},
1819
rulesConfig: {
@@ -34,6 +35,7 @@ module.exports = {
3435
'promise/no-promise-in-callback': 'warn',
3536
'promise/no-callback-in-promise': 'warn',
3637
'promise/avoid-new': 'warn',
38+
'promise/no-new-statics': 'error',
3739
'promise/no-return-in-finally': 'warn'
3840
}
3941
}

rules/lib/is-promise.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55
'use strict'
66

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

99
function isPromise(expression) {
1010
return (
@@ -29,7 +29,7 @@ function isPromise(expression) {
2929
expression.callee.type === 'MemberExpression' &&
3030
expression.callee.object.type === 'Identifier' &&
3131
expression.callee.object.name === 'Promise' &&
32-
STATIC_METHODS.indexOf(expression.callee.property.name) !== -1)
32+
PROMISE_STATICS.indexOf(expression.callee.property.name) !== -1)
3333
)
3434
}
3535

rules/lib/promise-statics.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict'
2+
3+
module.exports = ['all', 'race', 'reject', 'resolve']

rules/no-new-statics.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict'
2+
3+
const PROMISE_STATICS = require('./lib/promise-statics')
4+
5+
module.exports = {
6+
meta: {
7+
docs: {
8+
url: 'https://github.com/xjamundx/eslint-plugin-promise#no-new-statics'
9+
}
10+
},
11+
create(context) {
12+
return {
13+
NewExpression(node) {
14+
if (
15+
node.callee.type === 'MemberExpression' &&
16+
node.callee.object.name === 'Promise' &&
17+
PROMISE_STATICS.indexOf(node.callee.property.name) > -1
18+
) {
19+
context.report({
20+
node,
21+
message: 'Avoid calling new on a Promise static method'
22+
})
23+
}
24+
}
25+
}
26+
}
27+
}

test/no-new-statics.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict'
2+
3+
const rule = require('../rules/no-new-statics')
4+
const RuleTester = require('eslint').RuleTester
5+
const ruleTester = new RuleTester()
6+
7+
const message = 'Avoid calling new on a Promise static method'
8+
9+
ruleTester.run('no-new-statics', rule, {
10+
valid: [
11+
'Promise.resolve()',
12+
'Promise.reject()',
13+
'Promise.all()',
14+
'Promise.race()',
15+
'new Promise(function (resolve, reject) {})',
16+
'new SomeClass()',
17+
'SomeClass.resolve()',
18+
'new SomeClass.resolve()'
19+
],
20+
invalid: [
21+
{ code: 'new Promise.resolve()', errors: [{ message }] },
22+
{ code: 'new Promise.reject()', errors: [{ message }] },
23+
{ code: 'new Promise.all()', errors: [{ message }] },
24+
{ code: 'new Promise.race()', errors: [{ message }] }
25+
]
26+
})

0 commit comments

Comments
 (0)