Skip to content

Commit

Permalink
chore(prefer-strict-equal): use parseExpectCall (#388)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored and SimenB committed Aug 12, 2019
1 parent 4ca5889 commit b39aad5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 21 deletions.
18 changes: 9 additions & 9 deletions src/rules/prefer-strict-equal.ts
@@ -1,4 +1,4 @@
import { createRule, isExpectCallWithParent } from './tsUtils';
import { createRule, isExpectCall, parseExpectCall } from './tsUtils';

export default createRule({
name: __filename,
Expand All @@ -12,26 +12,26 @@ export default createRule({
useToStrictEqual: 'Use toStrictEqual() instead',
},
fixable: 'code',
schema: [],
type: 'suggestion',
schema: [],
},
defaultOptions: [],
create(context) {
return {
CallExpression(node) {
if (!isExpectCallWithParent(node)) {
if (!isExpectCall(node)) {
return;
}

const methodNode = node.parent.property;
const { matcher } = parseExpectCall(node);

if (methodNode && methodNode.name === 'toEqual') {
if (matcher && matcher.name === 'toEqual') {
context.report({
fix(fixer) {
return [fixer.replaceText(methodNode, 'toStrictEqual')];
},
fix: fixer => [
fixer.replaceText(matcher.node.property, 'toStrictEqual'),
],
messageId: 'useToStrictEqual',
node: methodNode,
node: matcher.node.property,
});
}
},
Expand Down
12 changes: 0 additions & 12 deletions src/rules/tsUtils.ts
Expand Up @@ -308,18 +308,6 @@ export const isExpectCall = (node: TSESTree.Node): node is ExpectCall =>
isSupportedAccessor(node.callee, 'expect') &&
node.parent !== undefined;

interface JestExpectCallWithParent extends JestExpectCallExpression {
parent: JestExpectCallMemberExpression;
}

export const isExpectCallWithParent = (
node: TSESTree.Node,
): node is JestExpectCallWithParent =>
isExpectCall(node) &&
node.parent !== undefined &&
node.parent.type === AST_NODE_TYPES.MemberExpression &&
node.parent.property.type === AST_NODE_TYPES.Identifier;

interface ParsedExpectMember<
Name extends ExpectPropertyName = ExpectPropertyName,
Node extends ExpectMember<Name> = ExpectMember<Name>
Expand Down

0 comments on commit b39aad5

Please sign in to comment.