2020-08-26 01:57:08 +02:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
|
|
value: true
|
|
|
|
});
|
2022-11-10 11:43:16 +01:00
|
|
|
exports.default = void 0;
|
|
|
|
var _utils = require("@typescript-eslint/utils");
|
|
|
|
var _utils2 = require("./utils");
|
|
|
|
const parseJestVersion = rawVersion => {
|
|
|
|
if (typeof rawVersion === 'number') {
|
|
|
|
return rawVersion;
|
2020-08-26 01:57:08 +02:00
|
|
|
}
|
2022-11-10 11:43:16 +01:00
|
|
|
const [majorVersion] = rawVersion.split('.');
|
|
|
|
return parseInt(majorVersion, 10);
|
2020-08-26 01:57:08 +02:00
|
|
|
};
|
2022-11-10 11:43:16 +01:00
|
|
|
var _default = (0, _utils2.createRule)({
|
2020-08-26 01:57:08 +02:00
|
|
|
name: __filename,
|
|
|
|
meta: {
|
|
|
|
docs: {
|
|
|
|
category: 'Best Practices',
|
|
|
|
description: 'Disallow use of deprecated functions',
|
2021-02-26 04:58:33 +01:00
|
|
|
recommended: 'error'
|
2020-08-26 01:57:08 +02:00
|
|
|
},
|
|
|
|
messages: {
|
|
|
|
deprecatedFunction: '`{{ deprecation }}` has been deprecated in favor of `{{ replacement }}`'
|
|
|
|
},
|
|
|
|
type: 'suggestion',
|
|
|
|
schema: [],
|
|
|
|
fixable: 'code'
|
|
|
|
},
|
|
|
|
defaultOptions: [],
|
|
|
|
create(context) {
|
2021-02-26 04:58:33 +01:00
|
|
|
var _context$settings, _context$settings$jes;
|
2022-11-10 11:43:16 +01:00
|
|
|
const jestVersion = parseJestVersion(((_context$settings = context.settings) === null || _context$settings === void 0 ? void 0 : (_context$settings$jes = _context$settings.jest) === null || _context$settings$jes === void 0 ? void 0 : _context$settings$jes.version) || (0, _utils2.detectJestVersion)());
|
|
|
|
const deprecations = {
|
|
|
|
...(jestVersion >= 15 && {
|
2021-02-26 04:58:33 +01:00
|
|
|
'jest.resetModuleRegistry': 'jest.resetModules'
|
|
|
|
}),
|
|
|
|
...(jestVersion >= 17 && {
|
|
|
|
'jest.addMatchers': 'expect.extend'
|
|
|
|
}),
|
|
|
|
...(jestVersion >= 21 && {
|
|
|
|
'require.requireMock': 'jest.requireMock',
|
|
|
|
'require.requireActual': 'jest.requireActual'
|
|
|
|
}),
|
|
|
|
...(jestVersion >= 22 && {
|
|
|
|
'jest.runTimersToTime': 'jest.advanceTimersByTime'
|
|
|
|
}),
|
|
|
|
...(jestVersion >= 26 && {
|
|
|
|
'jest.genMockFromModule': 'jest.createMockFromModule'
|
|
|
|
})
|
|
|
|
};
|
2020-08-26 01:57:08 +02:00
|
|
|
return {
|
|
|
|
CallExpression(node) {
|
2022-11-10 11:43:16 +01:00
|
|
|
if (node.callee.type !== _utils.AST_NODE_TYPES.MemberExpression) {
|
2020-08-26 01:57:08 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-11-10 11:43:16 +01:00
|
|
|
const deprecation = (0, _utils2.getNodeName)(node);
|
2020-08-26 01:57:08 +02:00
|
|
|
if (!deprecation || !(deprecation in deprecations)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const replacement = deprecations[deprecation];
|
|
|
|
const {
|
|
|
|
callee
|
|
|
|
} = node;
|
|
|
|
context.report({
|
|
|
|
messageId: 'deprecatedFunction',
|
|
|
|
data: {
|
|
|
|
deprecation,
|
|
|
|
replacement
|
|
|
|
},
|
|
|
|
node,
|
|
|
|
fix(fixer) {
|
|
|
|
let [name, func] = replacement.split('.');
|
2022-11-10 11:43:16 +01:00
|
|
|
if (callee.property.type === _utils.AST_NODE_TYPES.Literal) {
|
2020-08-26 01:57:08 +02:00
|
|
|
func = `'${func}'`;
|
|
|
|
}
|
|
|
|
return [fixer.replaceText(callee.object, name), fixer.replaceText(callee.property, func)];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
exports.default = _default;
|