Skip to content

Commit

Permalink
feat: updated
Browse files Browse the repository at this point in the history
  • Loading branch information
veritem committed Apr 2, 2023
1 parent 7c99644 commit b1ababe
Showing 1 changed file with 48 additions and 3 deletions.
51 changes: 48 additions & 3 deletions src/rules/valid-expect-in-promise.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { AST_NODE_TYPES, TSESLint, TSESTree } from '@typescript-eslint/utils'
import { createEslintRule, getAccessorValue, isFunction } from '../utils'
import { parseVitestFnCall } from '../utils/parseVitestFnCall'
import { KnownCallExpression, createEslintRule, getAccessorValue, isFunction, isSupportedAccessor } from '../utils'
import { isTypeOfVitestFnCall, parseVitestFnCall } from '../utils/parseVitestFnCall'

export const RULE_NAME = 'valid-expect-in-promise'
type MESSAGE_IDS = 'expectInFloatingPromise'
type Options = []

type PromiseChainCallExpression = KnownCallExpression<
'then' | 'catch' | 'finally'
>;

const isTestCaseCallWithCallbackArg = (node: TSESTree.CallExpression, context: TSESLint.RuleContext<string, Options>): boolean => {
const vitesCallFn = parseVitestFnCall(node, context)

Expand All @@ -24,6 +28,25 @@ const isTestCaseCallWithCallbackArg = (node: TSESTree.CallExpression, context: T
return callback && isFunction(callback) && callback.params.length === 1 + callbackArgIndex
}

const isPromiseChainCall = (node: TSESTree.Node): node is PromiseChainCallExpression => {
if (node.type === AST_NODE_TYPES.CallExpression &&
node.callee.type === AST_NODE_TYPES.MemberExpression &&
isSupportedAccessor(node.callee.property)) {
if (node.arguments.length === 0)
return false

switch (getAccessorValue(node.callee.property)) {
case 'then':
return node.arguments.length < 3
case 'catch':
case 'finally':
return node.arguments.length < 2
}
}

return false
}

export default createEslintRule<Options, MESSAGE_IDS>({
name: RULE_NAME,
meta: {
Expand All @@ -47,8 +70,30 @@ export default createEslintRule<Options, MESSAGE_IDS>({
CallExpression(node: TSESTree.CallExpression) {
if (isTestCaseCallWithCallbackArg(node, context))
inTestCaseWithDoneCallBack = true

if (isPromiseChainCall(node)) {
chains.unshift(false)

return
}

if (chains.length > 0 && isTypeOfVitestFnCall(node, context, ['expect']))
chains[0] = true
},
'CallExpression:exit'(node: TSESTree.CallExpression) {
if (inTestCaseWithDoneCallBack) {
if (isTypeOfVitestFnCall(node, context, ['test']))
inTestCaseWithDoneCallBack = false

return
}

if (isPromiseChainCall(node)) return

const hasExpectCall = chains.shift()

// TODO: start from here
}
// 'CallExpression:exit'(node: TSESTree.CallExpression) { }
}
}
})

0 comments on commit b1ababe

Please sign in to comment.