Skip to content

Commit

Permalink
Use variable separator when converting variable name from camelCase (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bhirsz authored Aug 4, 2024
1 parent 5e01124 commit 487d348
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
12 changes: 12 additions & 0 deletions docs/releasenotes/unreleased/transformers.2.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Use variable_separator when converting variable from camelCase in RenameVariables (#705)
----------------------------------------------------------------------------------------

Previously ``variable_separator`` configuration was not respected when converting variable names from camelCase to
snake_case. In result variable names were converted with spaces as the separator::

# from
${camelCase}
# to
${camel case}

Now the setting will be take into account.
6 changes: 4 additions & 2 deletions robotidy/transformers/RenameVariables.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ def __init__(self):
self._local = set()
self._global = set()

def _get_var_name(self, variable: str) -> "str|None":
@staticmethod
def _get_var_name(variable: str) -> "str|None":
if len(variable) > 1 and variable[0] in "$@&" and variable[1] != "{":
variable = f"{variable[0]}{{{variable[1:]}}}"
match = search_variable(variable, ignore_errors=True)
Expand Down Expand Up @@ -543,7 +544,8 @@ def rename(self, variable_value: str, case: VariableCase, strip_fn: str = "strip
# 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:
variable_name = self.CAMEL_CASE.sub(r" \1", variable_name)
var_sep = " " if self.variable_separator == VariableSeparator.SPACE else "_"
variable_name = self.CAMEL_CASE.sub(rf"{var_sep}\1", variable_name)
if self.variable_separator != VariableSeparator.IGNORE:
variable_name = variable_name.replace("_", " ")
variable_name = self.MORE_THAN_2_SPACES.sub(" ", variable_name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ ${INLINE_EVAL} ${{ eval }}
... other ${VALUE}
... ${{embedd_ ed}

${CAMEL CASE NAME} ${CAMEL CASE NAME}
${CAMEL CASE NAME} ${CAMEL CASE NAME}
${CAMEL CASE NAME} ${CAMEL CASE NAME}
${CAMEL CASE NAME_WORD_CAMEL CASE} ${CAMEL CASE NAME_WORD_CAMEL CASE}
${CAMEL_CASE_NAME} ${CAMEL_CASE_NAME}
${CAMEL_CASE_NAME} ${CAMEL_CASE_NAME}
${CAMEL_CASE_NAME} ${CAMEL_CASE_NAME}
${CAMEL_CASE_NAME_WORD_CAMEL_CASE} ${CAMEL_CASE_NAME_WORD_CAMEL_CASE}


*** Test Cases ***
Expand All @@ -56,7 +56,7 @@ Assign

Args
Keyword ${VARIABLE}
Keyword ${V A _RI ABLES}
Keyword ${V A _RI_ABLES}
... value with ${_ VARIABLE _}

For header
Expand Down

0 comments on commit 487d348

Please sign in to comment.