charlike
Version:
Small, fast, simple and streaming project scaffolder for myself, but not only. Supports hundreds of template engines through the @JSTransformers API or if you want custom `render` function passed through options
43 lines (38 loc) • 1.34 kB
JavaScript
/**
* Rule: no-return-wrap function
* Prevents uneccessary wrapping of results in Promise.resolve
* or Promise.reject as the Promise will do that for us
*/
var isPromise = require('./lib/is-promise')
var rejectMessage = 'Expected throw instead of Promise.reject'
var resolveMessage = 'Avoid wrapping return values in Promise.resolve'
function isInPromise (context) {
var expression = context.getAncestors().filter(function (node) {
return node.type === 'ExpressionStatement'
})[0]
return expression && expression.expression && isPromise(expression.expression)
}
module.exports = {
create: function (context) {
return {
ReturnStatement: function (node) {
if (isInPromise(context)) {
if (node.argument) {
if (node.argument.type === 'CallExpression') {
if (node.argument.callee.type === 'MemberExpression') {
if (node.argument.callee.object.name === 'Promise') {
if (node.argument.callee.property.name === 'resolve') {
context.report(node, resolveMessage)
} else if (node.argument.callee.property.name === 'reject') {
context.report(node, rejectMessage)
}
}
}
}
}
}
}
}
}
}