diff --git a/src/rules/__tests__/prefer-to-have-length.test.ts b/src/rules/__tests__/prefer-to-have-length.test.ts index 532d504fb..5f577bd80 100644 --- a/src/rules/__tests__/prefer-to-have-length.test.ts +++ b/src/rules/__tests__/prefer-to-have-length.test.ts @@ -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);', @@ -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 }], + }, ], }); diff --git a/src/rules/prefer-to-have-length.ts b/src/rules/prefer-to-have-length.ts index 4da493aff..0b5c4df63 100644 --- a/src/rules/prefer-to-have-length.ts +++ b/src/rules/prefer-to-have-length.ts @@ -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',