-
Notifications
You must be signed in to change notification settings - Fork 238
/
no-deprecated-functions.ts
105 lines (91 loc) · 2.55 KB
/
no-deprecated-functions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { AST_NODE_TYPES, type TSESTree } from '@typescript-eslint/utils';
import {
type JestVersion,
createRule,
detectJestVersion,
getNodeName,
} from './utils';
interface ContextSettings {
jest?: EslintPluginJestSettings;
}
interface EslintPluginJestSettings {
version: JestVersion | string;
}
const parseJestVersion = (rawVersion: number | string): JestVersion => {
if (typeof rawVersion === 'number') {
return rawVersion;
}
const [majorVersion] = rawVersion.split('.');
return parseInt(majorVersion, 10);
};
export default createRule({
name: __filename,
meta: {
docs: {
description: 'Disallow use of deprecated functions',
},
messages: {
deprecatedFunction:
'`{{ deprecation }}` has been deprecated in favor of `{{ replacement }}`',
},
type: 'suggestion',
schema: [],
fixable: 'code',
},
defaultOptions: [],
create(context) {
const jestVersion = parseJestVersion(
(context.settings as ContextSettings)?.jest?.version ||
detectJestVersion(),
);
const deprecations: Record<string, string> = {
...(jestVersion >= 15 && {
'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',
}),
};
return {
CallExpression(node: TSESTree.CallExpression) {
if (node.callee.type !== AST_NODE_TYPES.MemberExpression) {
return;
}
const deprecation = getNodeName(node);
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('.');
if (callee.property.type === AST_NODE_TYPES.Literal) {
func = `'${func}'`;
}
return [
fixer.replaceText(callee.object, name),
fixer.replaceText(callee.property, func),
];
},
});
},
};
},
});