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 RenameVariables replacing = with _= and renaming numerical values #693

Merged
merged 1 commit into from
May 14, 2024
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
9 changes: 9 additions & 0 deletions docs/releasenotes/unreleased/fixes.2.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
RenameVariables adding _ to variables with equal sign in Variables section (#692)
---------------------------------------------------------------------------------

Following code::

*** Variables ***
${random_seed} = ${None}

was formatted incorrectly - ``_`` was added before ``=``.
9 changes: 9 additions & 0 deletions docs/releasenotes/unreleased/transformers.2.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Ignoring additional variables in RenameVariables (#692)
-------------------------------------------------------

``RenameVariables`` will now ignore and do not transform following variables:

- ``${None}``
- ``${True}``
- ``${False}``
- numerical values such as ``${0x1A}`` or ``${0b01010}``
7 changes: 6 additions & 1 deletion docs/source/transformers/RenameVariables.rst
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,12 @@ Ignore variable case
--------------------

Case of all variables is converted according to the configured conventions. It is possible to pass the names of the
variables that should be ignored. By default, only ${\n} ``${\n}`` variable case is ignored and not transformed.
variables that should be ignored. By default, following variables case is ignored and not transformerd:

- ``${\n}``
- ``${None}``
- ``${True}``
- ``${False}``

Configure ``ignore_case`` to ignore an additional list. This parameter accepts comma-separated list of variable names
(case-sensitive)::
Expand Down
24 changes: 20 additions & 4 deletions robotidy/transformers/RenameVariables.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ def is_nested_variable(variable: str) -> bool:
return bool(match.base)


def is_name_hex_or_binary(variable: str) -> bool:
if "x" in variable:
base = 16
elif "b" in variable:
base = 2
else:
return False
try:
int(variable, base)
except ValueError:
return False
return True


def resolve_var_name(name: str) -> str:
"""Resolve name of the variable from \\${name} or $name syntax."""
if name.startswith("\\"):
Expand Down Expand Up @@ -201,7 +215,7 @@ class RenameVariables(Transformer):
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"}
DEFAULT_IGNORE_CASE = {"\\n", "None", "True", "False"}

def __init__(
self,
Expand Down Expand Up @@ -493,10 +507,10 @@ def rename_value(self, value: str, variable_case: VariableCase, is_var: bool = F
name += base
after = match.after
if after:
if is_var:
name += self.rename(after, case=variable_case, strip_fn="rstrip")
else:
if not is_var or after.strip() == "=":
name += after
else:
name += self.rename(after, case=variable_case, strip_fn="rstrip")
return name

def set_name_case(self, name: str, case: VariableCase):
Expand Down Expand Up @@ -524,6 +538,8 @@ def set_case_for_local_and_global(self, name):
def rename(self, variable_value: str, case: VariableCase, strip_fn: str = "strip"):
if not variable_value:
return variable_value
if is_name_hex_or_binary(variable_value):
return variable_value
# split on variable attribute access like ${var['item']}, ${var.item}, ${var(method)}..
variable_name, item_access = split_string_on_delimiter(variable_value)
if self.convert_camel_case:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@ True and False
ELIF ${False}
Log The lie.
END

Numerical Values
Log ${0x1A}
Log ${0b0110101}
Log ${5}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*** Variables ***
${RANDOM_SEED} = ${None}


*** Keywords ***
Keyword With Sign
${value} = Keyword Call ${RANDOM_SEED}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ Environment variable
True and False
IF $True
Log The truth.
ELIF ${FALSE}
ELIF ${False}
Log The lie.
END

Numerical Values
Log ${0x1A}
Log ${0b0110101}
Log ${5}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*** Variables ***
${random_seed} = ${None}


*** Keywords ***
Keyword With Sign
${value} = Keyword Call ${random_seed}
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@ True and False
ELIF ${False}
Log The lie.
END

Numerical Values
Log ${0x1A}
Log ${0b0110101}
Log ${5}
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def test_extended_variables_syntax(self):
self.compare(source="math_operations.robot")

def test_var_syntax(self):
self.compare(
source="VAR_syntax.robot",
target_version=">6.1.1",
)
self.compare(source="VAR_syntax.robot", target_version=">6.1.1")

def test_equal_sign_in_section(self):
self.compare(source="equal_sign_in_section.robot")
Loading