Skip to content

Commit 7b84d75

Browse files
committed
feat(rule): add no-new-statics rule
Resolves eslint-community#75
1 parent 3e674c3 commit 7b84d75

File tree

7 files changed

+80
-3
lines changed

7 files changed

+80
-3
lines changed

README.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,21 @@ Enforce best practices for JavaScript promises.
1313

1414
* [Installation](#installation)
1515
* [Usage](#usage)
16-
* [Rules](#rules)
16+
* [Rules](#rules) <<<<<<< HEAD =======
17+
* [`catch-or-return`](#catch-or-return)
18+
* [`no-return-wrap`](#no-return-wrap)
19+
* [`param-names`](#param-names)
20+
* [`always-return`](#always-return)
21+
* [`no-native`](#no-native)
22+
* [`no-nesting`](#no-nesting)
23+
* [`no-promise-in-callback`](#no-promise-in-callback)
24+
* [`no-callback-in-promise`](#no-callback-in-promise)
25+
* [`avoid-new`](#avoid-new)
26+
* [`no-new-statics`](#no-new-statics)
27+
* [`no-return-in-finally`](#no-return-in-finally)
28+
* [`prefer-await-to-then`](#prefer-await-to-then)
29+
* [`prefer-await-to-callbacks`](#prefer-await-to-callbacks)
30+
> > > > > > > feat(rule): add no-new-statics rule
1731
* [Maintainers](#maintainers)
1832
* [License](#license)
1933

@@ -61,6 +75,7 @@ Then configure the rules you want to use under the rules section.
6175
"promise/no-promise-in-callback": "warn",
6276
"promise/no-callback-in-promise": "warn",
6377
"promise/avoid-new": "warn",
78+
"promise/no-new-statics": "warn",
6479
"promise/no-return-in-finally": "warn"
6580
}
6681
}
@@ -87,6 +102,7 @@ or start with the recommended rule set
87102
| [`no-promise-in-callback`][no-promise-in-callback] | Avoid using promises inside of callbacks | :warning: | |
88103
| [`no-callback-in-promise`][no-callback-in-promise] | Avoid calling `cb()` inside of a `then()` (use [nodeify][] instead) | :warning: | |
89104
| [`avoid-new` ][avoid-new] | Avoid creating `new` promises outside of utility libs (use [pify][] instead) | :warning: | |
105+
| [`no-new-statics`][no-new-statics] | Avoid calling `new` on a Promise static method (e.g. `new Promise.resolve()`) | :bangbang: | |
90106
| [`no-return-in-finally`][no-return-in-finally] | Disallow return statements in `finally()` | :warning: | |
91107
| [`prefer-await-to-then`][prefer-await-to-then] | Prefer `await` to `then()` for reading Promise values | :seven: | |
92108
| [`prefer-await-to-callbacks`][prefer-await-to-callbacks] | Prefer async/await to the callback pattern | :seven: | |
@@ -119,6 +135,7 @@ or start with the recommended rule set
119135
[no-promise-in-callback]: docs/rules/no-promise-in-callback.md
120136
[no-callback-in-promise]: docs/rules/no-callback-in-promise.md
121137
[avoid-new]: docs/rules/avoid-new.md
138+
[no-new-statics]: docs/rules/no-new-statics.md
122139
[no-return-in-finally]: docs/rules/no-return-in-finally.md
123140
[prefer-await-to-then]: docs/rules/prefer-await-to-then.md
124141
[prefer-await-to-callbacks]: docs/rules/prefer-await-to-callbacks.md

docs/rules/no-new-statics.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Avoid calling `new` on a Promise static method (e.g. `new Promise.resolve()`) (no-new-statics)

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

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

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)