{"record":{"id":"5b2023498127c3dc","repo":"GitbookIO/gitbook","slug":"empty-or-invalid-expression","errorCode":null,"errorMessage":"Empty or invalid expression","messagePattern":"Empty or invalid expression","errorType":"exception","errorClass":"ExpressionError","httpStatus":null,"severity":"error","filePath":"packages/expr/src/runtime.ts","lineNumber":202,"sourceCode":"        }\n    }\n\n    /**\n     * Parses a binary expression and returns an @ExpressionParserResult.\n     */\n    public parse(\n        expr: string,\n        options: { loose?: boolean } = {\n            loose: false,\n        }\n    ): ExpressionParserResult {\n        try {\n            const ast = options.loose\n                ? parseLoose(expr, { ...this.#parserOptions })\n                : parse(expr, { ...this.#parserOptions });\n\n            if (!ast.body || ast.body.length === 0) {\n                throw new ExpressionError('Empty or invalid expression');\n            }\n\n            // Extract the first expression statement that we find\n            const firstExprIndex = ast.body.findIndex((node) => isParsedExpressionStatement(node));\n            const [statement] = ast.body.splice(firstExprIndex, 1);\n\n            if (!statement || !isParsedExpressionStatement(statement)) {\n                throw new ExpressionError('Empty or invalid expression');\n            }\n\n            // Return information on the other nodes as invalid nodes\n            const invalidNodes = ast.body.filter(filterOutModuleDeclarationStatement);\n\n            return {\n                result: statement.expression,\n                invalidNodes,\n            };\n        } catch (error) {","sourceCodeStart":184,"sourceCodeEnd":220,"githubUrl":"https://github.com/GitbookIO/gitbook/blob/db67585ee243d063c459a855988f21612cea9c95/packages/expr/src/runtime.ts#L184-L220","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nconst value = runtime.evaluate(expr /* '' */, inputs);\n\n// after\nconst value = expr.trim() ? runtime.evaluate(expr, inputs) : true;","handlingStrategy":"validation","validationCode":"if (!expr.trim()) {\n    return true; // sensible default instead of parsing\n}\nconst value = runtime.evaluate(expr, inputs);","typeGuard":"function isNonEmptyExpression(expr: string): boolean {\n    return expr.trim().length > 0;\n}","tryCatchPattern":null,"preventionTips":["Default blank expression fields to a safe literal","Trim user input and reject empty strings in the editor"],"tags":["expression","parsing","empty-input","expr"],"backgroundTag":"expression-parse-error","analyzedSha":"db67585ee243d063c459a855988f21612cea9c95","analyzedAt":"2026-08-28T17:49:47.831Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}