Sha256: 63bd4ce6d5b93f44abfa1dd8fda112ce6db796d9ce8d4c604d6346994e767c92
Contents?: true
Size: 1.88 KB
Versions: 43
Compression:
Stored size: 1.88 KB
Contents
/** * Rule: catch-or-return * Ensures that promises either include a catch() handler * or are returned (to be handled upstream) */ 'use strict' const getDocsUrl = require('./lib/get-docs-url') const isPromise = require('./lib/is-promise') module.exports = { meta: { docs: { url: getDocsUrl('catch-or-return') }, messages: { terminationMethod: 'Expected {{ terminationMethod }}() or return' } }, create(context) { const options = context.options[0] || {} const allowThen = options.allowThen let terminationMethod = options.terminationMethod || 'catch' if (typeof terminationMethod === 'string') { terminationMethod = [terminationMethod] } return { ExpressionStatement(node) { if (!isPromise(node.expression)) { return } // somePromise.then(a, b) if ( allowThen && node.expression.type === 'CallExpression' && node.expression.callee.type === 'MemberExpression' && node.expression.callee.property.name === 'then' && node.expression.arguments.length === 2 ) { return } // somePromise.catch() if ( node.expression.type === 'CallExpression' && node.expression.callee.type === 'MemberExpression' && terminationMethod.indexOf(node.expression.callee.property.name) !== -1 ) { return } // somePromise['catch']() if ( node.expression.type === 'CallExpression' && node.expression.callee.type === 'MemberExpression' && node.expression.callee.property.type === 'Literal' && node.expression.callee.property.value === 'catch' ) { return } context.report({ node, messageId: 'terminationMethod', data: { terminationMethod } }) } } } }
Version data entries
43 entries across 43 versions & 1 rubygems