Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 30 additions & 17 deletions resources/inline-invariant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import ts from 'typescript';
*
* to:
*
* (<cond>) || invariant(false ...)
* if (!(<cond>)) invariant(false, ...)
*/
export function inlineInvariant(context: ts.TransformationContext) {
const { factory } = context;
Expand All @@ -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);
}
}
16 changes: 9 additions & 7 deletions src/utilities/buildClientSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading