Skip to content

Commit b5e69df

Browse files
author
thomlom
committed
refactor(prefer-explicit-assert): use new utils and remove custom query option
1 parent bd60704 commit b5e69df

File tree

2 files changed

+12
-43
lines changed

2 files changed

+12
-43
lines changed

lib/rules/prefer-explicit-assert.ts

+12-34
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,23 @@
1-
import {
2-
ESLintUtils,
3-
TSESTree,
4-
ASTUtils,
5-
} from '@typescript-eslint/experimental-utils';
6-
import {
7-
getDocsUrl,
8-
ALL_QUERIES_METHODS,
9-
PRESENCE_MATCHERS,
10-
ABSENCE_MATCHERS,
11-
} from '../utils';
1+
import { TSESTree, ASTUtils } from '@typescript-eslint/experimental-utils';
2+
import { PRESENCE_MATCHERS, ABSENCE_MATCHERS } from '../utils';
123
import { findClosestCallNode, isMemberExpression } from '../node-utils';
134

5+
import { createTestingLibraryRule } from '../create-testing-library-rule';
6+
147
export const RULE_NAME = 'prefer-explicit-assert';
158
export type MessageIds =
169
| 'preferExplicitAssert'
1710
| 'preferExplicitAssertAssertion';
1811
type Options = [
1912
{
2013
assertion?: string;
21-
customQueryNames?: string[];
2214
}
2315
];
2416

25-
const ALL_GET_BY_QUERIES = ALL_QUERIES_METHODS.map(
26-
(queryMethod) => `get${queryMethod}`
27-
);
28-
29-
const isValidQuery = (node: TSESTree.Identifier, customQueryNames: string[]) =>
30-
ALL_GET_BY_QUERIES.includes(node.name) ||
31-
customQueryNames.includes(node.name);
32-
3317
const isAtTopLevel = (node: TSESTree.Node) =>
3418
node.parent.parent.type === 'ExpressionStatement';
3519

36-
export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
20+
export default createTestingLibraryRule<Options, MessageIds>({
3721
name: RULE_NAME,
3822
meta: {
3923
type: 'suggestion',
@@ -59,26 +43,18 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
5943
type: 'string',
6044
enum: PRESENCE_MATCHERS,
6145
},
62-
customQueryNames: {
63-
type: 'array',
64-
},
6546
},
6647
},
6748
],
6849
},
69-
defaultOptions: [
70-
{
71-
customQueryNames: [],
72-
},
73-
],
74-
75-
create: function (context, [options]) {
76-
const { customQueryNames, assertion } = options;
50+
defaultOptions: [{}],
51+
create(context, [options], helpers) {
52+
const { assertion } = options;
7753
const getQueryCalls: TSESTree.Identifier[] = [];
7854

7955
return {
8056
'CallExpression Identifier'(node: TSESTree.Identifier) {
81-
if (isValidQuery(node, customQueryNames)) {
57+
if (helpers.isGetByQuery(node)) {
8258
getQueryCalls.push(node);
8359
}
8460
},
@@ -93,7 +69,9 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
9369
node: queryCall,
9470
messageId: 'preferExplicitAssert',
9571
});
96-
} else if (assertion) {
72+
}
73+
74+
if (assertion) {
9775
const expectCallNode = findClosestCallNode(node, 'expect');
9876
if (!expectCallNode) return;
9977

tests/lib/rules/prefer-explicit-assert.test.ts

-9
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ ruleTester.run(RULE_NAME, rule, {
4545
{
4646
code: `function bar() { return getByText('foo') }`,
4747
},
48-
{
49-
code: `getByIcon('foo')`, // custom `getBy` query not extended through options
50-
},
5148
{
5249
code: `const { getByText } = render()`,
5350
},
@@ -130,7 +127,6 @@ ruleTester.run(RULE_NAME, rule, {
130127
// for coverage
131128
{
132129
code: `getByText("foo")`,
133-
options: [{ customQueryNames: ['bar'] }],
134130
errors: [
135131
{
136132
messageId: 'preferExplicitAssert',
@@ -139,11 +135,6 @@ ruleTester.run(RULE_NAME, rule, {
139135
},
140136
{
141137
code: `getByIcon('foo')`, // custom `getBy` query extended through options
142-
options: [
143-
{
144-
customQueryNames: ['getByIcon'],
145-
},
146-
],
147138
errors: [
148139
{
149140
messageId: 'preferExplicitAssert',

0 commit comments

Comments
 (0)