From c165961459f2a392d4c39a72ffa279ae58cfcd1b Mon Sep 17 00:00:00 2001 From: Bartlomiej Hirsz Date: Thu, 1 Aug 2024 14:50:56 +0200 Subject: [PATCH] Use variable separator when converting variable name from camelCase --- docs/releasenotes/unreleased/transformers.2.rst | 12 ++++++++++++ robotidy/transformers/RenameVariables.py | 6 ++++-- .../expected/test_ignore_var_separator.robot | 10 +++++----- 3 files changed, 21 insertions(+), 7 deletions(-) create mode 100644 docs/releasenotes/unreleased/transformers.2.rst diff --git a/docs/releasenotes/unreleased/transformers.2.rst b/docs/releasenotes/unreleased/transformers.2.rst new file mode 100644 index 00000000..c2be5dad --- /dev/null +++ b/docs/releasenotes/unreleased/transformers.2.rst @@ -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. diff --git a/robotidy/transformers/RenameVariables.py b/robotidy/transformers/RenameVariables.py index 6a4d771c..600d42f2 100644 --- a/robotidy/transformers/RenameVariables.py +++ b/robotidy/transformers/RenameVariables.py @@ -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) @@ -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) diff --git a/tests/atest/transformers/RenameVariables/expected/test_ignore_var_separator.robot b/tests/atest/transformers/RenameVariables/expected/test_ignore_var_separator.robot index df7b0d28..180af5c9 100644 --- a/tests/atest/transformers/RenameVariables/expected/test_ignore_var_separator.robot +++ b/tests/atest/transformers/RenameVariables/expected/test_ignore_var_separator.robot @@ -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 *** @@ -56,7 +56,7 @@ Assign Args Keyword ${VARIABLE} - Keyword ${V A _RI ABLES} + Keyword ${V A _RI_ABLES} ... value with ${_ VARIABLE _} For header