Skip to content

Commit 7dfbbd1

Browse files
authored
Handle missing default ELSE branch in ReplaceRunKeywordIF (#144)
1 parent ebc546d commit 7dfbbd1

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- Renamed short version of ``--lineseparator`` to ``-ls`` to avoid collision with ``--list\-l``
1818
- Description for disabled transformers can be now displayed & disabled transformers are in ``--list`` output [#114](https://github.com/MarketSquare/robotframework-tidy/issues/114)
1919
- Robotidy should now correctly load configuration files from path when using ``--config`` [#138](https://github.com/MarketSquare/robotframework-tidy/issues/138)
20+
- ReplaceRunKeywordIf will now set variable values to `None` if there is no ELSE branch [#140](https://github.com/MarketSquare/robotframework-tidy/issues/140)
2021
- Transformers should always use the same order. If you need to use custom order, provide --force-order flag [#142](https://github.com/MarketSquare/robotframework-tidy/issues/142)
2122

2223
### Other

docs/source/transformers/ReplaceRunKeywordIf.rst

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,20 @@ Run Keywords inside Run Keyword If will be splitted into separate keywords.
6060
Keyword2
6161
END
6262

63-
Supports global formatting params: ``--spacecount``, ``--startline`` and ``--endline``.
63+
Run Keyword If that assigns values but does not provide default branch will receive ELSE branch with Set Variable:
64+
65+
.. tabs::
66+
67+
.. code-tab:: robotframework Before
68+
69+
${var} Run Keyword If ${condition} Keyword
70+
71+
.. code-tab:: robotframework After
72+
73+
IF ${condition}
74+
${var} Keyword
75+
ELSE
76+
${var} Set Variable ${None}
77+
END
78+
79+
Supports global formatting params: ``--spacecount``, ``--startline`` and ``--endline``.

robotidy/transformers/ReplaceRunKeywordIf.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def create_branched(self, node):
8787
Token(Token.EOL)
8888
])
8989
prev_if = None
90-
for branch in reversed(list(self.split_args_on_delimeters(raw_args, ('ELSE', 'ELSE IF')))):
90+
for branch in reversed(list(self.split_args_on_delimeters(raw_args, ('ELSE', 'ELSE IF'), assign=assign))):
9191
if branch[0].value == 'ELSE':
9292
header = ElseHeader([
9393
separator,
@@ -140,10 +140,13 @@ def args_to_keyword(self, arg_tokens, assign, indent):
140140
return KeywordCall.from_tokens(separated_tokens)
141141

142142
@staticmethod
143-
def split_args_on_delimeters(args, delimeters):
143+
def split_args_on_delimeters(args, delimeters, assign=None):
144144
split_points = [index for index, arg in enumerate(args) if arg.value in delimeters]
145145
prev_index = 0
146146
for split_point in split_points:
147147
yield args[prev_index:split_point]
148148
prev_index = split_point
149149
yield args[prev_index:len(args)]
150+
if assign and 'ELSE' in delimeters and not any(arg.value == 'ELSE' for arg in args):
151+
values = [Token(Token.ARGUMENT, '${None}')] * len(assign)
152+
yield [Token(Token.ELSE), Token(Token.ARGUMENT, 'Set Variable'), *values]

tests/atest/transformers/ReplaceRunKeywordIf/expected/tests.robot

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@ Leave existing if blocks
1111
Single Return Value
1212
IF ${condition}
1313
${var} Keyword With ${var} ${arg}
14+
ELSE
15+
${var} Set Variable ${None}
1416
END
1517

1618
Multiple Return Values
1719
IF ${condition}
1820
${var} ${var2} Keyword With ${var} ${arg}
21+
ELSE
22+
${var} ${var2} Set Variable ${None} ${None}
1923
END
2024

2125
Run keyword if with else if

0 commit comments

Comments
 (0)