eslint-plugin-jest
Advanced tools
Comparing version 28.5.0 to 28.6.0
@@ -7,3 +7,3 @@ "use strict"; | ||
var _globals = _interopRequireDefault(require("./globals.json")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } | ||
// copied from https://github.com/babel/babel/blob/d8da63c929f2d28c401571e2a43166678c555bc4/packages/babel-helpers/src/helpers.js#L602-L606 | ||
@@ -10,0 +10,0 @@ /* istanbul ignore next */ |
@@ -31,3 +31,3 @@ "use strict"; | ||
const findPromiseCallExpressionNode = node => node.parent?.parent && [_utils.AST_NODE_TYPES.CallExpression, _utils.AST_NODE_TYPES.ArrayExpression].includes(node.parent.type) ? getPromiseCallExpressionNode(node.parent) : null; | ||
const findFirstAsyncFunction = ({ | ||
const findFirstFunctionExpression = ({ | ||
parent | ||
@@ -38,4 +38,10 @@ }) => { | ||
} | ||
return (0, _utils2.isFunction)(parent) && parent.async ? parent : findFirstAsyncFunction(parent); | ||
return (0, _utils2.isFunction)(parent) ? parent : findFirstFunctionExpression(parent); | ||
}; | ||
const getNormalizeFunctionExpression = functionExpression => { | ||
if (functionExpression.parent.type === _utils.AST_NODE_TYPES.Property && functionExpression.type === _utils.AST_NODE_TYPES.FunctionExpression) { | ||
return functionExpression.parent; | ||
} | ||
return functionExpression; | ||
}; | ||
const getParentIfThenified = node => { | ||
@@ -119,2 +125,3 @@ const grandParentNode = node.parent?.parent; | ||
const arrayExceptions = new Set(); | ||
const descriptors = []; | ||
const pushPromiseArrayException = loc => arrayExceptions.add(promiseArrayExceptionKey(loc)); | ||
@@ -230,3 +237,3 @@ | ||
const shouldBeAwaited = jestFnCall.modifiers.some(nod => (0, _utils2.getAccessorValue)(nod) !== 'not') || asyncMatchers.includes((0, _utils2.getAccessorValue)(matcher)); | ||
if (!parentNode?.parent || !shouldBeAwaited) { | ||
if (!parentNode.parent || !shouldBeAwaited) { | ||
return; | ||
@@ -239,3 +246,2 @@ } | ||
const isParentArrayExpression = parentNode.parent.type === _utils.AST_NODE_TYPES.ArrayExpression; | ||
const orReturned = alwaysAwait ? '' : ' or returned'; | ||
/** | ||
@@ -253,26 +259,47 @@ * An async assertion can be chained with `then` or `catch` statements. | ||
!promiseArrayExceptionExists(finalNode.loc)) { | ||
descriptors.push({ | ||
node: finalNode, | ||
messageId: targetNode === finalNode ? 'asyncMustBeAwaited' : 'promisesWithAsyncAssertionsMustBeAwaited' | ||
}); | ||
} | ||
if (isParentArrayExpression) { | ||
pushPromiseArrayException(finalNode.loc); | ||
} | ||
}, | ||
'Program:exit'() { | ||
const fixes = []; | ||
descriptors.forEach(({ | ||
node, | ||
messageId | ||
}, index) => { | ||
const orReturned = alwaysAwait ? '' : ' or returned'; | ||
context.report({ | ||
loc: finalNode.loc, | ||
loc: node.loc, | ||
data: { | ||
orReturned | ||
}, | ||
messageId: finalNode === targetNode ? 'asyncMustBeAwaited' : 'promisesWithAsyncAssertionsMustBeAwaited', | ||
messageId, | ||
node, | ||
fix(fixer) { | ||
if (!findFirstAsyncFunction(finalNode)) { | ||
return []; | ||
const functionExpression = findFirstFunctionExpression(node); | ||
if (!functionExpression) { | ||
return null; | ||
} | ||
const returnStatement = finalNode.parent?.type === _utils.AST_NODE_TYPES.ReturnStatement ? finalNode.parent : null; | ||
const foundAsyncFixer = fixes.some(fix => fix.text === 'async '); | ||
if (!functionExpression.async && !foundAsyncFixer) { | ||
const targetFunction = getNormalizeFunctionExpression(functionExpression); | ||
fixes.push(fixer.insertTextBefore(targetFunction, 'async ')); | ||
} | ||
const returnStatement = node.parent?.type === _utils.AST_NODE_TYPES.ReturnStatement ? node.parent : null; | ||
if (alwaysAwait && returnStatement) { | ||
const sourceCodeText = (0, _utils2.getSourceCode)(context).getText(returnStatement); | ||
const replacedText = sourceCodeText.replace('return', 'await'); | ||
return fixer.replaceText(returnStatement, replacedText); | ||
fixes.push(fixer.replaceText(returnStatement, replacedText)); | ||
} else { | ||
fixes.push(fixer.insertTextBefore(node, 'await ')); | ||
} | ||
return fixer.insertTextBefore(finalNode, 'await '); | ||
return index === descriptors.length - 1 ? fixes : null; | ||
} | ||
}); | ||
if (isParentArrayExpression) { | ||
pushPromiseArrayException(finalNode.loc); | ||
} | ||
} | ||
}); | ||
} | ||
@@ -279,0 +306,0 @@ }; |
{ | ||
"name": "eslint-plugin-jest", | ||
"version": "28.5.0", | ||
"version": "28.6.0", | ||
"description": "ESLint rules for Jest", | ||
@@ -51,9 +51,2 @@ "keywords": [ | ||
"release": { | ||
"branches": [ | ||
"main", | ||
{ | ||
"name": "next", | ||
"prerelease": true | ||
} | ||
], | ||
"plugins": [ | ||
@@ -110,3 +103,3 @@ "@semantic-release/commit-analyzer", | ||
"rimraf": "^5.0.0", | ||
"semantic-release": "^23.0.0", | ||
"semantic-release": "^24.0.0", | ||
"semver": "^7.3.5", | ||
@@ -113,0 +106,0 @@ "strip-ansi": "^6.0.0", |
@@ -310,6 +310,11 @@ <div align="center"> | ||
[configuration](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations).\ | ||
🎨 Set in the `style` [configuration](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations).\ | ||
🔧 Automatically fixable by the | ||
🎨 | ||
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/latest/use/core-concepts#rule-suggestions). | ||
💡 | ||
Manually fixable by | ||
[editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions). | ||
@@ -354,2 +359,3 @@ | Name | Description | 💼 | ⚠️ | 🔧 | 💡 | | ||
| [prefer-importing-jest-globals](docs/rules/prefer-importing-jest-globals.md) | Prefer importing Jest globals | | | 🔧 | | | ||
| [prefer-jest-mocked](docs/rules/prefer-jest-mocked.md) | Prefer `jest.mocked()` over `fn as jest.Mock` | | | 🔧 | | | ||
| [prefer-lowercase-title](docs/rules/prefer-lowercase-title.md) | Enforce lowercase test names | | | 🔧 | | | ||
@@ -356,0 +362,0 @@ | [prefer-mock-promise-shorthand](docs/rules/prefer-mock-promise-shorthand.md) | Prefer mock resolved/rejected shorthands for promises | | | 🔧 | | |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
320628
119
5437
426