Skip to content

Commit 0b5a294

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

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
@@ -22,6 +22,7 @@ Enforce best practices for JavaScript promises.
2222
* [`no-promise-in-callback`](#no-promise-in-callback)
2323
* [`no-callback-in-promise`](#no-callback-in-promise)
2424
* [`avoid-new`](#avoid-new)
25+
* [`no-new-statics`](#no-new-statics)
2526
* [`no-return-in-finally`](#no-return-in-finally)
2627
* [`prefer-await-to-then`](#prefer-await-to-then)
2728
* [`prefer-await-to-callbacks`](#prefer-await-to-callbacks)
@@ -72,6 +73,7 @@ Then configure the rules you want to use under the rules section.
7273
"promise/no-promise-in-callback": "warn",
7374
"promise/no-callback-in-promise": "warn",
7475
"promise/avoid-new": "warn",
76+
"promise/no-new-statics": "warn",
7577
"promise/no-return-in-finally": "warn"
7678
}
7779
}
@@ -98,6 +100,7 @@ or start with the recommended rule set
98100
| :warning: | `no-promise-in-callback` | Avoid using promises inside of callbacks |
99101
| :warning: | `no-callback-in-promise` | Avoid calling `cb()` inside of a `then()` (use [nodeify][] instead) |
100102
| :warning: | `avoid-new` | Avoid creating `new` promises outside of utility libs (use [pify][] instead) |
103+
| :warning: | `no-new-statics` | Avoid calling `new` on a Promise static method (e.g. `new Promise.resolve()`) |
101104
| :warning: | `no-return-in-finally` | Disallow return statements in `finally()` |
102105
| :seven: | `prefer-await-to-then` | Prefer `await` to `then()` for reading Promise values |
103106
| :seven: | `prefer-await-to-callbacks` | Prefer async/await to the callback pattern |
@@ -304,6 +307,10 @@ Avoid calling `cb()` inside of a `then()` (use [nodeify][] instead)
304307

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

310+
### `no-new-statics`
311+
312+
Avoid calling `new` on a Promise static method (e.g. `new Promise.resolve()`)
313+
307314
### `no-return-in-finally`
308315

309316
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': 'warn',
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)