eslint-plugin-jest
Version:
Eslint rules for Jest
124 lines (102 loc) • 3.94 kB
JavaScript
;
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
const _require = require('./util'),
getDocsUrl = _require.getDocsUrl,
isDescribe = _require.isDescribe,
isFunction = _require.isFunction;
const isAsync = node => node.async;
const isString = node => node.type === 'Literal' && typeof node.value === 'string' || node.type === 'TemplateLiteral';
const hasParams = node => node.params.length > 0;
const paramsLocation = params => {
const _params = _slicedToArray(params, 1),
first = _params[0];
const last = params[params.length - 1];
return {
start: {
line: first.loc.start.line,
column: first.loc.start.column
},
end: {
line: last.loc.end.line,
column: last.loc.end.column
}
};
};
module.exports = {
meta: {
docs: {
url: getDocsUrl(__filename)
},
messages: {
nameAndCallback: 'Describe requires name and callback arguments',
firstArgumentMustBeName: 'First argument must be name',
secondArgumentMustBeFunction: 'Second argument must be function',
noAsyncDescribeCallback: 'No async describe callback',
unexpectedDescribeArgument: 'Unexpected argument(s) in describe callback',
unexpectedReturnInDescribe: 'Unexpected return statement in describe callback'
}
},
create(context) {
return {
CallExpression(node) {
if (isDescribe(node)) {
if (node.arguments.length === 0) {
return context.report({
messageId: 'nameAndCallback',
loc: node.loc
});
}
const _node$arguments = _slicedToArray(node.arguments, 1),
name = _node$arguments[0];
const _node$arguments2 = _slicedToArray(node.arguments, 2),
callbackFunction = _node$arguments2[1];
if (!isString(name)) {
context.report({
messageId: 'firstArgumentMustBeName',
loc: paramsLocation(node.arguments)
});
}
if (callbackFunction === undefined) {
context.report({
messageId: 'nameAndCallback',
loc: paramsLocation(node.arguments)
});
return;
}
if (!isFunction(callbackFunction)) {
context.report({
messageId: 'secondArgumentMustBeFunction',
loc: paramsLocation(node.arguments)
});
return;
}
if (isAsync(callbackFunction)) {
context.report({
messageId: 'noAsyncDescribeCallback',
node: callbackFunction
});
}
if (hasParams(callbackFunction)) {
context.report({
messageId: 'unexpectedDescribeArgument',
loc: paramsLocation(callbackFunction.params)
});
}
if (callbackFunction.body.type === 'BlockStatement') {
callbackFunction.body.body.forEach(node => {
if (node.type === 'ReturnStatement') {
context.report({
messageId: 'unexpectedReturnInDescribe',
node
});
}
});
}
}
}
};
}
};