Skip to content

Commit a8f5f33

Browse files
authored
refactor: remove unneeded optional chains and other conditions (#1894)
1 parent b8b3110 commit a8f5f33

17 files changed

Lines changed: 38 additions & 54 deletions

src/rules/max-expects.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export default createRule({
3636

3737
const maybeResetCount = (node: FunctionExpression) => {
3838
const isTestFn =
39-
node.parent?.type !== AST_NODE_TYPES.CallExpression ||
39+
node.parent.type !== AST_NODE_TYPES.CallExpression ||
4040
isTypeOfJestFnCall(node.parent, context, ['test']);
4141

4242
if (isTestFn) {
@@ -58,7 +58,7 @@ export default createRule({
5858

5959
if (
6060
jestFnCall?.type !== 'expect' ||
61-
jestFnCall.head.node.parent?.type === AST_NODE_TYPES.MemberExpression
61+
jestFnCall.head.node.parent.type === AST_NODE_TYPES.MemberExpression
6262
) {
6363
return;
6464
}

src/rules/no-standalone-expect.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,12 @@ const getBlockType = (
1919
): 'function' | 'describe' | null => {
2020
const func = statement.parent;
2121

22-
/* istanbul ignore if */
23-
if (!func) {
24-
throw new Error(
25-
`Unexpected BlockStatement. No parent defined. - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`,
26-
);
27-
}
28-
2922
// functionDeclaration: function func() {}
3023
if (func.type === AST_NODE_TYPES.FunctionDeclaration) {
3124
return 'function';
3225
}
3326

34-
if (isFunction(func) && func.parent) {
27+
if (isFunction(func)) {
3528
const expr = func.parent;
3629

3730
// arrow function or function expr
@@ -94,7 +87,7 @@ export default createRule<
9487

9588
if (jestFnCall?.type === 'expect') {
9689
if (
97-
jestFnCall.head.node.parent?.type ===
90+
jestFnCall.head.node.parent.type ===
9891
AST_NODE_TYPES.MemberExpression &&
9992
jestFnCall.members.length === 1 &&
10093
!['assertions', 'hasAssertions'].includes(
@@ -152,7 +145,7 @@ export default createRule<
152145
},
153146

154147
ArrowFunctionExpression(node) {
155-
if (node.parent?.type !== AST_NODE_TYPES.CallExpression) {
148+
if (node.parent.type !== AST_NODE_TYPES.CallExpression) {
156149
callStack.push('arrow');
157150
}
158151
},

src/rules/no-unneeded-async-expect-function.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default createRule({
2727

2828
const { parent } = jestFnCall.head.node;
2929

30-
if (parent?.type !== AST_NODE_TYPES.CallExpression) {
30+
if (parent.type !== AST_NODE_TYPES.CallExpression) {
3131
return;
3232
}
3333

@@ -36,7 +36,7 @@ export default createRule({
3636
if (
3737
!awaitNode ||
3838
!isFunction(awaitNode) ||
39-
!awaitNode?.async ||
39+
!awaitNode.async ||
4040
awaitNode.body.type !== AST_NODE_TYPES.BlockStatement ||
4141
awaitNode.body.body.length !== 1
4242
) {

src/rules/prefer-comparison-matcher.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export default createRule({
7777

7878
const { parent: expect } = jestFnCall.head.node;
7979

80-
if (expect?.type !== AST_NODE_TYPES.CallExpression) {
80+
if (expect.type !== AST_NODE_TYPES.CallExpression) {
8181
return;
8282
}
8383

@@ -90,7 +90,7 @@ export default createRule({
9090
const matcherArg = getFirstMatcherArg(jestFnCall);
9191

9292
if (
93-
comparison?.type !== AST_NODE_TYPES.BinaryExpression ||
93+
comparison.type !== AST_NODE_TYPES.BinaryExpression ||
9494
isComparingToString(comparison) ||
9595
!EqualityMatcher.hasOwnProperty(getAccessorValue(matcher)) ||
9696
!isBooleanLiteral(matcherArg)

src/rules/prefer-equality-matcher.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export default createRule({
3535

3636
const { parent: expect } = jestFnCall.head.node;
3737

38-
if (expect?.type !== AST_NODE_TYPES.CallExpression) {
38+
if (expect.type !== AST_NODE_TYPES.CallExpression) {
3939
return;
4040
}
4141

@@ -48,7 +48,7 @@ export default createRule({
4848
const matcherArg = getFirstMatcherArg(jestFnCall);
4949

5050
if (
51-
comparison?.type !== AST_NODE_TYPES.BinaryExpression ||
51+
comparison.type !== AST_NODE_TYPES.BinaryExpression ||
5252
(comparison.operator !== '===' && comparison.operator !== '!==') ||
5353
!EqualityMatcher.hasOwnProperty(getAccessorValue(matcher)) ||
5454
!isBooleanLiteral(matcherArg)

src/rules/prefer-expect-resolves.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ export default createRule({
2626

2727
const { parent } = jestFnCall.head.node;
2828

29-
if (parent?.type !== AST_NODE_TYPES.CallExpression) {
29+
if (parent.type !== AST_NODE_TYPES.CallExpression) {
3030
return;
3131
}
3232

3333
const [awaitNode] = parent.arguments;
3434

35-
if (awaitNode?.type === AST_NODE_TYPES.AwaitExpression) {
35+
if (awaitNode.type === AST_NODE_TYPES.AwaitExpression) {
3636
context.report({
3737
node: awaitNode,
3838
messageId: 'expectResolves',

src/rules/prefer-importing-jest-globals.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export default createRule({
8686

8787
const isModule =
8888
context.parserOptions.sourceType === 'module' ||
89-
context.languageOptions?.sourceType === 'module';
89+
context.languageOptions.sourceType === 'module';
9090

9191
context.report({
9292
node: reportingNode,

src/rules/prefer-spy-on.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const getAutoFixMockImplementation = (
4848
context: TSESLint.RuleContext<'useJestSpyOn', unknown[]>,
4949
): string => {
5050
const hasMockImplementationAlready =
51-
jestFnCall.parent?.type === AST_NODE_TYPES.MemberExpression &&
51+
jestFnCall.parent.type === AST_NODE_TYPES.MemberExpression &&
5252
jestFnCall.parent.property.type === AST_NODE_TYPES.Identifier &&
5353
jestFnCall.parent.property.name === 'mockImplementation';
5454

src/rules/prefer-to-be.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const reportPreferToBe = (
7373
replaceAccessorFixer(fixer, expectFnCall.matcher, `toBe${whatToBe}`),
7474
];
7575

76-
if (expectFnCall.args?.length && whatToBe !== '') {
76+
if (expectFnCall.args.length && whatToBe !== '') {
7777
fixes.push(removeExtraArgumentsFixer(fixer, context, func, 0));
7878
}
7979

src/rules/prefer-to-contain.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export default createRule({
5959

6060
const { parent: expect } = jestFnCall.head.node;
6161

62-
if (expect?.type !== AST_NODE_TYPES.CallExpression) {
62+
if (expect.type !== AST_NODE_TYPES.CallExpression) {
6363
return;
6464
}
6565

0 commit comments

Comments
 (0)