2020-08-26 01:57:08 +02:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
|
|
value: true
|
|
|
|
});
|
|
|
|
exports.default = void 0;
|
2022-11-10 11:43:16 +01:00
|
|
|
var _utils = require("@typescript-eslint/utils");
|
|
|
|
var _utils2 = require("./utils");
|
2020-08-26 01:57:08 +02:00
|
|
|
function isEmptyFunction(node) {
|
2022-11-10 11:43:16 +01:00
|
|
|
if (!(0, _utils2.isFunction)(node)) {
|
2020-08-26 01:57:08 +02:00
|
|
|
return false;
|
|
|
|
}
|
2022-11-10 11:43:16 +01:00
|
|
|
return node.body.type === _utils.AST_NODE_TYPES.BlockStatement && !node.body.body.length;
|
2020-08-26 01:57:08 +02:00
|
|
|
}
|
2022-11-10 11:43:16 +01:00
|
|
|
function createTodoFixer(jestFnCall, fixer) {
|
|
|
|
if (jestFnCall.members.length) {
|
|
|
|
return (0, _utils2.replaceAccessorFixer)(fixer, jestFnCall.members[0], 'todo');
|
|
|
|
}
|
|
|
|
return fixer.replaceText(jestFnCall.head.node, `${jestFnCall.head.local}.todo`);
|
2020-08-26 01:57:08 +02:00
|
|
|
}
|
2022-11-10 11:43:16 +01:00
|
|
|
const isTargetedTestCase = jestFnCall => {
|
|
|
|
if (jestFnCall.members.some(s => (0, _utils2.getAccessorValue)(s) !== 'skip')) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-08-26 01:57:08 +02:00
|
|
|
|
2022-11-10 11:43:16 +01:00
|
|
|
// todo: we should support this too (needs custom fixer)
|
|
|
|
if (jestFnCall.name.startsWith('x')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return !jestFnCall.name.startsWith('f');
|
|
|
|
};
|
|
|
|
var _default = (0, _utils2.createRule)({
|
2020-08-26 01:57:08 +02:00
|
|
|
name: __filename,
|
|
|
|
meta: {
|
|
|
|
docs: {
|
|
|
|
category: 'Best Practices',
|
|
|
|
description: 'Suggest using `test.todo`',
|
|
|
|
recommended: false
|
|
|
|
},
|
|
|
|
messages: {
|
|
|
|
emptyTest: 'Prefer todo test case over empty test case',
|
|
|
|
unimplementedTest: 'Prefer todo test case over unimplemented test case'
|
|
|
|
},
|
|
|
|
fixable: 'code',
|
|
|
|
schema: [],
|
|
|
|
type: 'layout'
|
|
|
|
},
|
|
|
|
defaultOptions: [],
|
|
|
|
create(context) {
|
|
|
|
return {
|
|
|
|
CallExpression(node) {
|
|
|
|
const [title, callback] = node.arguments;
|
2022-11-10 11:43:16 +01:00
|
|
|
const jestFnCall = (0, _utils2.parseJestFnCall)(node, context);
|
|
|
|
if (!title || (jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'test' || !isTargetedTestCase(jestFnCall) || !(0, _utils2.isStringNode)(title)) {
|
2020-08-26 01:57:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (callback && isEmptyFunction(callback)) {
|
|
|
|
context.report({
|
|
|
|
messageId: 'emptyTest',
|
|
|
|
node,
|
2022-11-10 11:43:16 +01:00
|
|
|
fix: fixer => [fixer.removeRange([title.range[1], callback.range[1]]), createTodoFixer(jestFnCall, fixer)]
|
2020-08-26 01:57:08 +02:00
|
|
|
});
|
|
|
|
}
|
2022-11-10 11:43:16 +01:00
|
|
|
if ((0, _utils2.hasOnlyOneArgument)(node)) {
|
2020-08-26 01:57:08 +02:00
|
|
|
context.report({
|
|
|
|
messageId: 'unimplementedTest',
|
|
|
|
node,
|
2022-11-10 11:43:16 +01:00
|
|
|
fix: fixer => createTodoFixer(jestFnCall, fixer)
|
2020-08-26 01:57:08 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
exports.default = _default;
|