Skip to content

fix(prefer-to-have-length): support square bracket accessors #1010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions src/rules/__tests__/prefer-to-have-length.test.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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