diff --git a/README.md b/README.md index d3227ad2..1f5ee743 100644 --- a/README.md +++ b/README.md @@ -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: | | diff --git a/__tests__/no-new-statics.js b/__tests__/no-new-statics.js index 1815cc20..4704490b 100644 --- a/__tests__/no-new-statics.js +++ b/__tests__/no-new-statics.js @@ -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()'" }] } ] }) diff --git a/rules/no-new-statics.js b/rules/no-new-statics.js index b02cb942..0c13def6 100644 --- a/rules/no-new-statics.js +++ b/rules/no-new-statics.js @@ -7,7 +7,8 @@ module.exports = { meta: { docs: { url: getDocsUrl('no-new-statics') - } + }, + fixable: 'code' }, create(context) { return { @@ -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], + '' + ) + } }) } }