(core) Ignore leading whitespace in formulas, and strip out leading '=' sign users might add

Summary:
This addresses two issues, differently:
- For a formula with leading whitespace, like " 1+1", it is stored as is, but
  is fixed to work (it should be valid Python, and whitespace is only stripped out
  at parsing time to avoid intentation errors caused by the way it gets parsed)
- For a formula with a leading equals-sign ("="), it is stripped out on the
  client side before the formula is stored. Grist documentation uses leading
  "=" to indicate formulas (because UI shows an "=" icon), and Excel formulas
  actually contain the leading "=", so it is a common mistake to include it.

Test Plan: Added new test cases

Reviewers: jarek

Reviewed By: jarek

Differential Revision: https://phab.getgrist.com/D3873
This commit is contained in:
Dmitry S
2023-04-24 23:01:26 -04:00
parent d6abe6a737
commit b4cc519616
6 changed files with 42 additions and 8 deletions

View File

@@ -3,10 +3,11 @@ import contextlib
import itertools
import linecache
import re
import six
import textwrap
import astroid
import asttokens
import six
import friendly_errors
import textbuilder
@@ -41,6 +42,11 @@ def make_formula_body(formula, default_value, assoc_value=None):
if isinstance(formula, six.binary_type):
formula = formula.decode('utf8')
# Remove any common leading whitespace. In python, extra indent should not be an error, but
# it is in Grist because we parse the formula body before it gets inserted into a function (i.e.
# as if at module level).
formula = textwrap.dedent(formula)
if not formula.strip():
return textbuilder.Text('return ' + repr(default_value), assoc_value)