From cc218809f69bf21669d17b623f788afcf6c12c45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Lef=C3=A8vre=20=28lul=29?= Date: Mon, 12 Aug 2024 07:41:30 +0000 Subject: [PATCH] [FIX] parser: parsing empty string throws meaningful error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `parse("")` throws an error like `cannot ready property of undefined`. With this commit, it throws a user friendly message. Note that it doesn't cause any (known) real bug, but let's make it more robust nonetheless. closes odoo/o-spreadsheet#4830 Task: 0 X-original-commit: 76c0de41612c76329627ed5fbdc65a46813d0725 Signed-off-by: Rémi Rahir (rar) Signed-off-by: Lucas Lefèvre (lul) --- src/formulas/parser.ts | 2 +- tests/evaluation/parser.test.ts | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/formulas/parser.ts b/src/formulas/parser.ts index feadcacc95..3bb3d89702 100644 --- a/src/formulas/parser.ts +++ b/src/formulas/parser.ts @@ -229,7 +229,7 @@ export function parse(str: string): AST { export function parseTokens(tokens: Token[]): AST { tokens = tokens.filter((x) => x.type !== "SPACE"); - if (tokens[0].value === "=") { + if (tokens[0]?.value === "=") { tokens.splice(0, 1); } const result = parseExpression(tokens); diff --git a/tests/evaluation/parser.test.ts b/tests/evaluation/parser.test.ts index a85095b049..8626e241bc 100644 --- a/tests/evaluation/parser.test.ts +++ b/tests/evaluation/parser.test.ts @@ -171,6 +171,10 @@ describe("parser", () => { expect(() => parse("#REF")).toThrowError(new InvalidReferenceError().message); }); + test("Cannot parse empty string", () => { + expect(() => parse("")).toThrowError("Invalid expression"); + }); + test("AND", () => { expect(parse("=AND(true, false)")).toEqual({ type: "FUNCALL",