eslint-plugin-jest 25.3.2 → 25.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
## [25.3.3](https://github.com/jest-community/eslint-plugin-jest/compare/v25.3.2...v25.3.3) (2021-12-30)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* **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))
|
|
7
|
+
* **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))
|
|
8
|
+
|
|
1
9
|
## [25.3.2](https://github.com/jest-community/eslint-plugin-jest/compare/v25.3.1...v25.3.2) (2021-12-27)
|
|
2
10
|
|
|
3
11
|
|
|
@@ -32,52 +32,8 @@ const isBooleanEqualityMatcher = matcher => (0, _utils.isParsedEqualityMatcherCa
|
|
|
32
32
|
* @param {CallExpression} node
|
|
33
33
|
*
|
|
34
34
|
* @return {node is FixableIncludesCallExpression}
|
|
35
|
-
*
|
|
36
|
-
* @todo support `['includes']()` syntax (remove last property.type check to begin)
|
|
37
|
-
* @todo break out into `isMethodCall<Name extends string>(node: TSESTree.Node, method: Name)` util-fn
|
|
38
|
-
*/
|
|
39
|
-
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);
|
|
40
|
-
|
|
41
|
-
const buildToContainFuncExpectation = negated => negated ? `${_utils.ModifierName.not}.toContain` : 'toContain';
|
|
42
|
-
/**
|
|
43
|
-
* Finds the first `.` character token between the `object` & `property` of the given `member` expression.
|
|
44
|
-
*
|
|
45
|
-
* @param {TSESTree.MemberExpression} member
|
|
46
|
-
* @param {SourceCode} sourceCode
|
|
47
|
-
*
|
|
48
|
-
* @return {Token | null}
|
|
49
35
|
*/
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
const findPropertyDotToken = (member, sourceCode) => sourceCode.getFirstTokenBetween(member.object, member.property, token => token.value === '.');
|
|
53
|
-
|
|
54
|
-
const getNegationFixes = (node, modifier, matcher, sourceCode, fixer, fileName) => {
|
|
55
|
-
const [containArg] = node.arguments;
|
|
56
|
-
const negationPropertyDot = findPropertyDotToken(modifier.node, sourceCode);
|
|
57
|
-
const toContainFunc = buildToContainFuncExpectation((0, _utils.followTypeAssertionChain)(matcher.arguments[0]).value);
|
|
58
|
-
/* istanbul ignore if */
|
|
59
|
-
|
|
60
|
-
if (negationPropertyDot === null) {
|
|
61
|
-
throw new Error(`Unexpected null when attempting to fix ${fileName} - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return [fixer.remove(negationPropertyDot), fixer.remove(modifier.node.property), fixer.replaceText(matcher.node.property, toContainFunc), fixer.replaceText(matcher.arguments[0], sourceCode.getText(containArg))];
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
const getCommonFixes = (node, sourceCode, fileName) => {
|
|
68
|
-
const [containArg] = node.arguments;
|
|
69
|
-
const includesCallee = node.callee;
|
|
70
|
-
const propertyDot = findPropertyDotToken(includesCallee, sourceCode);
|
|
71
|
-
const closingParenthesis = sourceCode.getTokenAfter(containArg);
|
|
72
|
-
const openParenthesis = sourceCode.getTokenBefore(containArg);
|
|
73
|
-
/* istanbul ignore if */
|
|
74
|
-
|
|
75
|
-
if (propertyDot === null || closingParenthesis === null || openParenthesis === null) {
|
|
76
|
-
throw new Error(`Unexpected null when attempting to fix ${fileName} - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
return [containArg, includesCallee.property, propertyDot, closingParenthesis, openParenthesis];
|
|
80
|
-
}; // expect(array.includes(<value>)[not.]{toBe,toEqual}(<boolean>)
|
|
36
|
+
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>)
|
|
81
37
|
|
|
82
38
|
|
|
83
39
|
var _default = (0, _utils.createRule)({
|
|
@@ -106,7 +62,8 @@ var _default = (0, _utils.createRule)({
|
|
|
106
62
|
|
|
107
63
|
const {
|
|
108
64
|
expect: {
|
|
109
|
-
arguments: [includesCall]
|
|
65
|
+
arguments: [includesCall],
|
|
66
|
+
range: [, expectCallEnd]
|
|
110
67
|
},
|
|
111
68
|
matcher,
|
|
112
69
|
modifier
|
|
@@ -118,19 +75,14 @@ var _default = (0, _utils.createRule)({
|
|
|
118
75
|
|
|
119
76
|
context.report({
|
|
120
77
|
fix(fixer) {
|
|
121
|
-
const sourceCode = context.getSourceCode();
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
const toContainFunc = buildToContainFuncExpectation(!(0, _utils.followTypeAssertionChain)(matcher.arguments[0]).value);
|
|
130
|
-
const [containArg] = includesCall.arguments;
|
|
131
|
-
fixArr.push(fixer.replaceText(matcher.node.property, toContainFunc));
|
|
132
|
-
fixArr.push(fixer.replaceText(matcher.arguments[0], sourceCode.getText(containArg)));
|
|
133
|
-
return fixArr;
|
|
78
|
+
const sourceCode = context.getSourceCode(); // we need to negate the expectation if the current expected
|
|
79
|
+
// value is itself negated by the "not" modifier
|
|
80
|
+
|
|
81
|
+
const addNotModifier = (0, _utils.followTypeAssertionChain)(matcher.arguments[0]).value === !!modifier;
|
|
82
|
+
return [// remove the "includes" call entirely
|
|
83
|
+
fixer.removeRange([includesCall.callee.property.range[0] - 1, includesCall.range[1]]), // replace the current matcher with "toContain", adding "not" if needed
|
|
84
|
+
fixer.replaceTextRange([expectCallEnd, matcher.node.range[1]], addNotModifier ? `.${_utils.ModifierName.not}.toContain` : '.toContain'), // replace the matcher argument with the value from the "includes"
|
|
85
|
+
fixer.replaceText(matcher.arguments[0], sourceCode.getText(includesCall.arguments[0]))];
|
|
134
86
|
},
|
|
135
87
|
|
|
136
88
|
messageId: 'useToContain',
|
|
@@ -40,20 +40,15 @@ var _default = (0, _utils.createRule)({
|
|
|
40
40
|
matcher
|
|
41
41
|
} = (0, _utils.parseExpectCall)(node);
|
|
42
42
|
|
|
43
|
-
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')
|
|
43
|
+
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')) {
|
|
44
44
|
return;
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
context.report({
|
|
48
48
|
fix(fixer) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
if (propertyDot === null) {
|
|
53
|
-
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`);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
return [fixer.remove(propertyDot), fixer.remove(argument.property), fixer.replaceText(matcher.node.property, 'toHaveLength')];
|
|
49
|
+
return [// remove the "length" property accessor
|
|
50
|
+
fixer.removeRange([argument.property.range[0] - 1, argument.range[1]]), // replace the current matcher with "toHaveLength"
|
|
51
|
+
fixer.replaceTextRange([matcher.node.object.range[1], matcher.node.range[1]], '.toHaveLength')];
|
|
57
52
|
},
|
|
58
53
|
|
|
59
54
|
messageId: 'useToHaveLength',
|