eslint-plugin-jest
Advanced tools
Comparing version 25.3.2 to 25.3.3
@@ -0,1 +1,9 @@ | ||
## [25.3.3](https://github.com/jest-community/eslint-plugin-jest/compare/v25.3.2...v25.3.3) (2021-12-30) | ||
### Bug Fixes | ||
* **prefer-to-contain:** support square bracket accessors ([#1009](https://github.com/jest-community/eslint-plugin-jest/issues/1009)) ([73984a7](https://github.com/jest-community/eslint-plugin-jest/commit/73984a79f790986a17116589a587506bcc10efc0)) | ||
* **prefer-to-have-length:** support square bracket accessors ([#1010](https://github.com/jest-community/eslint-plugin-jest/issues/1010)) ([9e70f55](https://github.com/jest-community/eslint-plugin-jest/commit/9e70f550e341432f69a1cd334c19df87513ea906)) | ||
## [25.3.2](https://github.com/jest-community/eslint-plugin-jest/compare/v25.3.1...v25.3.2) (2021-12-27) | ||
@@ -2,0 +10,0 @@ |
@@ -35,50 +35,6 @@ "use strict"; | ||
* @return {node is FixableIncludesCallExpression} | ||
* | ||
* @todo support `['includes']()` syntax (remove last property.type check to begin) | ||
* @todo break out into `isMethodCall<Name extends string>(node: TSESTree.Node, method: Name)` util-fn | ||
*/ | ||
const isFixableIncludesCallExpression = node => node.type === _experimentalUtils.AST_NODE_TYPES.CallExpression && node.callee.type === _experimentalUtils.AST_NODE_TYPES.MemberExpression && (0, _utils.isSupportedAccessor)(node.callee.property, 'includes') && node.callee.property.type === _experimentalUtils.AST_NODE_TYPES.Identifier && (0, _utils.hasOnlyOneArgument)(node); | ||
const isFixableIncludesCallExpression = node => node.type === _experimentalUtils.AST_NODE_TYPES.CallExpression && node.callee.type === _experimentalUtils.AST_NODE_TYPES.MemberExpression && (0, _utils.isSupportedAccessor)(node.callee.property, 'includes') && (0, _utils.hasOnlyOneArgument)(node); // expect(array.includes(<value>)[not.]{toBe,toEqual}(<boolean>) | ||
const buildToContainFuncExpectation = negated => negated ? `${_utils.ModifierName.not}.toContain` : 'toContain'; | ||
/** | ||
* Finds the first `.` character token between the `object` & `property` of the given `member` expression. | ||
* | ||
* @param {TSESTree.MemberExpression} member | ||
* @param {SourceCode} sourceCode | ||
* | ||
* @return {Token | null} | ||
*/ | ||
const findPropertyDotToken = (member, sourceCode) => sourceCode.getFirstTokenBetween(member.object, member.property, token => token.value === '.'); | ||
const getNegationFixes = (node, modifier, matcher, sourceCode, fixer, fileName) => { | ||
const [containArg] = node.arguments; | ||
const negationPropertyDot = findPropertyDotToken(modifier.node, sourceCode); | ||
const toContainFunc = buildToContainFuncExpectation((0, _utils.followTypeAssertionChain)(matcher.arguments[0]).value); | ||
/* istanbul ignore if */ | ||
if (negationPropertyDot === null) { | ||
throw new Error(`Unexpected null when attempting to fix ${fileName} - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`); | ||
} | ||
return [fixer.remove(negationPropertyDot), fixer.remove(modifier.node.property), fixer.replaceText(matcher.node.property, toContainFunc), fixer.replaceText(matcher.arguments[0], sourceCode.getText(containArg))]; | ||
}; | ||
const getCommonFixes = (node, sourceCode, fileName) => { | ||
const [containArg] = node.arguments; | ||
const includesCallee = node.callee; | ||
const propertyDot = findPropertyDotToken(includesCallee, sourceCode); | ||
const closingParenthesis = sourceCode.getTokenAfter(containArg); | ||
const openParenthesis = sourceCode.getTokenBefore(containArg); | ||
/* istanbul ignore if */ | ||
if (propertyDot === null || closingParenthesis === null || openParenthesis === null) { | ||
throw new Error(`Unexpected null when attempting to fix ${fileName} - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`); | ||
} | ||
return [containArg, includesCallee.property, propertyDot, closingParenthesis, openParenthesis]; | ||
}; // expect(array.includes(<value>)[not.]{toBe,toEqual}(<boolean>) | ||
var _default = (0, _utils.createRule)({ | ||
@@ -110,3 +66,4 @@ name: __filename, | ||
expect: { | ||
arguments: [includesCall] | ||
arguments: [includesCall], | ||
range: [, expectCallEnd] | ||
}, | ||
@@ -123,15 +80,10 @@ matcher, | ||
fix(fixer) { | ||
const sourceCode = context.getSourceCode(); | ||
const fileName = context.getFilename(); | ||
const fixArr = getCommonFixes(includesCall, sourceCode, fileName).map(target => fixer.remove(target)); | ||
const sourceCode = context.getSourceCode(); // we need to negate the expectation if the current expected | ||
// value is itself negated by the "not" modifier | ||
if (modifier) { | ||
return getNegationFixes(includesCall, modifier, matcher, sourceCode, fixer, fileName).concat(fixArr); | ||
} | ||
const toContainFunc = buildToContainFuncExpectation(!(0, _utils.followTypeAssertionChain)(matcher.arguments[0]).value); | ||
const [containArg] = includesCall.arguments; | ||
fixArr.push(fixer.replaceText(matcher.node.property, toContainFunc)); | ||
fixArr.push(fixer.replaceText(matcher.arguments[0], sourceCode.getText(containArg))); | ||
return fixArr; | ||
const addNotModifier = (0, _utils.followTypeAssertionChain)(matcher.arguments[0]).value === !!modifier; | ||
return [// remove the "includes" call entirely | ||
fixer.removeRange([includesCall.callee.property.range[0] - 1, includesCall.range[1]]), // replace the current matcher with "toContain", adding "not" if needed | ||
fixer.replaceTextRange([expectCallEnd, matcher.node.range[1]], addNotModifier ? `.${_utils.ModifierName.not}.toContain` : '.toContain'), // replace the matcher argument with the value from the "includes" | ||
fixer.replaceText(matcher.arguments[0], sourceCode.getText(includesCall.arguments[0]))]; | ||
}, | ||
@@ -138,0 +90,0 @@ |
@@ -43,3 +43,3 @@ "use strict"; | ||
if (!matcher || !(0, _utils.isParsedEqualityMatcherCall)(matcher) || (argument === null || argument === void 0 ? void 0 : argument.type) !== _experimentalUtils.AST_NODE_TYPES.MemberExpression || !(0, _utils.isSupportedAccessor)(argument.property, 'length') || argument.property.type !== _experimentalUtils.AST_NODE_TYPES.Identifier) { | ||
if (!matcher || !(0, _utils.isParsedEqualityMatcherCall)(matcher) || (argument === null || argument === void 0 ? void 0 : argument.type) !== _experimentalUtils.AST_NODE_TYPES.MemberExpression || !(0, _utils.isSupportedAccessor)(argument.property, 'length')) { | ||
return; | ||
@@ -50,10 +50,5 @@ } | ||
fix(fixer) { | ||
const propertyDot = context.getSourceCode().getFirstTokenBetween(argument.object, argument.property, token => token.value === '.'); | ||
/* istanbul ignore if */ | ||
if (propertyDot === null) { | ||
throw new Error(`Unexpected null when attempting to fix ${context.getFilename()} - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`); | ||
} | ||
return [fixer.remove(propertyDot), fixer.remove(argument.property), fixer.replaceText(matcher.node.property, 'toHaveLength')]; | ||
return [// remove the "length" property accessor | ||
fixer.removeRange([argument.property.range[0] - 1, argument.range[1]]), // replace the current matcher with "toHaveLength" | ||
fixer.replaceTextRange([matcher.node.object.range[1], matcher.node.range[1]], '.toHaveLength')]; | ||
}, | ||
@@ -60,0 +55,0 @@ |
{ | ||
"name": "eslint-plugin-jest", | ||
"version": "25.3.2", | ||
"version": "25.3.3", | ||
"description": "Eslint rules for Jest", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
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
296643
4331