-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refacto(no-wait-for-side-effects): migrate to v4 (#300)
* test: improve errors location asserts * refactor: use new rule creator * refactor: improve error reported location * refactor: use new helpers for detection * test: add more cases * feat: detect properly if fireEvent and userEvent should be reported * test: add cases for increasing coverage up to 100% * refactor: rename rule for consistency * docs: remove duplicated no-wait-for-snapshot row * fix: get identifier node simpler
- Loading branch information
Showing
9 changed files
with
288 additions
and
154 deletions.
There are no files selected for viewing
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
2 changes: 1 addition & 1 deletion
2
docs/rules/no-side-effects-wait-for.md → docs/rules/no-wait-for-side-effects.md
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 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 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 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,67 @@ | ||
import { TSESTree } from '@typescript-eslint/experimental-utils'; | ||
import { getPropertyIdentifierNode } from '../node-utils'; | ||
import { createTestingLibraryRule } from '../create-testing-library-rule'; | ||
|
||
export const RULE_NAME = 'no-wait-for-side-effects'; | ||
export type MessageIds = 'noSideEffectsWaitFor'; | ||
type Options = []; | ||
|
||
export default createTestingLibraryRule<Options, MessageIds>({ | ||
name: RULE_NAME, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: "It's preferred to avoid side effects in `waitFor`", | ||
category: 'Best Practices', | ||
recommended: false, | ||
}, | ||
messages: { | ||
noSideEffectsWaitFor: | ||
'Avoid using side effects within `waitFor` callback', | ||
}, | ||
fixable: null, | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create: function (context, _, helpers) { | ||
function hasSideEffects(body: Array<TSESTree.Node>): boolean { | ||
return body.some((node: TSESTree.ExpressionStatement) => { | ||
const expressionIdentifier = getPropertyIdentifierNode(node); | ||
|
||
if (!expressionIdentifier) { | ||
return false; | ||
} | ||
|
||
return ( | ||
helpers.isFireEventUtil(expressionIdentifier) || | ||
helpers.isUserEventUtil(expressionIdentifier) | ||
); | ||
}); | ||
} | ||
|
||
function reportSideEffects(node: TSESTree.BlockStatement) { | ||
const callExpressionNode = node.parent.parent as TSESTree.CallExpression; | ||
const callExpressionIdentifier = getPropertyIdentifierNode( | ||
callExpressionNode | ||
); | ||
|
||
if (!helpers.isAsyncUtil(callExpressionIdentifier, ['waitFor'])) { | ||
return; | ||
} | ||
|
||
if (!hasSideEffects(node.body)) { | ||
return; | ||
} | ||
|
||
context.report({ | ||
node: callExpressionNode, | ||
messageId: 'noSideEffectsWaitFor', | ||
}); | ||
} | ||
|
||
return { | ||
'CallExpression > ArrowFunctionExpression > BlockStatement': reportSideEffects, | ||
'CallExpression > FunctionExpression > BlockStatement': reportSideEffects, | ||
}; | ||
}, | ||
}); |
Oops, something went wrong.