eslint-plugin-jest-dom 5.10.2 → 5.10.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.
|
@@ -14,6 +14,24 @@ var _context = require("../context");
|
|
|
14
14
|
//------------------------------------------------------------------------------
|
|
15
15
|
|
|
16
16
|
const messageId = "use-to-have-class";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* `classList[x]` is a class name only when `x` is an index. Keys like
|
|
20
|
+
* `classList["length"]` or `classList["value"]` read other properties.
|
|
21
|
+
*/
|
|
22
|
+
const isClassListIndex = ({
|
|
23
|
+
property
|
|
24
|
+
}) => {
|
|
25
|
+
let key;
|
|
26
|
+
if (property.type === "Literal") {
|
|
27
|
+
key = property.value;
|
|
28
|
+
} else if (property.type === "TemplateLiteral" && property.expressions.length === 0) {
|
|
29
|
+
key = property.quasis[0].value.cooked;
|
|
30
|
+
} else {
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
return typeof key === "number" || typeof key === "string" && /^(0|[1-9]\d*)$/.test(key);
|
|
34
|
+
};
|
|
17
35
|
const meta = exports.meta = {
|
|
18
36
|
docs: {
|
|
19
37
|
category: "Best Practices",
|
|
@@ -46,11 +64,14 @@ const create = context => ({
|
|
|
46
64
|
});
|
|
47
65
|
},
|
|
48
66
|
//expect(el.classList[0]).toBe("bar")
|
|
49
|
-
[`CallExpression[callee.object.callee.name=expect][callee.object.arguments.0.object.property.name=classList][callee.property.name=/toBe$|to(Strict)?Equal|toContain/][arguments.0.type=/Literal$/]`](node) {
|
|
67
|
+
[`CallExpression[callee.object.callee.name=expect][callee.object.arguments.0.object.property.name=classList][callee.object.arguments.0.computed=true][callee.property.name=/toBe$|to(Strict)?Equal|toContain/][arguments.0.type=/Literal$/]`](node) {
|
|
50
68
|
const [classValue] = node.arguments;
|
|
51
69
|
const matcher = node.callee.property;
|
|
52
70
|
const classNameProp = node.callee.object.arguments[0].object;
|
|
53
71
|
const expectArg = node.callee.object.arguments[0];
|
|
72
|
+
if (!isClassListIndex(expectArg)) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
54
75
|
context.report({
|
|
55
76
|
node: matcher,
|
|
56
77
|
messageId,
|
|
@@ -64,7 +85,10 @@ const create = context => ({
|
|
|
64
85
|
});
|
|
65
86
|
},
|
|
66
87
|
//expect(el.classList[0]).not.toBe("bar")
|
|
67
|
-
[`CallExpression[callee.object.object.callee.name=expect][callee.object.object.arguments.0.object.property.name=classList][callee.object.property.name=not][callee.property.name=/toBe$|to(Strict)?Equal|toContain/][arguments.0.type=/Literal$/]`](node) {
|
|
88
|
+
[`CallExpression[callee.object.object.callee.name=expect][callee.object.object.arguments.0.object.property.name=classList][callee.object.object.arguments.0.computed=true][callee.object.property.name=not][callee.property.name=/toBe$|to(Strict)?Equal|toContain/][arguments.0.type=/Literal$/]`](node) {
|
|
89
|
+
if (!isClassListIndex(node.callee.object.object.arguments[0])) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
68
92
|
//can't autofix this case because the class could be in another element of the classList array.
|
|
69
93
|
context.report({
|
|
70
94
|
node,
|