Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,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) | | |
| [`no-new-statics`][no-new-statics] | Avoid calling `new` on a Promise static method | :bangbang: | |
| [`no-new-statics`][no-new-statics] | Avoid calling `new` on a Promise static method | :bangbang: | :wrench: |
| [`no-return-in-finally`][no-return-in-finally] | Disallow return statements in `finally()` | :warning: | |
| [`valid-params`][valid-params] | Ensures the proper number of arguments are passed to Promise functions | :warning: | |
| [`prefer-await-to-then`][prefer-await-to-then] | Prefer `await` to `then()` for reading Promise values | :seven: | |
Expand Down
19 changes: 19 additions & 0 deletions __tests__/no-new-statics.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,38 @@ ruleTester.run('no-new-statics', rule, {
invalid: [
{
code: 'new Promise.resolve()',
output: 'Promise.resolve()',
errors: [{ message: "Avoid calling 'new' on 'Promise.resolve()'" }]
},
{
code: 'new Promise.reject()',
output: 'Promise.reject()',
errors: [{ message: "Avoid calling 'new' on 'Promise.reject()'" }]
},
{
code: 'new Promise.all()',
output: 'Promise.all()',
errors: [{ message: "Avoid calling 'new' on 'Promise.all()'" }]
},
{
code: 'new Promise.race()',
output: 'Promise.race()',
errors: [{ message: "Avoid calling 'new' on 'Promise.race()'" }]
},
{
code: [
'function foo() {',
' var a = getA()',
' return new Promise.resolve(a)',
'}'
].join('\n'),
output: [
'function foo() {',
' var a = getA()',
' return Promise.resolve(a)',
'}'
].join('\n'),
errors: [{ message: "Avoid calling 'new' on 'Promise.resolve()'" }]
}
]
})
11 changes: 9 additions & 2 deletions rules/no-new-statics.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ module.exports = {
meta: {
docs: {
url: getDocsUrl('no-new-statics')
}
},
fixable: 'code'
},
create(context) {
return {
Expand All @@ -20,7 +21,13 @@ module.exports = {
context.report({
node,
message: "Avoid calling 'new' on 'Promise.{{ name }}()'",
data: { name: node.callee.property.name }
data: { name: node.callee.property.name },
fix(fixer) {
return fixer.replaceTextRange(
[node.start, node.start + 'new '.length],
''
)
}
})
}
}
Expand Down