2020-03-07 11:45:40 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
|
|
value: true
|
|
|
|
});
|
|
|
|
exports.default = void 0;
|
|
|
|
|
|
|
|
var _utils = require("./utils");
|
|
|
|
|
|
|
|
var _default = (0, _utils.createRule)({
|
|
|
|
name: __filename,
|
|
|
|
meta: {
|
|
|
|
docs: {
|
|
|
|
category: 'Best Practices',
|
|
|
|
description: 'Disallow alias methods',
|
2020-07-14 01:09:22 +08:00
|
|
|
recommended: false
|
2020-03-07 11:45:40 +08:00
|
|
|
},
|
|
|
|
messages: {
|
2020-03-31 20:40:00 +08:00
|
|
|
replaceAlias: `Replace {{ alias }}() with its canonical name of {{ canonical }}()`
|
2020-03-07 11:45:40 +08:00
|
|
|
},
|
|
|
|
fixable: 'code',
|
|
|
|
type: 'suggestion',
|
|
|
|
schema: []
|
|
|
|
},
|
|
|
|
defaultOptions: [],
|
|
|
|
|
|
|
|
create(context) {
|
2020-03-31 20:40:00 +08:00
|
|
|
// map of jest matcher aliases & their canonical names
|
|
|
|
const methodNames = {
|
|
|
|
toBeCalled: 'toHaveBeenCalled',
|
|
|
|
toBeCalledTimes: 'toHaveBeenCalledTimes',
|
|
|
|
toBeCalledWith: 'toHaveBeenCalledWith',
|
|
|
|
lastCalledWith: 'toHaveBeenLastCalledWith',
|
|
|
|
nthCalledWith: 'toHaveBeenNthCalledWith',
|
|
|
|
toReturn: 'toHaveReturned',
|
|
|
|
toReturnTimes: 'toHaveReturnedTimes',
|
|
|
|
toReturnWith: 'toHaveReturnedWith',
|
|
|
|
lastReturnedWith: 'toHaveLastReturnedWith',
|
|
|
|
nthReturnedWith: 'toHaveNthReturnedWith',
|
|
|
|
toThrowError: 'toThrow'
|
|
|
|
};
|
2020-03-07 11:45:40 +08:00
|
|
|
return {
|
|
|
|
CallExpression(node) {
|
|
|
|
if (!(0, _utils.isExpectCall)(node)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-31 20:40:00 +08:00
|
|
|
const {
|
|
|
|
matcher
|
|
|
|
} = (0, _utils.parseExpectCall)(node);
|
2020-03-07 11:45:40 +08:00
|
|
|
|
|
|
|
if (!matcher) {
|
|
|
|
return;
|
2020-03-31 20:40:00 +08:00
|
|
|
}
|
2020-03-07 11:45:40 +08:00
|
|
|
|
2020-03-31 20:40:00 +08:00
|
|
|
const alias = matcher.name;
|
2020-03-07 11:45:40 +08:00
|
|
|
|
2020-03-31 20:40:00 +08:00
|
|
|
if (alias in methodNames) {
|
|
|
|
const canonical = methodNames[alias];
|
2020-03-07 11:45:40 +08:00
|
|
|
context.report({
|
|
|
|
messageId: 'replaceAlias',
|
|
|
|
data: {
|
2020-03-31 20:40:00 +08:00
|
|
|
alias,
|
|
|
|
canonical
|
2020-03-07 11:45:40 +08:00
|
|
|
},
|
|
|
|
node: matcher.node.property,
|
2020-03-31 20:40:00 +08:00
|
|
|
fix: fixer => [fixer.replaceText(matcher.node.property, canonical)]
|
2020-03-07 11:45:40 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
exports.default = _default;
|