Skip to content

Commit f564723

Browse files
committed
Merge branch 'jest-community:main' into prefer-jest-globals
2 parents cddb7f3 + 34b9696 commit f564723

13 files changed

+697
-522
lines changed

.yarn/releases/yarn-3.8.0.cjs renamed to .yarn/releases/yarn-3.8.1.cjs

+172-172
Large diffs are not rendered by default.

.yarnrc.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ plugins:
66
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
77
spec: '@yarnpkg/plugin-interactive-tools'
88

9-
yarnPath: .yarn/releases/yarn-3.8.0.cjs
9+
yarnPath: .yarn/releases/yarn-3.8.1.cjs

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ set to warn in.\
318318
🎨 Set in the `style` [configuration](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations).\
319319
🔧 Automatically fixable by the
320320
[`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).\
321-
💡 Manually fixable by [editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).\
321+
💡 Manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).\
322322
❌ Deprecated.
323323

324324
| Name                          | Description | 💼 | ⚠️ | 🔧 | 💡 ||

docs/rules/no-done-callback.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[config](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations).
55

66
💡 This rule is manually fixable by
7-
[editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).
7+
[editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
88

99
<!-- end auto-generated rule header -->
1010

docs/rules/no-focused-tests.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[config](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations).
55

66
💡 This rule is manually fixable by
7-
[editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).
7+
[editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
88

99
<!-- end auto-generated rule header -->
1010

docs/rules/prefer-equality-matcher.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Suggest using the built-in equality matchers (`prefer-equality-matcher`)
22

33
💡 This rule is manually fixable by
4-
[editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).
4+
[editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
55

66
<!-- end auto-generated rule header -->
77

docs/rules/prefer-expect-assertions.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Suggest using `expect.assertions()` OR `expect.hasAssertions()` (`prefer-expect-assertions`)
22

33
💡 This rule is manually fixable by
4-
[editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).
4+
[editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
55

66
<!-- end auto-generated rule header -->
77

docs/rules/prefer-strict-equal.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Suggest using `toStrictEqual()` (`prefer-strict-equal`)
22

33
💡 This rule is manually fixable by
4-
[editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).
4+
[editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
55

66
<!-- end auto-generated rule header -->
77

docs/rules/unbound-method.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Enforce unbound methods are called with their expected scope (`unbound-method`)
22

3-
💭 This rule requires type information.
3+
💭 This rule requires
4+
[type information](https://typescript-eslint.io/linting/typed-linting).
45

56
<!-- end auto-generated rule header -->
67

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135
"pinst": "^3.0.0",
136136
"prettier": "^3.0.0",
137137
"rimraf": "^5.0.0",
138-
"semantic-release": "^22.0.0",
138+
"semantic-release": "^23.0.0",
139139
"semver": "^7.3.5",
140140
"strip-ansi": "^6.0.0",
141141
"ts-node": "^10.2.1",
@@ -154,7 +154,7 @@
154154
"optional": true
155155
}
156156
},
157-
"packageManager": "[email protected].0",
157+
"packageManager": "[email protected].1",
158158
"engines": {
159159
"node": "^14.15.0 || ^16.10.0 || >=18.0.0"
160160
},

src/rules/__tests__/prefer-importing-jest-globals.test.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ const importImports = [
2929
parserOptions: { sourceType: 'module' },
3030
errors: [
3131
{
32-
endColumn: 7,
33-
column: 3,
34-
line: 3,
32+
endColumn: 9,
33+
column: 1,
34+
line: 2,
3535
messageId: 'preferImportingJestGlobal',
3636
} as const,
3737
],
@@ -54,9 +54,9 @@ const importImports = [
5454
parserOptions: { sourceType: 'module' },
5555
errors: [
5656
{
57-
endColumn: 7,
58-
column: 3,
59-
line: 3,
57+
endColumn: 9,
58+
column: 1,
59+
line: 2,
6060
messageId: 'preferImportingJestGlobal',
6161
} as const,
6262
],
@@ -186,17 +186,17 @@ const requireImports = [
186186
})
187187
`,
188188
output: dedent`
189-
const { test, expect } = require('@jest/globals');
189+
const { describe, test, expect } = require('@jest/globals');
190190
describe("suite", () => {
191191
test("foo");
192192
expect(true).toBeDefined();
193193
})
194194
`,
195195
errors: [
196196
{
197-
endColumn: 7,
198-
column: 3,
199-
line: 3,
197+
endColumn: 9,
198+
column: 1,
199+
line: 2,
200200
messageId: 'preferImportingJestGlobal',
201201
} as const,
202202
],

src/rules/prefer-importing-jest-globals.ts

+58-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
1+
import { AST_NODE_TYPES, type TSESTree } from '@typescript-eslint/utils';
2+
import jestFnNames from '../globals.json';
23
import {
34
type ParsedJestFnCall,
45
createRule,
@@ -31,11 +32,32 @@ export default createRule({
3132
},
3233
defaultOptions: [],
3334
create(context) {
35+
const jestFunctionNames: string[] = Object.keys(jestFnNames);
3436
const importedJestFunctions: string[] = [];
3537
const usedJestFunctions: ParsedJestFnCall[] = [];
3638

3739
return {
38-
CallExpression(node) {
40+
CallExpression(node: TSESTree.CallExpression) {
41+
if (
42+
node.type === AST_NODE_TYPES.CallExpression &&
43+
node.callee.type === AST_NODE_TYPES.Identifier &&
44+
jestFunctionNames.includes(node.callee.name)
45+
) {
46+
const jestFnCall: ParsedJestFnCall = {
47+
head: {
48+
type: 'import',
49+
node: node.callee,
50+
original: node.callee.name,
51+
local: node.callee.name,
52+
},
53+
name: node.callee.name,
54+
type: 'jest',
55+
members: [],
56+
};
57+
58+
usedJestFunctions.push(jestFnCall);
59+
}
60+
3961
const jestFnCall = parseJestFnCall(node, context);
4062

4163
if (!jestFnCall) {
@@ -48,6 +70,38 @@ export default createRule({
4870

4971
usedJestFunctions.push(jestFnCall);
5072
},
73+
// if (
74+
// node.init &&
75+
// node.init.type === AST_NODE_TYPES.CallExpression &&
76+
// node.init.callee.type === AST_NODE_TYPES.Identifier &&
77+
// node.init.callee.name === 'require' &&
78+
// node.init.arguments[0]?.type === 'Literal' &&
79+
// node.init.arguments[0]?.value === '@jest/globals'
80+
// ) {
81+
// if (node.id.type === AST_NODE_TYPES.Identifier) {
82+
// importedJestFunctions.push(node.id.name);
83+
// } else if (node.id.type === AST_NODE_TYPES.ObjectPattern) {
84+
// node.id.properties.forEach(property => {
85+
// if (
86+
// property.type === AST_NODE_TYPES.Property &&
87+
// property.key.type === AST_NODE_TYPES.Identifier
88+
// ) {
89+
// importedJestFunctions.push(property.key.name);
90+
// }
91+
// });
92+
// }
93+
// }
94+
95+
// // if (node.id.name) {
96+
// // usedJestFunctions.push({
97+
// // name: node.id.name,
98+
// // type: 'jest',
99+
// // // Assign a valid value of type 'ResolvedJestFnWithNode' here
100+
// // head: { type: 'jest', node: node },
101+
// // members: [],
102+
// // });
103+
// // }
104+
// },
51105
'Program:exit'() {
52106
const jestFunctionsToReport = usedJestFunctions.filter(
53107
jestFunction => !importedJestFunctions.includes(jestFunction.name),
@@ -56,8 +110,8 @@ export default createRule({
56110
if (!jestFunctionsToReport.length) {
57111
return;
58112
}
59-
const jestFunctionsToImport = jestFunctionsToReport.map(
60-
jestFunction => jestFunction.name,
113+
const jestFunctionsToImport = Array.from(
114+
new Set(jestFunctionsToReport.map(jestFunction => jestFunction.name)),
61115
);
62116
const reportingNode = jestFunctionsToReport[0].head.node;
63117

0 commit comments

Comments
 (0)