Skip to content

Commit

Permalink
[FIX] parser: inconsistent handling of #REF
Browse files Browse the repository at this point in the history
Depending on how we get a formula with #REF, the parser can have
different behavior even if the formula is the same.

- If we write directly =#REF (or it comes from a snapshot), the parser
  will throw an error.
- If we write =A1, then delete the row 1 to get =#REF, the parser will
return a REFERENCE token with an #REF value.

This is a bit of a problem when exporting the data to xlsx: since
we parse the formulas to know whether we need to export it, we get
a different result depending on how we got the #REF.

Task: 4207052
X-original-commit: f20d87a
Part-of: #5151
Signed-off-by: Lucas Lefèvre (lul) <[email protected]>
Signed-off-by: Adrien Minne (adrm) <[email protected]>
  • Loading branch information
hokolomopo committed Oct 29, 2024
1 parent 1aa1b2c commit c6a75e6
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/formulas/parser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { parseNumber, removeStringQuotes } from "../helpers/index";
import { _t } from "../translation";
import { DEFAULT_LOCALE } from "../types";
import { BadExpressionError, InvalidReferenceError } from "../types/errors";
import { BadExpressionError, CellErrorType } from "../types/errors";
import { rangeTokenize } from "./range_tokenizer";
import { Token } from "./tokenizer";

Expand Down Expand Up @@ -112,7 +112,11 @@ function parseOperand(tokens: Token[]): AST {
case "STRING":
return { type: "STRING", value: removeStringQuotes(current.value) };
case "INVALID_REFERENCE":
throw new InvalidReferenceError();
return {
type: "REFERENCE",
value: CellErrorType.InvalidReference,
};

case "REFERENCE":
if (tokens[0]?.value === ":" && tokens[1]?.type === "REFERENCE") {
tokens.shift();
Expand Down
7 changes: 5 additions & 2 deletions tests/evaluation/parser.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { astToFormula, parse } from "../../src";
import { InvalidReferenceError } from "../../src/types/errors";
import { CellErrorType } from "../../src/types/errors";

describe("parser", () => {
test("can parse a function call with no argument", () => {
Expand Down Expand Up @@ -168,7 +168,10 @@ describe("parser", () => {
});

test("Can parse invalid references", () => {
expect(() => parse("#REF")).toThrowError(new InvalidReferenceError().message);
expect(parse("#REF")).toEqual({
type: "REFERENCE",
value: CellErrorType.InvalidReference,
});
});

test("Cannot parse empty string", () => {
Expand Down

0 comments on commit c6a75e6

Please sign in to comment.