2020-03-07 11:45:40 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
|
|
value: true
|
|
|
|
});
|
|
|
|
exports.default = void 0;
|
|
|
|
|
|
|
|
var _experimentalUtils = require("@typescript-eslint/experimental-utils");
|
|
|
|
|
|
|
|
var _utils = require("./utils");
|
|
|
|
|
2020-03-31 20:40:00 +08:00
|
|
|
function isEmptyFunction(node) {
|
|
|
|
if (!(0, _utils.isFunction)(node)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
/* istanbul ignore if https://github.com/typescript-eslint/typescript-eslint/issues/734 */
|
2020-03-07 11:45:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
if (!node.body) {
|
|
|
|
throw new Error(`Unexpected null while performing prefer-todo - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return node.body.type === _experimentalUtils.AST_NODE_TYPES.BlockStatement && node.body.body && !node.body.body.length;
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:40:00 +08:00
|
|
|
function createTodoFixer(node, fixer) {
|
2020-03-07 11:45:40 +08:00
|
|
|
const testName = (0, _utils.getNodeName)(node.callee).split('.').shift();
|
|
|
|
return fixer.replaceText(node.callee, `${testName}.todo`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const isTargetedTestCase = node => (0, _utils.isTestCase)(node) && [_utils.TestCaseName.it, _utils.TestCaseName.test, 'it.skip', 'test.skip'].includes((0, _utils.getNodeName)(node.callee));
|
|
|
|
|
|
|
|
var _default = (0, _utils.createRule)({
|
|
|
|
name: __filename,
|
|
|
|
meta: {
|
|
|
|
docs: {
|
|
|
|
category: 'Best Practices',
|
|
|
|
description: 'Suggest using `test.todo`',
|
|
|
|
recommended: false
|
|
|
|
},
|
|
|
|
messages: {
|
2020-03-31 20:40:00 +08:00
|
|
|
emptyTest: 'Prefer todo test case over empty test case',
|
|
|
|
unimplementedTest: 'Prefer todo test case over unimplemented test case'
|
2020-03-07 11:45:40 +08:00
|
|
|
},
|
|
|
|
fixable: 'code',
|
|
|
|
schema: [],
|
|
|
|
type: 'layout'
|
|
|
|
},
|
|
|
|
defaultOptions: [],
|
|
|
|
|
|
|
|
create(context) {
|
|
|
|
return {
|
|
|
|
CallExpression(node) {
|
2020-03-31 20:40:00 +08:00
|
|
|
const [title, callback] = node.arguments;
|
2020-03-07 11:45:40 +08:00
|
|
|
|
2020-03-31 20:40:00 +08:00
|
|
|
if (!title || !isTargetedTestCase(node) || !(0, _utils.isStringNode)(title)) {
|
2020-03-07 11:45:40 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:40:00 +08:00
|
|
|
if (callback && isEmptyFunction(callback)) {
|
2020-03-07 11:45:40 +08:00
|
|
|
context.report({
|
2020-03-31 20:40:00 +08:00
|
|
|
messageId: 'emptyTest',
|
2020-03-07 11:45:40 +08:00
|
|
|
node,
|
2020-03-31 20:40:00 +08:00
|
|
|
fix: fixer => [fixer.removeRange([title.range[1], callback.range[1]]), createTodoFixer(node, fixer)]
|
2020-03-07 11:45:40 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((0, _utils.hasOnlyOneArgument)(node)) {
|
|
|
|
context.report({
|
2020-03-31 20:40:00 +08:00
|
|
|
messageId: 'unimplementedTest',
|
2020-03-07 11:45:40 +08:00
|
|
|
node,
|
2020-03-31 20:40:00 +08:00
|
|
|
fix: fixer => [createTodoFixer(node, fixer)]
|
2020-03-07 11:45:40 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
exports.default = _default;
|