Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rename variables with extended syntax #553

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}
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")
Loading