Skip to content

Commit 9e70f55

Browse files
authored
fix(prefer-to-have-length): support square bracket accessors (#1010)
* fix(prefer-to-have-length): support square bracket accessors * refactor(prefer-to-have-length): simplify fixer * refactor(prefer-to-have-length): simplify fixer further * chore(prefer-to-have-length): add comments
1 parent 73984a7 commit 9e70f55

File tree

2 files changed

+41
-27
lines changed

2 files changed

+41
-27
lines changed

src/rules/__tests__/prefer-to-have-length.test.ts

+30-7
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,34 @@ ruleTester.run('prefer-to-have-length', rule, {
1818
`expect(user.getUserName(5)).resolves.toEqual('Paul')`,
1919
`expect(user.getUserName(5)).rejects.toEqual('Paul')`,
2020
'expect(a);',
21-
'expect(files["length"]).toBe(1);',
2221
],
2322

2423
invalid: [
25-
// todo: support this
26-
// {
27-
// code: 'expect(files["length"]).toBe(1);',
28-
// errors: [{ messageId: 'useToHaveLength', column: 22, line: 1 }],
29-
// output: 'expect(files).toHaveLength(1);',
30-
// },
24+
{
25+
code: 'expect(files["length"]).toBe(1);',
26+
output: 'expect(files).toHaveLength(1);',
27+
errors: [{ messageId: 'useToHaveLength', column: 25, line: 1 }],
28+
},
29+
{
30+
code: 'expect(files["length"])["not"].toBe(1);',
31+
output: 'expect(files)["not"].toHaveLength(1);',
32+
errors: [{ messageId: 'useToHaveLength', column: 32, line: 1 }],
33+
},
34+
{
35+
code: 'expect(files["length"])["toBe"](1);',
36+
output: 'expect(files).toHaveLength(1);',
37+
errors: [{ messageId: 'useToHaveLength', column: 25, line: 1 }],
38+
},
39+
{
40+
code: 'expect(files["length"]).not["toBe"](1);',
41+
output: 'expect(files).not.toHaveLength(1);',
42+
errors: [{ messageId: 'useToHaveLength', column: 29, line: 1 }],
43+
},
44+
{
45+
code: 'expect(files["length"])["not"]["toBe"](1);',
46+
output: 'expect(files)["not"].toHaveLength(1);',
47+
errors: [{ messageId: 'useToHaveLength', column: 32, line: 1 }],
48+
},
3149
{
3250
code: 'expect(files.length).toBe(1);',
3351
output: 'expect(files).toHaveLength(1);',
@@ -43,5 +61,10 @@ ruleTester.run('prefer-to-have-length', rule, {
4361
output: 'expect(files).toHaveLength(1);',
4462
errors: [{ messageId: 'useToHaveLength', column: 22, line: 1 }],
4563
},
64+
{
65+
code: 'expect(files.length).not.toStrictEqual(1);',
66+
output: 'expect(files).not.toHaveLength(1);',
67+
errors: [{ messageId: 'useToHaveLength', column: 26, line: 1 }],
68+
},
4669
],
4770
});

src/rules/prefer-to-have-length.ts

+11-20
Original file line numberDiff line numberDiff line change
@@ -41,33 +41,24 @@ export default createRule({
4141
!matcher ||
4242
!isParsedEqualityMatcherCall(matcher) ||
4343
argument?.type !== AST_NODE_TYPES.MemberExpression ||
44-
!isSupportedAccessor(argument.property, 'length') ||
45-
argument.property.type !== AST_NODE_TYPES.Identifier
44+
!isSupportedAccessor(argument.property, 'length')
4645
) {
4746
return;
4847
}
4948

5049
context.report({
5150
fix(fixer) {
52-
const propertyDot = context
53-
.getSourceCode()
54-
.getFirstTokenBetween(
55-
argument.object,
56-
argument.property,
57-
token => token.value === '.',
58-
);
59-
60-
/* istanbul ignore if */
61-
if (propertyDot === null) {
62-
throw new Error(
63-
`Unexpected null when attempting to fix ${context.getFilename()} - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`,
64-
);
65-
}
66-
6751
return [
68-
fixer.remove(propertyDot),
69-
fixer.remove(argument.property),
70-
fixer.replaceText(matcher.node.property, 'toHaveLength'),
52+
// remove the "length" property accessor
53+
fixer.removeRange([
54+
argument.property.range[0] - 1,
55+
argument.range[1],
56+
]),
57+
// replace the current matcher with "toHaveLength"
58+
fixer.replaceTextRange(
59+
[matcher.node.object.range[1], matcher.node.range[1]],
60+
'.toHaveLength',
61+
),
7162
];
7263
},
7364
messageId: 'useToHaveLength',

0 commit comments

Comments
 (0)