2020-08-25 23:57:08 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
|
|
value: true
|
|
|
|
});
|
|
|
|
exports.default = void 0;
|
2022-11-10 10:43:16 +00:00
|
|
|
var _utils = require("@typescript-eslint/utils");
|
|
|
|
var _utils2 = require("./utils");
|
|
|
|
var _default = (0, _utils2.createRule)({
|
2020-08-25 23:57:08 +00:00
|
|
|
name: __filename,
|
|
|
|
meta: {
|
|
|
|
docs: {
|
|
|
|
category: 'Best Practices',
|
|
|
|
description: 'Disallow focused tests',
|
|
|
|
recommended: 'error'
|
|
|
|
},
|
|
|
|
messages: {
|
2022-11-10 10:43:16 +00:00
|
|
|
focusedTest: 'Unexpected focused test.',
|
|
|
|
suggestRemoveFocus: 'Remove focus from test.'
|
2020-08-25 23:57:08 +00:00
|
|
|
},
|
|
|
|
schema: [],
|
2022-11-10 10:43:16 +00:00
|
|
|
type: 'suggestion',
|
|
|
|
hasSuggestions: true
|
2020-08-25 23:57:08 +00:00
|
|
|
},
|
|
|
|
defaultOptions: [],
|
2022-11-10 10:43:16 +00:00
|
|
|
create(context) {
|
|
|
|
return {
|
|
|
|
CallExpression(node) {
|
|
|
|
const jestFnCall = (0, _utils2.parseJestFnCall)(node, context);
|
|
|
|
if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'test' && (jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'describe') {
|
2020-08-25 23:57:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-11-10 10:43:16 +00:00
|
|
|
if (jestFnCall.name.startsWith('f')) {
|
2020-08-25 23:57:08 +00:00
|
|
|
context.report({
|
|
|
|
messageId: 'focusedTest',
|
2022-11-10 10:43:16 +00:00
|
|
|
node,
|
|
|
|
suggest: [{
|
|
|
|
messageId: 'suggestRemoveFocus',
|
|
|
|
fix(fixer) {
|
|
|
|
// don't apply the fixer if we're an aliased import
|
|
|
|
if (jestFnCall.head.type === 'import' && jestFnCall.name !== jestFnCall.head.local) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return fixer.removeRange([node.range[0], node.range[0] + 1]);
|
|
|
|
}
|
|
|
|
}]
|
2020-08-25 23:57:08 +00:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2022-11-10 10:43:16 +00:00
|
|
|
const onlyNode = jestFnCall.members.find(s => (0, _utils2.getAccessorValue)(s) === 'only');
|
|
|
|
if (!onlyNode) {
|
2020-08-25 23:57:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
context.report({
|
|
|
|
messageId: 'focusedTest',
|
2022-11-10 10:43:16 +00:00
|
|
|
node: onlyNode,
|
|
|
|
suggest: [{
|
|
|
|
messageId: 'suggestRemoveFocus',
|
|
|
|
fix: fixer => fixer.removeRange([onlyNode.range[0] - 1, onlyNode.range[1] + Number(onlyNode.type !== _utils.AST_NODE_TYPES.Identifier)])
|
|
|
|
}]
|
2020-08-25 23:57:08 +00:00
|
|
|
});
|
|
|
|
}
|
2022-11-10 10:43:16 +00:00
|
|
|
};
|
|
|
|
}
|
2020-08-25 23:57:08 +00:00
|
|
|
});
|
|
|
|
exports.default = _default;
|