From d4ef7c3376f0633a935afbd1c855d18752a8578f Mon Sep 17 00:00:00 2001 From: Yaacov Rydzinski Date: Tue, 10 Mar 2026 15:32:24 +0200 Subject: [PATCH] polish: fix deno TS errors --- resources/inline-invariant.ts | 47 +++++++++++++++++++----------- src/utilities/buildClientSchema.ts | 16 +++++----- 2 files changed, 39 insertions(+), 24 deletions(-) diff --git a/resources/inline-invariant.ts b/resources/inline-invariant.ts index 21c969f2a3..9b73f1a3eb 100644 --- a/resources/inline-invariant.ts +++ b/resources/inline-invariant.ts @@ -9,7 +9,7 @@ import ts from 'typescript'; * * to: * - * () || invariant(false ...) + * if (!()) invariant(false, ...) */ export function inlineInvariant(context: ts.TransformationContext) { const { factory } = context; @@ -21,25 +21,38 @@ export function inlineInvariant(context: ts.TransformationContext) { } function visitNode(node: ts.Node): ts.Node { - if (ts.isCallExpression(node)) { - const { expression, arguments: args } = node; - - if (ts.isIdentifier(expression) && args.length > 0) { - const funcName = expression.escapedText; - if (funcName === 'invariant' || funcName === 'devAssert') { - const [condition, ...otherArgs] = args; - - return factory.createBinaryExpression( - factory.createParenthesizedExpression(condition), - ts.SyntaxKind.BarBarToken, - factory.createCallExpression(expression, undefined, [ - factory.createFalse(), - ...otherArgs, - ]), - ); + if (ts.isExpressionStatement(node)) { + const expression = node.expression; + + if (ts.isCallExpression(expression)) { + const { arguments: args } = expression; + + if (ts.isIdentifier(expression.expression) && args.length > 0) { + const funcName = expression.expression.escapedText; + if (funcName === 'invariant' || funcName === 'devAssert') { + const [condition, ...otherArgs] = args; + if (condition.kind === ts.SyntaxKind.FalseKeyword) { + return node; + } + const inverseCondition = factory.createPrefixUnaryExpression( + ts.SyntaxKind.ExclamationToken, + factory.createParenthesizedExpression(condition), + ); + + return factory.createIfStatement( + inverseCondition, + factory.createExpressionStatement( + factory.createCallExpression(expression.expression, undefined, [ + factory.createFalse(), + ...otherArgs, + ]), + ), + ); + } } } } + return ts.visitEachChild(node, visitNode, context); } } diff --git a/src/utilities/buildClientSchema.ts b/src/utilities/buildClientSchema.ts index c760e1ec95..6890ddec7c 100644 --- a/src/utilities/buildClientSchema.ts +++ b/src/utilities/buildClientSchema.ts @@ -190,14 +190,16 @@ export function buildClientSchema( return buildEnumDef(type); case TypeKind.INPUT_OBJECT: return buildInputObjectDef(type); + default: + // TypeScript considers this unreachable, but invalid runtime input can reach it. + // Note: we include a default case rather than throwing after the switch to avoid + // the use of a @ts-expect-error statement. + throw new Error( + `Invalid or incomplete introspection result. Ensure that a full introspection query is used in order to build a client schema: ${inspect( + type, + )}.`, + ); } - // Unreachable. - // @ts-expect-error - throw new Error( - `Invalid or incomplete introspection result. Ensure that a full introspection query is used in order to build a client schema: ${inspect( - type, - )}.`, - ); } function buildScalarDef(