Skip to content

Commit 08656c7

Browse files
committed
chore: 100% coverage
1 parent ee578f6 commit 08656c7

7 files changed

+26
-95
lines changed

lib/node-utils.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ export function isImportSpecifier(
3232
return node && node.type === 'ImportSpecifier';
3333
}
3434

35-
export function isImportDeclaration(
36-
node: TSESTree.Node
37-
): node is TSESTree.ImportDeclaration {
38-
return node && node.type === 'ImportDeclaration';
39-
}
40-
4135
export function isImportDefaultSpecifier(
4236
node: TSESTree.Node
4337
): node is TSESTree.ImportDefaultSpecifier {

lib/rules/await-fire-event.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import { ESLintUtils, TSESTree } from '@typescript-eslint/experimental-utils';
22
import { getDocsUrl } from '../utils';
3-
import {
4-
isMemberExpression,
5-
isIdentifier,
6-
isCallExpression,
7-
hasThenProperty,
8-
} from '../node-utils';
3+
import { isIdentifier, isCallExpression, hasThenProperty } from '../node-utils';
94

105
export const RULE_NAME = 'await-fire-event';
116
export type MessageIds = 'awaitFireEvent';
@@ -50,10 +45,8 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
5045
'CallExpression > MemberExpression > Identifier[name=fireEvent]'(
5146
node: TSESTree.Identifier
5247
) {
53-
if (!isMemberExpression(node.parent)) {
54-
return;
55-
}
56-
const fireEventMethodNode = node.parent.property;
48+
const memberExpression = node.parent as TSESTree.MemberExpression;
49+
const fireEventMethodNode = memberExpression.property;
5750

5851
if (
5952
isIdentifier(fireEventMethodNode) &&

lib/rules/no-debug.ts

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import {
66
isIdentifier,
77
isCallExpression,
88
isLiteral,
9-
isVariableDeclarator,
10-
isImportDeclaration,
119
isAwaitExpression,
1210
isMemberExpression,
1311
} from '../node-utils';
@@ -59,9 +57,6 @@ function isRenderVariableDeclarator(
5957
function hasTestingLibraryImportModule(
6058
importDeclarationNode: TSESTree.ImportDeclaration
6159
) {
62-
if (!isLiteral(importDeclarationNode.source)) {
63-
return;
64-
}
6560
const literal = importDeclarationNode.source;
6661
return LIBRARY_MODULES_WITH_SCREEN.some(module => module === literal.value);
6762
}
@@ -128,10 +123,7 @@ export default ESLintUtils.RuleCreator(getDocsUrl)({
128123
[`VariableDeclarator > CallExpression > Identifier[name="require"]`](
129124
node: TSESTree.Identifier
130125
) {
131-
if (!isCallExpression(node.parent)) {
132-
return;
133-
}
134-
const { arguments: args } = node.parent;
126+
const { arguments: args } = node.parent as TSESTree.CallExpression;
135127

136128
const literalNodeScreenModuleName = args.find(
137129
args =>
@@ -144,10 +136,8 @@ export default ESLintUtils.RuleCreator(getDocsUrl)({
144136
return;
145137
}
146138

147-
if (!isVariableDeclarator(node.parent.parent)) {
148-
return;
149-
}
150-
const declaratorNode = node.parent.parent;
139+
const declaratorNode = node.parent
140+
.parent as TSESTree.VariableDeclarator;
151141

152142
hasImportedScreen =
153143
isObjectPattern(declaratorNode.id) &&
@@ -161,10 +151,7 @@ export default ESLintUtils.RuleCreator(getDocsUrl)({
161151
// checks if import has shape:
162152
// import { screen } from '@testing-library/dom';
163153
'ImportDeclaration ImportSpecifier'(node: TSESTree.ImportSpecifier) {
164-
if (!isImportDeclaration(node.parent)) {
165-
return;
166-
}
167-
const importDeclarationNode = node.parent;
154+
const importDeclarationNode = node.parent as TSESTree.ImportDeclaration;
168155

169156
if (!hasTestingLibraryImportModule(importDeclarationNode)) return;
170157

@@ -175,10 +162,7 @@ export default ESLintUtils.RuleCreator(getDocsUrl)({
175162
'ImportDeclaration ImportNamespaceSpecifier'(
176163
node: TSESTree.ImportNamespaceSpecifier
177164
) {
178-
if (!isImportDeclaration(node.parent)) {
179-
return;
180-
}
181-
const importDeclarationNode = node.parent;
165+
const importDeclarationNode = node.parent as TSESTree.ImportDeclaration;
182166
if (!hasTestingLibraryImportModule(importDeclarationNode)) return;
183167

184168
wildcardImportName = node.local && node.local.name;
@@ -194,14 +178,9 @@ export default ESLintUtils.RuleCreator(getDocsUrl)({
194178
[`CallExpression > MemberExpression > Identifier[name="debug"]`](
195179
node: TSESTree.Identifier
196180
) {
197-
if (
198-
!isMemberExpression(node.parent) ||
199-
!isIdentifier(node.parent.object)
200-
) {
201-
return;
202-
}
203-
204-
const memberExpressionName = node.parent.object.name;
181+
const memberExpression = node.parent as TSESTree.MemberExpression;
182+
const identifier = memberExpression.object as TSESTree.Identifier;
183+
const memberExpressionName = identifier.name;
205184
/*
206185
check if `debug` used following the pattern:
207186

lib/rules/no-dom-import.ts

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import { ESLintUtils, TSESTree } from '@typescript-eslint/experimental-utils';
22
import { getDocsUrl } from '../utils';
3-
import {
4-
isLiteral,
5-
isCallExpression,
6-
isIdentifier,
7-
isImportDeclaration,
8-
} from '../node-utils';
3+
import { isLiteral, isIdentifier } from '../node-utils';
94

105
export const RULE_NAME = 'no-dom-import';
116
export type MessageIds = 'noDomImport' | 'noDomImportFramework';
@@ -56,22 +51,17 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
5651
},
5752
fix(fixer) {
5853
if (isRequire) {
59-
if (!isCallExpression(node.parent)) {
60-
return;
61-
}
62-
63-
const [name] = node.parent.arguments;
64-
if (!isLiteral(name)) {
65-
return;
66-
}
54+
const callExpression = node.parent as TSESTree.CallExpression;
55+
const name = callExpression.arguments[0] as TSESTree.Literal;
6756

6857
// Replace the module name with the raw module name as we can't predict which punctuation the user is going to use
6958
return fixer.replaceText(
7059
name,
7160
name.raw.replace(moduleName, correctModuleName)
7261
);
73-
} else if (isImportDeclaration(node) && isLiteral(node.source)) {
74-
const name = node.source;
62+
} else {
63+
const importDeclaration = node as TSESTree.ImportDeclaration;
64+
const name = importDeclaration.source;
7565
return fixer.replaceText(
7666
name,
7767
name.raw.replace(moduleName, correctModuleName)
@@ -88,9 +78,6 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
8878
}
8979
return {
9080
ImportDeclaration(node) {
91-
if (!isLiteral(node.source) || typeof node.source.value !== 'string') {
92-
return;
93-
}
9481
const value = node.source.value;
9582
const domModuleName = DOM_TESTING_LIBRARY_MODULES.find(
9683
module => module === value
@@ -104,10 +91,8 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
10491
[`CallExpression > Identifier[name="require"]`](
10592
node: TSESTree.Identifier
10693
) {
107-
if (!isCallExpression(node.parent)) {
108-
return;
109-
}
110-
const { arguments: args } = node.parent;
94+
const callExpression = node.parent as TSESTree.CallExpression;
95+
const { arguments: args } = callExpression;
11196

11297
const literalNodeDomModuleName = args.find(
11398
args =>

lib/rules/no-manual-cleanup.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { ESLintUtils, TSESTree } from '@typescript-eslint/experimental-utils';
22
import { getDocsUrl } from '../utils';
33
import {
4-
isLiteral,
54
isImportDefaultSpecifier,
65
isCallExpression,
76
isIdentifier,
@@ -63,13 +62,8 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
6362

6463
return {
6564
ImportDeclaration(node) {
66-
if (!isLiteral(node.source) || typeof node.source.value !== 'string') {
67-
return;
68-
}
69-
70-
const testingLibraryWithCleanup = node.source.value.match(
71-
CLEANUP_LIBRARY_REGEX
72-
);
65+
const value = node.source.value as string;
66+
const testingLibraryWithCleanup = value.match(CLEANUP_LIBRARY_REGEX);
7367

7468
// Early return if the library doesn't support `cleanup`
7569
if (!testingLibraryWithCleanup) {
@@ -100,14 +94,10 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
10094
isIdentifier(node.init.callee) &&
10195
node.init.callee.name === 'require'
10296
) {
103-
const [requiredModule] = node.init.arguments;
104-
if (
105-
!isLiteral(requiredModule) ||
106-
typeof requiredModule.value !== 'string'
107-
) {
108-
return;
109-
}
110-
const testingLibraryWithCleanup = requiredModule.value.match(
97+
const requiredModule = node.init.arguments[0] as TSESTree.Literal;
98+
const requiredModuleValue = requiredModule.value as string;
99+
100+
const testingLibraryWithCleanup = requiredModuleValue.match(
111101
CLEANUP_LIBRARY_REGEX
112102
);
113103

lib/rules/prefer-presence-queries.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
5252

5353
if (expectCallNode && isMemberExpression(expectCallNode.parent)) {
5454
const expectStatement = expectCallNode.parent;
55-
if (!isIdentifier(expectStatement.property)) return;
56-
const property = expectStatement.property;
55+
const property = expectStatement.property as TSESTree.Identifier;
5756
let matcher = property.name;
5857
let isNegatedMatcher = false;
5958

@@ -66,10 +65,6 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
6665
matcher = expectStatement.parent.property.name;
6766
}
6867

69-
if (!matcher) {
70-
return;
71-
}
72-
7368
const validMatchers = isThrowingQuery(node)
7469
? PRESENCE_MATCHERS
7570
: ABSENCE_MATCHERS;

lib/rules/prefer-wait-for.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { ESLintUtils, TSESTree } from '@typescript-eslint/experimental-utils';
22
import { getDocsUrl } from '../utils';
33
import {
44
isImportSpecifier,
5-
isLiteral,
65
isMemberExpression,
76
isIdentifier,
87
findClosestCallExpressionNode,
@@ -43,10 +42,6 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
4342
node: node,
4443
messageId: 'preferWaitForImport',
4544
fix(fixer) {
46-
if (!isLiteral(node.source)) {
47-
return;
48-
}
49-
5045
const excludedImports = [...DEPRECATED_METHODS, 'waitFor'];
5146

5247
// TODO: refactor `importNodes` to TSESTree.ImportSpecifier[] ? (to not have to traverse the list twice)

0 commit comments

Comments
 (0)