Skip to content

Commit

Permalink
Merge branch 'jest-community:main' into prefer-jest-globals
Browse files Browse the repository at this point in the history
  • Loading branch information
MadeinFrance committed Mar 19, 2024
2 parents cddb7f3 + 34b9696 commit f564723
Show file tree
Hide file tree
Showing 13 changed files with 697 additions and 522 deletions.
344 changes: 172 additions & 172 deletions .yarn/releases/yarn-3.8.0.cjs → .yarn/releases/yarn-3.8.1.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ plugins:
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
spec: '@yarnpkg/plugin-interactive-tools'

yarnPath: .yarn/releases/yarn-3.8.0.cjs
yarnPath: .yarn/releases/yarn-3.8.1.cjs
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ set to warn in.\
🎨 Set in the `style` [configuration](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations).\
🔧 Automatically fixable by the
[`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix).\
💡 Manually fixable by [editor suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).\
💡 Manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).\
❌ Deprecated.

| Name                          | Description | 💼 | ⚠️ | 🔧 | 💡 ||
Expand Down
2 changes: 1 addition & 1 deletion docs/rules/no-done-callback.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[config](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations).

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

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

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/no-focused-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[config](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations).

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

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

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/prefer-equality-matcher.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Suggest using the built-in equality matchers (`prefer-equality-matcher`)

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

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

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/prefer-expect-assertions.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Suggest using `expect.assertions()` OR `expect.hasAssertions()` (`prefer-expect-assertions`)

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

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

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/prefer-strict-equal.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Suggest using `toStrictEqual()` (`prefer-strict-equal`)

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

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

Expand Down
3 changes: 2 additions & 1 deletion docs/rules/unbound-method.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Enforce unbound methods are called with their expected scope (`unbound-method`)

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

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

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@
"pinst": "^3.0.0",
"prettier": "^3.0.0",
"rimraf": "^5.0.0",
"semantic-release": "^22.0.0",
"semantic-release": "^23.0.0",
"semver": "^7.3.5",
"strip-ansi": "^6.0.0",
"ts-node": "^10.2.1",
Expand All @@ -154,7 +154,7 @@
"optional": true
}
},
"packageManager": "yarn@3.8.0",
"packageManager": "yarn@3.8.1",
"engines": {
"node": "^14.15.0 || ^16.10.0 || >=18.0.0"
},
Expand Down
20 changes: 10 additions & 10 deletions src/rules/__tests__/prefer-importing-jest-globals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ const importImports = [
parserOptions: { sourceType: 'module' },
errors: [
{
endColumn: 7,
column: 3,
line: 3,
endColumn: 9,
column: 1,
line: 2,
messageId: 'preferImportingJestGlobal',
} as const,
],
Expand All @@ -54,9 +54,9 @@ const importImports = [
parserOptions: { sourceType: 'module' },
errors: [
{
endColumn: 7,
column: 3,
line: 3,
endColumn: 9,
column: 1,
line: 2,
messageId: 'preferImportingJestGlobal',
} as const,
],
Expand Down Expand Up @@ -186,17 +186,17 @@ const requireImports = [
})
`,
output: dedent`
const { test, expect } = require('@jest/globals');
const { describe, test, expect } = require('@jest/globals');
describe("suite", () => {
test("foo");
expect(true).toBeDefined();
})
`,
errors: [
{
endColumn: 7,
column: 3,
line: 3,
endColumn: 9,
column: 1,
line: 2,
messageId: 'preferImportingJestGlobal',
} as const,
],
Expand Down
62 changes: 58 additions & 4 deletions src/rules/prefer-importing-jest-globals.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import { AST_NODE_TYPES, type TSESTree } from '@typescript-eslint/utils';
import jestFnNames from '../globals.json';
import {
type ParsedJestFnCall,
createRule,
Expand Down Expand Up @@ -31,11 +32,32 @@ export default createRule({
},
defaultOptions: [],
create(context) {
const jestFunctionNames: string[] = Object.keys(jestFnNames);
const importedJestFunctions: string[] = [];
const usedJestFunctions: ParsedJestFnCall[] = [];

return {
CallExpression(node) {
CallExpression(node: TSESTree.CallExpression) {
if (
node.type === AST_NODE_TYPES.CallExpression &&
node.callee.type === AST_NODE_TYPES.Identifier &&
jestFunctionNames.includes(node.callee.name)
) {
const jestFnCall: ParsedJestFnCall = {
head: {
type: 'import',
node: node.callee,
original: node.callee.name,
local: node.callee.name,
},
name: node.callee.name,
type: 'jest',
members: [],
};

usedJestFunctions.push(jestFnCall);
}

const jestFnCall = parseJestFnCall(node, context);

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

usedJestFunctions.push(jestFnCall);
},
// if (
// node.init &&
// node.init.type === AST_NODE_TYPES.CallExpression &&
// node.init.callee.type === AST_NODE_TYPES.Identifier &&
// node.init.callee.name === 'require' &&
// node.init.arguments[0]?.type === 'Literal' &&
// node.init.arguments[0]?.value === '@jest/globals'
// ) {
// if (node.id.type === AST_NODE_TYPES.Identifier) {
// importedJestFunctions.push(node.id.name);
// } else if (node.id.type === AST_NODE_TYPES.ObjectPattern) {
// node.id.properties.forEach(property => {
// if (
// property.type === AST_NODE_TYPES.Property &&
// property.key.type === AST_NODE_TYPES.Identifier
// ) {
// importedJestFunctions.push(property.key.name);
// }
// });
// }
// }

// // if (node.id.name) {
// // usedJestFunctions.push({
// // name: node.id.name,
// // type: 'jest',
// // // Assign a valid value of type 'ResolvedJestFnWithNode' here
// // head: { type: 'jest', node: node },
// // members: [],
// // });
// // }
// },
'Program:exit'() {
const jestFunctionsToReport = usedJestFunctions.filter(
jestFunction => !importedJestFunctions.includes(jestFunction.name),
Expand All @@ -56,8 +110,8 @@ export default createRule({
if (!jestFunctionsToReport.length) {
return;
}
const jestFunctionsToImport = jestFunctionsToReport.map(
jestFunction => jestFunction.name,
const jestFunctionsToImport = Array.from(
new Set(jestFunctionsToReport.map(jestFunction => jestFunction.name)),
);
const reportingNode = jestFunctionsToReport[0].head.node;

Expand Down

0 comments on commit f564723

Please sign in to comment.