Skip to content

Commit

Permalink
refactor(prefer-explicit-assert): use new utils and remove custom que…
Browse files Browse the repository at this point in the history
…ry option
  • Loading branch information
thomlom committed Dec 22, 2020
1 parent bd60704 commit b5e69df
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 43 deletions.
46 changes: 12 additions & 34 deletions lib/rules/prefer-explicit-assert.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,23 @@
import {
ESLintUtils,
TSESTree,
ASTUtils,
} from '@typescript-eslint/experimental-utils';
import {
getDocsUrl,
ALL_QUERIES_METHODS,
PRESENCE_MATCHERS,
ABSENCE_MATCHERS,
} from '../utils';
import { TSESTree, ASTUtils } from '@typescript-eslint/experimental-utils';
import { PRESENCE_MATCHERS, ABSENCE_MATCHERS } from '../utils';
import { findClosestCallNode, isMemberExpression } from '../node-utils';

import { createTestingLibraryRule } from '../create-testing-library-rule';

export const RULE_NAME = 'prefer-explicit-assert';
export type MessageIds =
| 'preferExplicitAssert'
| 'preferExplicitAssertAssertion';
type Options = [
{
assertion?: string;
customQueryNames?: string[];
}
];

const ALL_GET_BY_QUERIES = ALL_QUERIES_METHODS.map(
(queryMethod) => `get${queryMethod}`
);

const isValidQuery = (node: TSESTree.Identifier, customQueryNames: string[]) =>
ALL_GET_BY_QUERIES.includes(node.name) ||
customQueryNames.includes(node.name);

const isAtTopLevel = (node: TSESTree.Node) =>
node.parent.parent.type === 'ExpressionStatement';

export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
export default createTestingLibraryRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'suggestion',
Expand All @@ -59,26 +43,18 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
type: 'string',
enum: PRESENCE_MATCHERS,
},
customQueryNames: {
type: 'array',
},
},
},
],
},
defaultOptions: [
{
customQueryNames: [],
},
],

create: function (context, [options]) {
const { customQueryNames, assertion } = options;
defaultOptions: [{}],
create(context, [options], helpers) {
const { assertion } = options;
const getQueryCalls: TSESTree.Identifier[] = [];

return {
'CallExpression Identifier'(node: TSESTree.Identifier) {
if (isValidQuery(node, customQueryNames)) {
if (helpers.isGetByQuery(node)) {
getQueryCalls.push(node);
}
},
Expand All @@ -93,7 +69,9 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
node: queryCall,
messageId: 'preferExplicitAssert',
});
} else if (assertion) {
}

if (assertion) {
const expectCallNode = findClosestCallNode(node, 'expect');
if (!expectCallNode) return;

Expand Down
9 changes: 0 additions & 9 deletions tests/lib/rules/prefer-explicit-assert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ ruleTester.run(RULE_NAME, rule, {
{
code: `function bar() { return getByText('foo') }`,
},
{
code: `getByIcon('foo')`, // custom `getBy` query not extended through options
},
{
code: `const { getByText } = render()`,
},
Expand Down Expand Up @@ -130,7 +127,6 @@ ruleTester.run(RULE_NAME, rule, {
// for coverage
{
code: `getByText("foo")`,
options: [{ customQueryNames: ['bar'] }],
errors: [
{
messageId: 'preferExplicitAssert',
Expand All @@ -139,11 +135,6 @@ ruleTester.run(RULE_NAME, rule, {
},
{
code: `getByIcon('foo')`, // custom `getBy` query extended through options
options: [
{
customQueryNames: ['getByIcon'],
},
],
errors: [
{
messageId: 'preferExplicitAssert',
Expand Down

0 comments on commit b5e69df

Please sign in to comment.