Skip to content

Commit b7a548a

Browse files
committed
fix(no-deprecated-functions): make missing jest a rule error, not fatal
1 parent 4851e6b commit b7a548a

File tree

2 files changed

+55
-32
lines changed

2 files changed

+55
-32
lines changed

src/rules/no-deprecated-functions.ts

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,11 @@
11
import { AST_NODE_TYPES, type TSESTree } from '@typescript-eslint/utils';
22
import {
3-
type JestVersion,
3+
type EslintPluginJestRuleContext,
44
createRule,
5-
detectJestVersion,
5+
getJestVersion,
66
getNodeName,
77
} from './utils';
88

9-
interface ContextSettings {
10-
jest?: EslintPluginJestSettings;
11-
}
12-
13-
interface EslintPluginJestSettings {
14-
version: JestVersion | string;
15-
}
16-
17-
const parseJestVersion = (rawVersion: number | string): JestVersion => {
18-
if (typeof rawVersion === 'number') {
19-
return rawVersion;
20-
}
21-
22-
const [majorVersion] = rawVersion.split('.');
23-
24-
return parseInt(majorVersion, 10);
25-
};
26-
279
export default createRule({
2810
name: __filename,
2911
meta: {
@@ -33,17 +15,20 @@ export default createRule({
3315
messages: {
3416
deprecatedFunction:
3517
'`{{ deprecation }}` has been deprecated in favor of `{{ replacement }}`',
18+
jestNotDetected:
19+
'Unable to detect Jest version - please ensure jest package is installed, or otherwise set version explicitly',
3620
},
3721
type: 'suggestion',
3822
schema: [],
3923
fixable: 'code',
4024
},
4125
defaultOptions: [],
4226
create(context) {
43-
const jestVersion = parseJestVersion(
44-
(context.settings as ContextSettings)?.jest?.version ||
45-
detectJestVersion(),
46-
);
27+
// If jest version is not detected, it is set to Infinity so that all possible deprecations
28+
// are reported with a "jest not detected" error message
29+
const jestVersion =
30+
getJestVersion(context as EslintPluginJestRuleContext) || Infinity;
31+
const jestNotDetected = jestVersion === Infinity;
4732

4833
const deprecations: Record<string, string> = {
4934
...(jestVersion >= 15 && {
@@ -80,13 +65,16 @@ export default createRule({
8065
const { callee } = node;
8166

8267
context.report({
83-
messageId: 'deprecatedFunction',
68+
messageId: jestNotDetected ? 'jestNotDetected' : 'deprecatedFunction',
8469
data: {
8570
deprecation,
8671
replacement,
8772
},
8873
node,
8974
fix(fixer) {
75+
if (jestNotDetected) {
76+
return [];
77+
}
9078
let [name, func] = replacement.split('.');
9179

9280
if (callee.property.type === AST_NODE_TYPES.Literal) {

src/rules/utils/detectJestVersion.ts

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
import type { JSONSchemaForNPMPackageJsonFiles } from '@schemastore/package';
2+
import type { RuleContext } from '@typescript-eslint/utils/ts-eslint';
3+
4+
interface ContextSettings {
5+
[key: string]: unknown;
6+
jest?: EslintPluginJestSettings;
7+
}
8+
9+
interface EslintPluginJestSettings {
10+
version: JestVersion | string;
11+
}
12+
13+
export interface EslintPluginJestRuleContext
14+
extends Readonly<RuleContext<never, []>> {
15+
settings: ContextSettings;
16+
}
217

318
export type JestVersion =
419
| 14
@@ -22,7 +37,25 @@ export type JestVersion =
2237

2338
let cachedJestVersion: JestVersion | null = null;
2439

25-
export const detectJestVersion = (): JestVersion => {
40+
const parseJestVersion = (rawVersion: number | string): JestVersion => {
41+
if (typeof rawVersion === 'number') {
42+
return rawVersion;
43+
}
44+
45+
const [majorVersion] = rawVersion.split('.');
46+
47+
return parseInt(majorVersion, 10);
48+
};
49+
50+
export const getContextJestVersion = (
51+
context: EslintPluginJestRuleContext,
52+
): JestVersion | null => {
53+
return context.settings.jest?.version
54+
? parseJestVersion(context.settings.jest.version)
55+
: null;
56+
};
57+
58+
export const detectJestVersion = (): JestVersion | null => {
2659
if (cachedJestVersion) {
2760
return cachedJestVersion;
2861
}
@@ -35,13 +68,15 @@ export const detectJestVersion = (): JestVersion => {
3568
require(jestPath) as JSONSchemaForNPMPackageJsonFiles;
3669

3770
if (jestPackageJson.version) {
38-
const [majorVersion] = jestPackageJson.version.split('.');
39-
40-
return (cachedJestVersion = parseInt(majorVersion, 10));
71+
return (cachedJestVersion = parseJestVersion(jestPackageJson.version));
4172
}
4273
} catch {}
4374

44-
throw new Error(
45-
'Unable to detect Jest version - please ensure jest package is installed, or otherwise set version explicitly',
46-
);
75+
return null;
76+
};
77+
78+
export const getJestVersion = (
79+
context: EslintPluginJestRuleContext,
80+
): JestVersion | null => {
81+
return getContextJestVersion(context) || detectJestVersion();
4782
};

0 commit comments

Comments
 (0)