-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
198 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* Rule: prefer-await-to-callbacks | ||
* Discourage using then() and instead use async/await. | ||
*/ | ||
|
||
var errorMessage = 'Avoid callbacks. Prefer Async/Await.' | ||
|
||
module.exports = function (context) { | ||
function checkLastParamsForCallback (node) { | ||
var len = node.params.length - 1 | ||
var lastParam = node.params[len] | ||
if (lastParam && (lastParam.name === 'callback' || lastParam.name === 'cb')) { | ||
context.report(lastParam, errorMessage) | ||
} | ||
} | ||
function isInsideYieldOrAwait () { | ||
return context.getAncestors().some(function (parent) { | ||
return parent.type === 'AwaitExpression' || parent.type === 'YieldExpression' | ||
}) | ||
} | ||
return { | ||
CallExpression: function (node) { | ||
// callbacks aren't allowed | ||
if (node.callee.name === 'cb' || node.callee.name === 'callback') { | ||
context.report(node, errorMessage) | ||
return | ||
} | ||
|
||
// thennables aren't allowed either | ||
var args = node.arguments | ||
var num = args.length - 1 | ||
var arg = num > -1 && node.arguments && node.arguments[num] | ||
if (arg && arg.type === 'FunctionExpression' || arg.type === 'ArrowFunctionExpression') { | ||
if (arg.params && arg.params[0] && arg.params[0].name === 'err') { | ||
if (!isInsideYieldOrAwait()) { | ||
context.report(arg, errorMessage) | ||
} | ||
} | ||
} | ||
}, | ||
FunctionDeclaration: checkLastParamsForCallback, | ||
FunctionExpression: checkLastParamsForCallback, | ||
ArrowFunctionExpression: checkLastParamsForCallback | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Rule: prefer-await-to-then | ||
* Discourage using then() and instead use async/await. | ||
*/ | ||
|
||
module.exports = function (context) { | ||
return { | ||
MemberExpression: function (node) { | ||
// you can then() if you are inside of a yield or await | ||
if (context.getAncestors().some(function (parent) { | ||
return parent.type === 'AwaitExpression' || parent.type === 'YieldExpression' | ||
})) { | ||
return | ||
} | ||
|
||
// if you're a then expression then you're probably a promise | ||
if (node.property && node.property.name === 'then') { | ||
context.report(node.property, 'Prefer await to then().') | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
'use strict' | ||
|
||
var rule = require('../rules/prefer-await-to-callbacks') | ||
var RuleTester = require('eslint').RuleTester | ||
var message = 'Avoid callbacks. Prefer Async/Await.' | ||
var parserOptions = { ecmaVersion: 8 } | ||
var ruleTester = new RuleTester() | ||
|
||
ruleTester.run('prefer-await-to-callbacks', rule, { | ||
valid: [ | ||
{ code: 'async function hi() { await thing().catch(err => console.log(err)) }', parserOptions: parserOptions }, | ||
{ code: 'async function hi() { await thing().then() }', parserOptions: parserOptions }, | ||
{ code: 'async function hi() { await thing().catch() }', parserOptions: parserOptions } | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: 'heart(function(err) {})', | ||
parserOptions: parserOptions, | ||
errors: [ { message: message } ] | ||
}, | ||
{ | ||
code: 'heart(err => {})', | ||
parserOptions: parserOptions, | ||
errors: [ { message: message } ] | ||
}, | ||
{ | ||
code: 'heart("ball", function(err) {})', | ||
parserOptions: parserOptions, | ||
errors: [ { message: message } ] | ||
}, | ||
{ | ||
code: 'function getData(id, callback) {}', | ||
parserOptions: parserOptions, | ||
errors: [ { message: message } ] | ||
}, | ||
{ | ||
code: 'const getData = (cb) => {}', | ||
parserOptions: parserOptions, | ||
errors: [ { message: message } ] | ||
}, | ||
{ | ||
code: 'var x = function (x, cb) {}', | ||
parserOptions: parserOptions, | ||
errors: [ { message: message } ] | ||
}, | ||
{ | ||
code: 'cb()', | ||
parserOptions: parserOptions, | ||
errors: [ { message: message } ] | ||
}, | ||
{ | ||
code: 'callback()', | ||
parserOptions: parserOptions, | ||
errors: [ { message: message } ] | ||
} | ||
] | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict' | ||
|
||
var rule = require('../rules/prefer-await-to-then') | ||
var RuleTester = require('eslint').RuleTester | ||
var message = 'Prefer await to then().' | ||
var parserOptions = { ecmaVersion: 8 } | ||
var ruleTester = new RuleTester() | ||
ruleTester.run('prefer-await-to-then', rule, { | ||
valid: [ | ||
{ code: 'async function hi() { await thing() }', parserOptions: parserOptions }, | ||
{ code: 'async function hi() { await thing().then() }', parserOptions: parserOptions }, | ||
{ code: 'async function hi() { await thing().catch() }', parserOptions: parserOptions } | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: 'hey.then(x => {})', | ||
parserOptions: parserOptions, | ||
errors: [ { message: message } ] | ||
}, | ||
{ | ||
code: 'hey.then(function() { }).then()', | ||
parserOptions: parserOptions, | ||
errors: [ { message: message }, { message: message } ] | ||
}, | ||
{ | ||
code: 'hey.then(function() { }).then(x).catch()', | ||
parserOptions: parserOptions, | ||
errors: [ { message: message }, { message: message } ] | ||
}, | ||
{ | ||
code: 'async function a() { hey.then(function() { }).then(function() { }) }', | ||
parserOptions: parserOptions, | ||
errors: [ { message: message }, { message: message } ] | ||
} | ||
] | ||
}) |