mirror of
https://github.com/gristlabs/grist-core.git
synced 2026-03-02 04:09:24 +00:00
(core) Fix bug with renaming when a formula uses a local name for a user table.
Summary: When a formula used a local variable referring to a user table (which is a global name), e.g. `myvar = MyTable; myvar.lookupOne(...)`, renaming logic mistakenly used the inferred name (`MyTable`) in places where the actual variable name (`myvar`) should have been used. Additionally, we should not rename local variables, even if they match a global name. This fixes both issues. Test Plan: Added a test case that checks that local variables aren't renamed. Reviewers: georgegevoian Reviewed By: georgegevoian Differential Revision: https://phab.getgrist.com/D3846
This commit is contained in:
@@ -195,6 +195,11 @@ def _is_table(node):
|
||||
return (isinstance(node, astroid.nodes.ClassDef) and node.decorators and
|
||||
node.decorators.nodes[0].as_string() == 'grist.UserTable')
|
||||
|
||||
def _is_local(node):
|
||||
"""
|
||||
Returns true if node is a Name node for an innermost variable.
|
||||
"""
|
||||
return isinstance(node, astroid.nodes.Name) and node.name in node.scope().locals
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
@@ -394,14 +399,15 @@ def parse_grist_names(builder):
|
||||
in_text, in_value, in_patch = patch_source
|
||||
if in_value:
|
||||
return (in_value, in_patch.start, table_id, col_id)
|
||||
return None
|
||||
|
||||
parsed_names = []
|
||||
for node in asttokens.util.walk(atok.tree):
|
||||
if isinstance(node, astroid.nodes.Name):
|
||||
obj = infer(node)
|
||||
if _is_table(obj):
|
||||
if _is_table(obj) and not _is_local(node):
|
||||
start, end = atok.get_text_range(node)
|
||||
parsed_names.append(make_tuple(start, end, obj.name, None))
|
||||
parsed_names.append(make_tuple(start, end, node.name, None))
|
||||
|
||||
elif isinstance(node, astroid.nodes.Attribute):
|
||||
obj = infer(node.expr)
|
||||
@@ -431,7 +437,7 @@ def save_to_linecache(source_code):
|
||||
Makes source code available to friendly-traceback and traceback formatting in general.
|
||||
"""
|
||||
if six.PY3:
|
||||
import friendly_traceback.source_cache
|
||||
import friendly_traceback.source_cache # pylint: disable=import-error
|
||||
|
||||
friendly_traceback.source_cache.cache.add(code_filename, source_code)
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user