Skip to content

Commit

Permalink
fix(prefer-to-have-length): support square bracket accessors (#1010)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
G-Rath committed Dec 30, 2021
1 parent 73984a7 commit 9e70f55
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 27 deletions.
37 changes: 30 additions & 7 deletions src/rules/__tests__/prefer-to-have-length.test.ts
Expand Up @@ -18,16 +18,34 @@ ruleTester.run('prefer-to-have-length', rule, {
`expect(user.getUserName(5)).resolves.toEqual('Paul')`,
`expect(user.getUserName(5)).rejects.toEqual('Paul')`,
'expect(a);',
'expect(files["length"]).toBe(1);',
],

invalid: [
// todo: support this
// {
// code: 'expect(files["length"]).toBe(1);',
// errors: [{ messageId: 'useToHaveLength', column: 22, line: 1 }],
// output: 'expect(files).toHaveLength(1);',
// },
{
code: 'expect(files["length"]).toBe(1);',
output: 'expect(files).toHaveLength(1);',
errors: [{ messageId: 'useToHaveLength', column: 25, line: 1 }],
},
{
code: 'expect(files["length"])["not"].toBe(1);',
output: 'expect(files)["not"].toHaveLength(1);',
errors: [{ messageId: 'useToHaveLength', column: 32, line: 1 }],
},
{
code: 'expect(files["length"])["toBe"](1);',
output: 'expect(files).toHaveLength(1);',
errors: [{ messageId: 'useToHaveLength', column: 25, line: 1 }],
},
{
code: 'expect(files["length"]).not["toBe"](1);',
output: 'expect(files).not.toHaveLength(1);',
errors: [{ messageId: 'useToHaveLength', column: 29, line: 1 }],
},
{
code: 'expect(files["length"])["not"]["toBe"](1);',
output: 'expect(files)["not"].toHaveLength(1);',
errors: [{ messageId: 'useToHaveLength', column: 32, line: 1 }],
},
{
code: 'expect(files.length).toBe(1);',
output: 'expect(files).toHaveLength(1);',
Expand All @@ -43,5 +61,10 @@ ruleTester.run('prefer-to-have-length', rule, {
output: 'expect(files).toHaveLength(1);',
errors: [{ messageId: 'useToHaveLength', column: 22, line: 1 }],
},
{
code: 'expect(files.length).not.toStrictEqual(1);',
output: 'expect(files).not.toHaveLength(1);',
errors: [{ messageId: 'useToHaveLength', column: 26, line: 1 }],
},
],
});
31 changes: 11 additions & 20 deletions src/rules/prefer-to-have-length.ts
Expand Up @@ -41,33 +41,24 @@ export default createRule({
!matcher ||
!isParsedEqualityMatcherCall(matcher) ||
argument?.type !== AST_NODE_TYPES.MemberExpression ||
!isSupportedAccessor(argument.property, 'length') ||
argument.property.type !== AST_NODE_TYPES.Identifier
!isSupportedAccessor(argument.property, 'length')
) {
return;
}

context.report({
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'),
// 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',
),
];
},
messageId: 'useToHaveLength',
Expand Down

0 comments on commit 9e70f55

Please sign in to comment.