-
-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rule): add no-identity-handlers rule
Resolves #12
- Loading branch information
Showing
4 changed files
with
145 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
'use strict' | ||
|
||
const isPromise = require('./lib/is-promise') | ||
|
||
function isFunctionExpression(node) { | ||
return ( | ||
node.type === 'FunctionExpression' || | ||
node.type === 'ArrowFunctionExpression' | ||
) | ||
} | ||
|
||
function getFirstParamName(node) { | ||
const firstParam = node.params[0] | ||
return firstParam && firstParam.type === 'Identifier' ? firstParam.name : null | ||
} | ||
|
||
function getBodyValueName(node) { | ||
const body = node.body || {} | ||
if (body.type === 'Identifier') { | ||
return body.name | ||
} | ||
if (body.type === 'BlockStatement') { | ||
const firstBodyStatement = body.body[0] || { type: '', argument: {} } | ||
return (firstBodyStatement.type === 'ReturnStatement' || | ||
firstBodyStatement.type === 'ThrowStatement') && | ||
firstBodyStatement.argument.type === 'Identifier' | ||
? firstBodyStatement.argument.name | ||
: null | ||
} | ||
return null | ||
} | ||
|
||
function isIdentityFunction(node) { | ||
return node.params.length === 1 | ||
? getFirstParamName(node) === getBodyValueName(node) | ||
: false | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
url: | ||
'https://github.com/xjamundx/eslint-plugin-promise#no-identity-handlers' | ||
} | ||
}, | ||
create(context) { | ||
function checkIdentity(node) { | ||
if (node && isFunctionExpression(node) && isIdentityFunction(node)) { | ||
context.report({ | ||
node, | ||
message: 'No identity handlers' | ||
}) | ||
} | ||
} | ||
|
||
return { | ||
CallExpression(node) { | ||
if (!isPromise(node)) { | ||
return | ||
} | ||
|
||
switch (node.callee.property.name) { | ||
case 'then': | ||
checkIdentity(node.arguments[0]) | ||
checkIdentity(node.arguments[1]) | ||
break | ||
case 'catch': | ||
checkIdentity(node.arguments[0]) | ||
break | ||
default: | ||
break | ||
} | ||
} | ||
} | ||
} | ||
} |
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,60 @@ | ||
'use strict' | ||
|
||
const rule = require('../rules/no-identity-handlers') | ||
const RuleTester = require('eslint').RuleTester | ||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 6 | ||
} | ||
}) | ||
|
||
ruleTester.run('no-identity-handlers', rule, { | ||
valid: [ | ||
'Promise.resolve(2).then(value => value * 2)', | ||
'Promise.resolve(2).then(value => { return value + 1 })', | ||
'Promise.resolve(2).then(() => null)', | ||
'Promise.resolve(2).then(() => doSomethingTotallyDifferent())', | ||
'somePromise().then(value => doSomethingWith(value))', | ||
'somePromise().then(handler)', | ||
'Promise.reject(Error()).catch(err => { console.error(err); throw err; })', | ||
'Promise.reject(Error()).catch(err => { throw doSomethingTo(err) })', | ||
'somePromise().catch(handler)', | ||
'somePromise().catch(createHandler())', | ||
'somePromise().then(value => value * 2, err => { console.error(err); throw err; })', | ||
'somePromise().then(func, func)', | ||
|
||
// edge cases that aren't really valid but shouldn't throw or report | ||
'Promise.resolve(2).then()', | ||
'Promise.reject(Error()).catch()' | ||
], | ||
invalid: [ | ||
{ | ||
code: 'Promise.resolve(2).then(_ => _)', | ||
errors: [{ message: 'No identity handlers' }] | ||
}, | ||
{ | ||
code: 'Promise.resolve(2).then(val => { return val })', | ||
errors: [{ message: 'No identity handlers' }] | ||
}, | ||
{ | ||
code: 'Promise.resolve(2).then(function (value) { return value })', | ||
errors: [{ message: 'No identity handlers' }] | ||
}, | ||
{ | ||
code: 'Promise.reject(Error()).catch(err => { throw err })', | ||
errors: [{ message: 'No identity handlers' }] | ||
}, | ||
{ | ||
code: 'Promise.reject(Error()).catch(function (e) { throw e })', | ||
errors: [{ message: 'No identity handlers' }] | ||
}, | ||
{ | ||
code: 'Promise.reject(Error()).then(null, error => { throw error })', | ||
errors: [{ message: 'No identity handlers' }] | ||
}, | ||
{ | ||
code: 'Promise.reject(Error()).then(null, function (e) { throw e })', | ||
errors: [{ message: 'No identity handlers' }] | ||
} | ||
] | ||
}) |