Skip to content

Commit

Permalink
chore(prefer-to-have-length): convert to typescript (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored and SimenB committed Aug 15, 2019
1 parent ff86470 commit c446449
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 59 deletions.
@@ -1,19 +1,31 @@
import { RuleTester } from 'eslint';
import { TSESLint } from '@typescript-eslint/experimental-utils';
import rule from '../prefer-to-have-length';

const ruleTester = new RuleTester();
const ruleTester = new TSESLint.RuleTester({
parserOptions: {
ecmaVersion: 2015,
},
});

ruleTester.run('prefer-to-have-length', rule, {
valid: [
'expect(files).toHaveLength(1);',
"expect(files.name).toBe('file');",
"expect(files[`name`]).toBe('file');",
'expect(result).toBe(true);',
`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);',
errors: [{ messageId: 'useToHaveLength', column: 22, line: 1 }],
Expand Down
57 changes: 0 additions & 57 deletions src/rules/prefer-to-have-length.js

This file was deleted.

80 changes: 80 additions & 0 deletions src/rules/prefer-to-have-length.ts
@@ -0,0 +1,80 @@
import { AST_NODE_TYPES } from '@typescript-eslint/experimental-utils';
import {
createRule,
isExpectCall,
isParsedEqualityMatcherCall,
isSupportedAccessor,
parseExpectCall,
} from './tsUtils';

export default createRule({
name: __filename,
meta: {
docs: {
category: 'Best Practices',
description: 'Suggest using `toHaveLength()`',
recommended: false,
},
messages: {
useToHaveLength: 'Use toHaveLength() instead',
},
fixable: 'code',
type: 'suggestion',
schema: [],
},
defaultOptions: [],
create(context) {
return {
CallExpression(node) {
if (!isExpectCall(node)) {
return;
}

const {
expect: {
arguments: [argument],
},
matcher,
} = parseExpectCall(node);

if (
!matcher ||
!isParsedEqualityMatcherCall(matcher) ||
!argument ||
argument.type !== AST_NODE_TYPES.MemberExpression ||
!isSupportedAccessor(argument.property, 'length') ||
argument.property.type !== AST_NODE_TYPES.Identifier
) {
return;
}

context.report({
fix(fixer) {
const propertyDot = context
.getSourceCode()
.getFirstTokenBetween(
argument.object,
argument.property,
token => token.value === '.',
);

/* istanbul ignore next */
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'),
];
},
messageId: 'useToHaveLength',
node: matcher.node.property,
});
},
};
},
});

0 comments on commit c446449

Please sign in to comment.