Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.

Commit a427761

Browse files
ESLint: Enable 'no-unnecessary-condition' rule (#737)
1 parent 20e3466 commit a427761

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

.eslintrc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ overrides:
491491
'@typescript-eslint/no-throw-literal': error
492492
'@typescript-eslint/no-type-alias': off # TODO consider
493493
'@typescript-eslint/no-unnecessary-boolean-literal-compare': error
494-
'@typescript-eslint/no-unnecessary-condition': off # TODO blocked by https://github.com/typescript-eslint/typescript-eslint/issues/2752
494+
'@typescript-eslint/no-unnecessary-condition': error
495495
'@typescript-eslint/no-unnecessary-qualifier': error
496496
'@typescript-eslint/no-unnecessary-type-arguments': error
497497
'@typescript-eslint/no-unnecessary-type-assertion': error

src/index.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ type Middleware = (request: Request, response: Response) => Promise<void>;
187187
* configure behavior, and returns an express middleware.
188188
*/
189189
export function graphqlHTTP(options: Options): Middleware {
190-
devAssert(options != null, 'GraphQL middleware requires options.');
190+
devAssertIsNonNullable(options, 'GraphQL middleware requires options.');
191191

192192
return async function graphqlMiddleware(
193193
request: Request,
@@ -239,9 +239,8 @@ export function graphqlHTTP(options: Options): Middleware {
239239
optionsData.formatError ??
240240
formatErrorFn;
241241

242-
// Assert that schema is required.
243-
devAssert(
244-
schema != null,
242+
devAssertIsObject(
243+
schema,
245244
'GraphQL middleware options must contain a schema.',
246245
);
247246

@@ -432,8 +431,8 @@ export function graphqlHTTP(options: Options): Middleware {
432431
: options,
433432
);
434433

435-
devAssert(
436-
optionsResult != null && typeof optionsResult === 'object',
434+
devAssertIsObject(
435+
optionsResult,
437436
'GraphQL middleware option function must return an options object or a promise which will be resolved to an options object.',
438437
);
439438

@@ -533,9 +532,17 @@ function sendResponse(response: Response, type: string, data: string): void {
533532
response.end(chunk);
534533
}
535534

536-
function devAssert(condition: unknown, message: string): asserts condition {
535+
function devAssertIsObject(value: unknown, message: string): void {
536+
devAssert(value != null && typeof value === 'object', message);
537+
}
538+
539+
function devAssertIsNonNullable(value: unknown, message: string): void {
540+
devAssert(value != null, message);
541+
}
542+
543+
function devAssert(condition: unknown, message: string): void {
537544
const booleanCondition = Boolean(condition);
538545
if (!booleanCondition) {
539-
throw new Error(message);
546+
throw new TypeError(message);
540547
}
541548
}

0 commit comments

Comments
 (0)