| [9af201e] | 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.default = void 0;
|
|---|
| 7 |
|
|---|
| 8 | var _utils = require("./utils");
|
|---|
| 9 |
|
|---|
| 10 | var _default = (0, _utils.createRule)({
|
|---|
| 11 | name: __filename,
|
|---|
| 12 | meta: {
|
|---|
| 13 | docs: {
|
|---|
| 14 | category: 'Best Practices',
|
|---|
| 15 | description: 'Disallow alias methods',
|
|---|
| 16 | recommended: false
|
|---|
| 17 | },
|
|---|
| 18 | messages: {
|
|---|
| 19 | replaceAlias: `Replace {{ alias }}() with its canonical name of {{ canonical }}()`
|
|---|
| 20 | },
|
|---|
| 21 | fixable: 'code',
|
|---|
| 22 | type: 'suggestion',
|
|---|
| 23 | schema: []
|
|---|
| 24 | },
|
|---|
| 25 | defaultOptions: [],
|
|---|
| 26 |
|
|---|
| 27 | create(context) {
|
|---|
| 28 | // map of jest matcher aliases & their canonical names
|
|---|
| 29 | const methodNames = {
|
|---|
| 30 | toBeCalled: 'toHaveBeenCalled',
|
|---|
| 31 | toBeCalledTimes: 'toHaveBeenCalledTimes',
|
|---|
| 32 | toBeCalledWith: 'toHaveBeenCalledWith',
|
|---|
| 33 | lastCalledWith: 'toHaveBeenLastCalledWith',
|
|---|
| 34 | nthCalledWith: 'toHaveBeenNthCalledWith',
|
|---|
| 35 | toReturn: 'toHaveReturned',
|
|---|
| 36 | toReturnTimes: 'toHaveReturnedTimes',
|
|---|
| 37 | toReturnWith: 'toHaveReturnedWith',
|
|---|
| 38 | lastReturnedWith: 'toHaveLastReturnedWith',
|
|---|
| 39 | nthReturnedWith: 'toHaveNthReturnedWith',
|
|---|
| 40 | toThrowError: 'toThrow'
|
|---|
| 41 | };
|
|---|
| 42 | return {
|
|---|
| 43 | CallExpression(node) {
|
|---|
| 44 | if (!(0, _utils.isExpectCall)(node)) {
|
|---|
| 45 | return;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | const {
|
|---|
| 49 | matcher
|
|---|
| 50 | } = (0, _utils.parseExpectCall)(node);
|
|---|
| 51 |
|
|---|
| 52 | if (!matcher) {
|
|---|
| 53 | return;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | const alias = matcher.name;
|
|---|
| 57 |
|
|---|
| 58 | if (alias in methodNames) {
|
|---|
| 59 | const canonical = methodNames[alias];
|
|---|
| 60 | context.report({
|
|---|
| 61 | messageId: 'replaceAlias',
|
|---|
| 62 | data: {
|
|---|
| 63 | alias,
|
|---|
| 64 | canonical
|
|---|
| 65 | },
|
|---|
| 66 | node: matcher.node.property,
|
|---|
| 67 | fix: fixer => [fixer.replaceText(matcher.node.property, canonical)]
|
|---|
| 68 | });
|
|---|
| 69 | }
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | };
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | });
|
|---|
| 76 |
|
|---|
| 77 | exports.default = _default; |
|---|