Skip to content

Commit

Permalink
Fix Robotidy not recognizing variables using extended variables syntax (
Browse files Browse the repository at this point in the history
  • Loading branch information
bhirsz authored Aug 1, 2023
1 parent ba0bb35 commit 8df48fe
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
10 changes: 10 additions & 0 deletions docs/releasenotes/4.4.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,13 @@ it was transformed to::

Additionally, number of spaces before `ELSE` and `ELSE IF` in Inline IFs should now be calculated correctly
(based on the separator length and not indentation).

Invalid variable case with extended syntax in RenameVariables (#551)
--------------------------------------------------------------------

Robotidy should now be able to recognize variables using extended variable syntax and set variable case accordingly::

*** Test Cases ***
Simple math operations
${i} Set Variable ${0}
Log ${i+1} # i+1 instead of previous I+1
18 changes: 13 additions & 5 deletions robotidy/transformers/RenameVariables.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ class RenameVariables(Transformer):
HANDLES_SKIP = frozenset({"skip_sections"})
MORE_THAN_2_SPACES: Pattern = re.compile(r"\s{2,}")
CAMEL_CASE: Pattern = re.compile(r"((?<=[a-z0-9])[A-Z]|(?!^)[A-Z](?=[a-z]))")
EXTENDED_SYNTAX: Pattern = re.compile(r"(.+?)([^\s\w].+)", re.UNICODE)
DEFAULT_IGNORE_CASE = {"\\n"}

def __init__(
Expand Down Expand Up @@ -424,13 +425,20 @@ def set_name_case(self, name: str, case: str):
if case == "lower":
return name.lower()
if case == "auto":
if self.variables_scope.is_local(name):
return name.lower()
if self.variables_scope.is_global(name):
return name.upper()
return self.set_name_case(name, self.unknown_variables_case)
return self.set_case_for_local_and_global(name)
return name

def set_case_for_local_and_global(self, name):
if self.variables_scope.is_local(name):
return name.lower()
if self.variables_scope.is_global(name):
return name.upper()
extended_syntax = self.EXTENDED_SYNTAX.match(name)
if extended_syntax is None:
return self.set_name_case(name, self.unknown_variables_case)
base_name, extended = extended_syntax.groups()
return self.set_case_for_local_and_global(base_name) + extended

def rename(self, variable_value: str, case: str, strip_fn: str = "strip"):
if not variable_value:
return variable_value
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
*** Test Cases ***
Simple math operations
${i} Set Variable ${0}
Log ${i+1}

Global Value
Log ${I-1}

Invalid syntax
${i} Set Variable ${0}
${y} Set Variable ${1}
Log ${i+y+1}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
*** Test Cases ***
Simple math operations
${i} Set Variable ${0}
Log ${i+1}

Global Value
Log ${i-1}

Invalid syntax
${i} Set Variable ${0}
${y} Set Variable ${1}
Log ${i+y+1}
3 changes: 3 additions & 0 deletions tests/atest/transformers/RenameVariables/test_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,6 @@ def test_exclude_configure_ignore_vars(self):
expected="configure_ignore_vars.robot",
config=":ignore_case=global_arg,env_var,False",
)

def test_extended_variables_syntax(self):
self.compare(source="math_operations.robot")

0 comments on commit 8df48fe

Please sign in to comment.