GitbookIO/gitbook · error · ExpressionError
Empty or invalid expression
Error message
Empty or invalid expression
What it means
Thrown by ExpressionRuntime.parse in @gitbook/expr when the parsed program has no body statements (or an empty one), meaning the expression is empty or wholly unparseable. Both the strict and loose parsers returned an AST with nothing executable in it, so downstream evaluation or variable extraction is impossible.
Source
Thrown at packages/expr/src/runtime.ts:202
}
}
/**
* Parses a binary expression and returns an @ExpressionParserResult.
*/
public parse(
expr: string,
options: { loose?: boolean } = {
loose: false,
}
): ExpressionParserResult {
try {
const ast = options.loose
? parseLoose(expr, { ...this.#parserOptions })
: parse(expr, { ...this.#parserOptions });
if (!ast.body || ast.body.length === 0) {
throw new ExpressionError('Empty or invalid expression');
}
// Extract the first expression statement that we find
const firstExprIndex = ast.body.findIndex((node) => isParsedExpressionStatement(node));
const [statement] = ast.body.splice(firstExprIndex, 1);
if (!statement || !isParsedExpressionStatement(statement)) {
throw new ExpressionError('Empty or invalid expression');
}
// Return information on the other nodes as invalid nodes
const invalidNodes = ast.body.filter(filterOutModuleDeclarationStatement);
return {
result: statement.expression,
invalidNodes,
};
} catch (error) {View on GitHub (pinned to db67585ee2)
Solutions
- Skip evaluation when the expression is empty/whitespace-only (early return a default)
- Default missing expression fields to a sensible literal like 'true' or 'null' depending on context
- Trim and validate input in the editor/UI before submitting to the runtime
Example fix
// before const value = runtime.evaluate(expr /* '' */, inputs); // after const value = expr.trim() ? runtime.evaluate(expr, inputs) : true;
Defensive patterns
Strategy: validation
Validate before calling
if (!expr.trim()) {
return true; // sensible default instead of parsing
}
const value = runtime.evaluate(expr, inputs); Type guard
function isNonEmptyExpression(expr: string): boolean {
return expr.trim().length > 0;
} Prevention
- Default blank expression fields to a safe literal
- Trim user input and reject empty strings in the editor
When it happens
Trigger: Calling parse/evaluate/getVariables with an empty string, a whitespace-only string, or a string consisting only of comments or characters the parser drops entirely.
Common situations: Optional expression fields left blank ('' or undefined stringified); trimming user input down to nothing before passing it; building expressions dynamically so an empty fragment reaches the runtime.
Related errors
- Invalid nodes found when parsing
- Expected schema of object to be provided: ${schema.type}
- Unsupported schema type: ${schema.type}
AI-assisted analysis of GitbookIO/gitbook@db67585ee2 (2026-08-28).
Data as JSON: /api/errors/5b2023498127c3dc.
Report an issue: GitHub.