Skip to content

Commit 15f0649

Browse files
authored
feat(fixer): add fixer to no-new-statics rule (#133)
1 parent 56434a6 commit 15f0649

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ or start with the recommended rule set
8888
| [`no-promise-in-callback`][no-promise-in-callback] | Avoid using promises inside of callbacks | :warning: | |
8989
| [`no-callback-in-promise`][no-callback-in-promise] | Avoid calling `cb()` inside of a `then()` (use [nodeify][] instead) | :warning: | |
9090
| [`avoid-new`][avoid-new] | Avoid creating `new` promises outside of utility libs (use [pify][] instead) | | |
91-
| [`no-new-statics`][no-new-statics] | Avoid calling `new` on a Promise static method | :bangbang: | |
91+
| [`no-new-statics`][no-new-statics] | Avoid calling `new` on a Promise static method | :bangbang: | :wrench: |
9292
| [`no-return-in-finally`][no-return-in-finally] | Disallow return statements in `finally()` | :warning: | |
9393
| [`valid-params`][valid-params] | Ensures the proper number of arguments are passed to Promise functions | :warning: | |
9494
| [`prefer-await-to-then`][prefer-await-to-then] | Prefer `await` to `then()` for reading Promise values | :seven: | |

__tests__/no-new-statics.js

+19
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,38 @@ ruleTester.run('no-new-statics', rule, {
1818
invalid: [
1919
{
2020
code: 'new Promise.resolve()',
21+
output: 'Promise.resolve()',
2122
errors: [{ message: "Avoid calling 'new' on 'Promise.resolve()'" }]
2223
},
2324
{
2425
code: 'new Promise.reject()',
26+
output: 'Promise.reject()',
2527
errors: [{ message: "Avoid calling 'new' on 'Promise.reject()'" }]
2628
},
2729
{
2830
code: 'new Promise.all()',
31+
output: 'Promise.all()',
2932
errors: [{ message: "Avoid calling 'new' on 'Promise.all()'" }]
3033
},
3134
{
3235
code: 'new Promise.race()',
36+
output: 'Promise.race()',
3337
errors: [{ message: "Avoid calling 'new' on 'Promise.race()'" }]
38+
},
39+
{
40+
code: [
41+
'function foo() {',
42+
' var a = getA()',
43+
' return new Promise.resolve(a)',
44+
'}'
45+
].join('\n'),
46+
output: [
47+
'function foo() {',
48+
' var a = getA()',
49+
' return Promise.resolve(a)',
50+
'}'
51+
].join('\n'),
52+
errors: [{ message: "Avoid calling 'new' on 'Promise.resolve()'" }]
3453
}
3554
]
3655
})

rules/no-new-statics.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ module.exports = {
77
meta: {
88
docs: {
99
url: getDocsUrl('no-new-statics')
10-
}
10+
},
11+
fixable: 'code'
1112
},
1213
create(context) {
1314
return {
@@ -20,7 +21,13 @@ module.exports = {
2021
context.report({
2122
node,
2223
message: "Avoid calling 'new' on 'Promise.{{ name }}()'",
23-
data: { name: node.callee.property.name }
24+
data: { name: node.callee.property.name },
25+
fix(fixer) {
26+
return fixer.replaceTextRange(
27+
[node.start, node.start + 'new '.length],
28+
''
29+
)
30+
}
2431
})
2532
}
2633
}

0 commit comments

Comments
 (0)