Skip to content

Commit 5e01124

Browse files
authored
Ignore comments in formatted Run Keywords (#707)
1 parent d9e0ce9 commit 5e01124

File tree

6 files changed

+111
-9
lines changed

6 files changed

+111
-9
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Ignore comments in IndentNestedKeywords transformer (#702)
2+
----------------------------------------------------------
3+
4+
``IndentNestedKeywords`` moves comments before transformation. This is required in order to properly format different
5+
types of the source code (especially when expanding single line to multiple lines). However as side affect
6+
``IndentNestedKeywords`` moved the comments even if the code didn't require formatting::
7+
8+
*** Test Cases ***
9+
Keyword with commented out single line
10+
Run Keywords
11+
... No Operation
12+
# ... No Operation
13+
... No Operation
14+
15+
In such case the code is already formatted and does not require moving the comments. After this release such
16+
comments will be left alone in a case where the code is already formatted.

robotidy/transformers/IndentNestedKeywords.py

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,34 @@ def parse_keyword_lines(self, lines, tokens, new_line, eol):
9090
tokens.append(eol)
9191
return tokens
9292

93+
@staticmethod
94+
def node_was_transformed(old_tokens, new_tokens) -> bool:
95+
"""
96+
Compare code before and after transformation while ignoring comments to check if code was transformed.
97+
"""
98+
if len(new_tokens) > len(old_tokens):
99+
return True
100+
old_tokens_no_comm = []
101+
data_in_line = False
102+
for token in old_tokens:
103+
if token.type == Token.EOL:
104+
if not data_in_line:
105+
continue
106+
data_in_line = False
107+
elif token.type == Token.COMMENT:
108+
if old_tokens_no_comm and old_tokens_no_comm[-1].type == Token.SEPARATOR:
109+
old_tokens_no_comm.pop()
110+
continue
111+
elif token.type != Token.SEPARATOR:
112+
data_in_line = True
113+
old_tokens_no_comm.append(token)
114+
if len(new_tokens) != len(old_tokens_no_comm):
115+
return True
116+
for new_token, old_token in zip(new_tokens, old_tokens_no_comm):
117+
if new_token.type != old_token.type or new_token.value != old_token.value:
118+
return True
119+
return False
120+
93121
@skip_if_disabled
94122
def visit_SuiteSetup(self, node): # noqa
95123
lines = self.get_setting_lines(node, 0)
@@ -99,8 +127,11 @@ def visit_SuiteSetup(self, node): # noqa
99127
separator = self.get_separator()
100128
new_line = misc.get_new_line()
101129
tokens = [node.data_tokens[0], separator, *misc.join_tokens_with_token(lines[0][1], separator)]
102-
node.tokens = self.parse_keyword_lines(lines, tokens, new_line, eol=node.tokens[-1])
103-
return (*comments, node)
130+
formatted_tokens = self.parse_keyword_lines(lines, tokens, new_line, eol=node.tokens[-1])
131+
if self.node_was_transformed(node.tokens, formatted_tokens):
132+
node.tokens = formatted_tokens
133+
return (*comments, node)
134+
return node
104135

105136
visit_SuiteTeardown = visit_TestSetup = visit_TestTeardown = visit_SuiteSetup
106137

@@ -146,8 +177,11 @@ def visit_KeywordCall(self, node): # noqa
146177
tokens.extend([*misc.join_tokens_with_token(assign, separator), separator])
147178
tokens.extend(misc.join_tokens_with_token(lines[0][1], separator))
148179
new_line = misc.get_new_line(indent)
149-
node.tokens = self.parse_keyword_lines(lines, tokens, new_line, eol=node.tokens[-1])
150-
return (*comments, node)
180+
formatted_tokens = self.parse_keyword_lines(lines, tokens, new_line, eol=node.tokens[-1])
181+
if self.node_was_transformed(node.tokens, formatted_tokens):
182+
node.tokens = formatted_tokens
183+
return (*comments, node)
184+
return node
151185

152186
def split_too_long_lines(self, lines, indent):
153187
"""

robotidy/transformers/OrderSettingsSection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class OrderSettingsSection(Transformer):
1616
- documentation (Documentation, Metadata),
1717
- imports (Library, Resource, Variables),
1818
- settings (Suite Setup and Teardown, Test Setup and Teardown, Test Timeout, Test Template),
19-
- tags (Force Tags, Default Tags)
19+
- tags (Force Tags, Default Tags, Test Tags)
2020
2121
Then ordered by groups (according to ``group_order = documentation,imports,settings,tags`` order). Every
2222
group is separated by ``new_lines_between_groups = 1`` new lines.
@@ -216,8 +216,8 @@ def sort_builtin_libs(statements):
216216
if (
217217
isinstance(statement, LibraryImport)
218218
and statement.name
219-
and statement.name != "Remote"
220219
and statement.name in STDLIBS
220+
and statement.name != "Remote"
221221
):
222222
before.append((comments, statement))
223223
else:

robotidy/utils/misc.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,9 @@ def get_comments(tokens):
348348
if token.value.startswith("#"):
349349
comments.append(token)
350350
elif comments:
351-
comments[-1].value += prev_sep + token.value
351+
comments[-1] = Token(Token.COMMENT, comments[-1].value + prev_sep + token.value)
352352
else:
353-
token.value = f"# {token.value}"
354-
comments.append(token)
353+
comments.append(Token(Token.COMMENT, f"# {token.value}"))
355354
elif token.type == Token.SEPARATOR:
356355
prev_sep = token.value
357356
return comments

tests/atest/transformers/IndentNestedKeywords/expected/comments.robot

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
*** Settings ***
2+
Suite Setup Run Keywords
3+
... No Operation
4+
# ... No Operation
5+
# comment
6+
Suite Teardown Run Keywords
7+
... No Operation
8+
... No Operation
9+
Test Setup Run Keywords
10+
# ... No Operation
11+
... No Operation
12+
13+
114
*** Keywords ***
215
Comments
316
# comment1 comment2
@@ -11,3 +24,18 @@ Comments
1124
... Keyword ${arg}
1225
... ELSE
1326
... Keyword
27+
28+
Golden Keywords
29+
[Test Setup] Run Keywords
30+
... No Operation
31+
# No Operation
32+
... No Operation
33+
34+
Run Keywords
35+
... No Operation
36+
# ... No Operation
37+
... No Operation
38+
39+
Run Keywords
40+
... No Operation
41+
... No Operation # comment about this keyword call

tests/atest/transformers/IndentNestedKeywords/source/comments.robot

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
*** Settings ***
2+
Suite Setup Run Keywords
3+
... No Operation
4+
# ... No Operation
5+
Suite Teardown Run Keywords No Operation No Operation # comment
6+
Test Setup Run Keywords
7+
# ... No Operation
8+
... No Operation
9+
10+
111
*** Keywords ***
212
Comments
313
Run Keyword # comment1 comment2
@@ -6,3 +16,18 @@ Comments
616
... ${arg} # comment 5
717
... ELSE # comment 6
818
... Keyword # comment 7
19+
20+
Golden Keywords
21+
[Test Setup] Run Keywords
22+
... No Operation
23+
# No Operation
24+
... No Operation
25+
26+
Run Keywords
27+
... No Operation
28+
# ... No Operation
29+
... No Operation
30+
31+
Run Keywords
32+
... No Operation
33+
... No Operation # comment about this keyword call

0 commit comments

Comments
 (0)