Skip to content

Commit

Permalink
[FIX] parser: parsing empty string throws meaningful error
Browse files Browse the repository at this point in the history
`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 #4830

Task: 0
X-original-commit: 76c0de4
Signed-off-by: Rémi Rahir (rar) <[email protected]>
Signed-off-by: Lucas Lefèvre (lul) <[email protected]>
  • Loading branch information
LucasLefevre committed Aug 12, 2024
1 parent bee93a4 commit cc21880
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/formulas/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions tests/evaluation/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit cc21880

Please sign in to comment.