Skip to content

Commit 85a3a20

Browse files
G-Rathbenmonro
andauthored
fix(createBannedAttributeRule): check arguments length before accessing (#167)
Co-authored-by: Ben Monro <[email protected]>
1 parent 40353d3 commit 85a3a20

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/__tests__/__fixtures__/createBannedAttributeTestCases.js

+10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33
export default ({ preferred, negatedPreferred, attribute }) => {
44
const doubleNegativeCases = negatedPreferred.startsWith("toBe")
55
? [
6+
{
7+
code: `expect().not.${negatedPreferred}`,
8+
errors: [
9+
{
10+
message: `Use ${preferred} instead of not.${negatedPreferred}`,
11+
},
12+
],
13+
output: `expect().${preferred}`,
14+
},
615
{
716
code: `const el = screen.getByText("foo"); expect(el).not.${negatedPreferred}`,
817
errors: [
@@ -66,6 +75,7 @@ export default ({ preferred, negatedPreferred, attribute }) => {
6675

6776
return {
6877
valid: [
78+
`expect().not.toHaveProperty('value', 'foo')`,
6979
`const el = screen.getByText("foo"); expect(el).not.toHaveProperty('value', 'foo')`,
7080
`const el = screen.getByText("foo"); expect(el).${preferred}`,
7181
`const el = screen.getByText("foo"); expect(el).${negatedPreferred}`,

src/createBannedAttributeRule.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default ({ preferred, negatedPreferred, attributes }) => (context) => {
1313
: negatedPreferred;
1414

1515
const isBannedArg = (node) =>
16+
node.arguments.length &&
1617
attributes.some((attr) => attr === node.arguments[0].value);
1718

1819
//expect(el).not.toBeEnabled() => expect(el).toBeDisabled()
@@ -42,6 +43,10 @@ export default ({ preferred, negatedPreferred, attributes }) => (context) => {
4243
"CallExpression[callee.property.name=/toBe(Truthy|Falsy)?|toEqual/][callee.object.callee.name='expect']"(
4344
node
4445
) {
46+
if (!node.callee.object.arguments.length) {
47+
return;
48+
}
49+
4550
const {
4651
arguments: [{ object, property, property: { name } = {} }],
4752
} = node.callee.object;
@@ -77,11 +82,11 @@ export default ({ preferred, negatedPreferred, attributes }) => (context) => {
7782
"CallExpression[callee.property.name=/toHaveProperty|toHaveAttribute/][callee.object.property.name='not'][callee.object.object.callee.name='expect']"(
7883
node
7984
) {
80-
const arg = node.arguments[0].value;
8185
if (!isBannedArg(node)) {
8286
return;
8387
}
8488

89+
const arg = node.arguments[0].value;
8590
const correctFunction = getCorrectFunctionFor(node, true);
8691

8792
const incorrectFunction = node.callee.property.name;

0 commit comments

Comments
 (0)