-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(prefer-to-have-length): convert to typescript (#396)
- Loading branch information
Showing
3 changed files
with
94 additions
and
59 deletions.
There are no files selected for viewing
16 changes: 14 additions & 2 deletions
16
...s/__tests__/prefer-to-have-length.test.js → ...s/__tests__/prefer-to-have-length.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); |