Skip to content

Commit 487d348

Browse files
authored
Use variable separator when converting variable name from camelCase (#708)
1 parent 5e01124 commit 487d348

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Use variable_separator when converting variable from camelCase in RenameVariables (#705)
2+
----------------------------------------------------------------------------------------
3+
4+
Previously ``variable_separator`` configuration was not respected when converting variable names from camelCase to
5+
snake_case. In result variable names were converted with spaces as the separator::
6+
7+
# from
8+
${camelCase}
9+
# to
10+
${camel case}
11+
12+
Now the setting will be take into account.

robotidy/transformers/RenameVariables.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ def __init__(self):
9999
self._local = set()
100100
self._global = set()
101101

102-
def _get_var_name(self, variable: str) -> "str|None":
102+
@staticmethod
103+
def _get_var_name(variable: str) -> "str|None":
103104
if len(variable) > 1 and variable[0] in "$@&" and variable[1] != "{":
104105
variable = f"{variable[0]}{{{variable[1:]}}}"
105106
match = search_variable(variable, ignore_errors=True)
@@ -543,7 +544,8 @@ def rename(self, variable_value: str, case: VariableCase, strip_fn: str = "strip
543544
# split on variable attribute access like ${var['item']}, ${var.item}, ${var(method)}..
544545
variable_name, item_access = split_string_on_delimiter(variable_value)
545546
if self.convert_camel_case:
546-
variable_name = self.CAMEL_CASE.sub(r" \1", variable_name)
547+
var_sep = " " if self.variable_separator == VariableSeparator.SPACE else "_"
548+
variable_name = self.CAMEL_CASE.sub(rf"{var_sep}\1", variable_name)
547549
if self.variable_separator != VariableSeparator.IGNORE:
548550
variable_name = variable_name.replace("_", " ")
549551
variable_name = self.MORE_THAN_2_SPACES.sub(" ", variable_name)

tests/atest/transformers/RenameVariables/expected/test_ignore_var_separator.robot

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ ${INLINE_EVAL} ${{ eval }}
3939
... other ${VALUE}
4040
... ${{embedd_ ed}
4141

42-
${CAMEL CASE NAME} ${CAMEL CASE NAME}
43-
${CAMEL CASE NAME} ${CAMEL CASE NAME}
44-
${CAMEL CASE NAME} ${CAMEL CASE NAME}
45-
${CAMEL CASE NAME_WORD_CAMEL CASE} ${CAMEL CASE NAME_WORD_CAMEL CASE}
42+
${CAMEL_CASE_NAME} ${CAMEL_CASE_NAME}
43+
${CAMEL_CASE_NAME} ${CAMEL_CASE_NAME}
44+
${CAMEL_CASE_NAME} ${CAMEL_CASE_NAME}
45+
${CAMEL_CASE_NAME_WORD_CAMEL_CASE} ${CAMEL_CASE_NAME_WORD_CAMEL_CASE}
4646

4747

4848
*** Test Cases ***
@@ -56,7 +56,7 @@ Assign
5656

5757
Args
5858
Keyword ${VARIABLE}
59-
Keyword ${V A _RI ABLES}
59+
Keyword ${V A _RI_ABLES}
6060
... value with ${_ VARIABLE _}
6161

6262
For header

0 commit comments

Comments
 (0)