/
msc.ts
36 lines (29 loc) · 1.3 KB
/
msc.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils'
import { getFirstMatcherArg, ParsedExpectVitestFnCall } from './parseVitestFnCall'
import { EqualityMatcher } from './types'
import { getAccessorValue, isSupportedAccessor } from '.'
export const isBooleanLiteral = (node: TSESTree.Node): node is TSESTree.BooleanLiteral =>
node.type === AST_NODE_TYPES.Literal && typeof node.value === 'boolean'
/**
* Checks if the given `ParsedExpectMatcher` is either a call to one of the equality matchers,
* with a boolean` literal as the sole argument, *or* is a call to `toBeTruthy` or `toBeFalsy`.
*/
export const isBooleanEqualityMatcher = (
expectFnCall: ParsedExpectVitestFnCall
): boolean => {
const matcherName = getAccessorValue(expectFnCall.matcher)
if (['toBeTruthy', 'toBeFalsy'].includes(matcherName))
return true
if (expectFnCall.args.length !== 1)
return false
const arg = getFirstMatcherArg(expectFnCall)
// eslint-disable-next-line no-prototype-builtins
return EqualityMatcher.hasOwnProperty(matcherName) && isBooleanLiteral(arg)
}
export const isInstanceOfBinaryExpression = (
node: TSESTree.Node,
className: string
): node is TSESTree.BinaryExpression =>
node.type === AST_NODE_TYPES.BinaryExpression &&
node.operator === 'instanceof' &&
isSupportedAccessor(node.right, className)