Skip to content

Commit

Permalink
feat(fixer): add fixer to no-new-statics rule (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
macklinu committed Jul 9, 2018
1 parent 56434a6 commit 15f0649
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -88,7 +88,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
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
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

0 comments on commit 15f0649

Please sign in to comment.