Skip to content

Commit a1652d1

Browse files
authored
Fix OrderSettings transformer with inline comment (#223)
This patch fixes a bug with the OrderSettings transformer where if a test case/keyword has a comment on the same line the output would be incorrect, e.g. given this input ```robot Test case # comment1 [Teardown] teardown [Documentation] this is Keyword1 # comment2 Keyword2 # comment3 Keyword3 ``` The expected output is: ```robot Test case # comment1 [Documentation] this is Keyword1 # comment2 Keyword2 # comment3 Keyword3 [Teardown] teardown ``` However before this patch the output would be: ```robot Test case [Documentation] this is # comment1 Keyword1 # comment2 Keyword2 # comment3 Keyword3 [Teardown] teardown ```
1 parent f80010c commit a1652d1

File tree

6 files changed

+70
-2
lines changed

6 files changed

+70
-2
lines changed

robotidy/transformers/OrderSettings.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,11 @@ def order_settings(self, node, setting_types, before, after):
144144
after_seen = False
145145
# when after_seen is set to True then all statements go to trailing_after and last non data
146146
# will be appended after tokens defined in `after` set (like [Return])
147+
comment = []
147148
for child in node.body:
148-
if getattr(child, "type", "invalid") in setting_types:
149+
if isinstance(child, Comment) and child.lineno == node.lineno:
150+
comment.append(child)
151+
elif getattr(child, "type", "invalid") in setting_types:
149152
after_seen = after_seen or child.type in after
150153
settings[child.type] = child
151154
elif after_seen:
@@ -158,7 +161,7 @@ def order_settings(self, node, setting_types, before, after):
158161
trailing_non_data.insert(0, trailing_after.pop())
159162
not_settings += trailing_after
160163
node.body = (
161-
self.add_in_order(before, settings) + not_settings + self.add_in_order(after, settings) + trailing_non_data
164+
comment + self.add_in_order(before, settings) + not_settings + self.add_in_order(after, settings) + trailing_non_data
162165
)
163166
return node
164167

tests/atest/transformers/OrderSettings/expected/custom_order_all_end.robot

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ Test case 4
3232
Keyword3
3333
[Teardown] teardown
3434

35+
Test case 5 # comment1
36+
Keyword1
37+
# comment2
38+
Keyword2
39+
# comment3
40+
Keyword3
41+
[Documentation] this is
42+
[Teardown] teardown
43+
3544
*** Keywords ***
3645
Keyword
3746
Keyword
@@ -79,4 +88,8 @@ Return first and comment last
7988
[Return] stuff
8089
# I want to be here
8190

91+
Comment on the same line # comment
92+
[Documentation] this is
93+
... doc
94+
8295
# what will happen with me?

tests/atest/transformers/OrderSettings/expected/custom_order_default.robot

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ Test case 4
3232
Keyword3
3333
[Teardown] teardown
3434

35+
Test case 5 # comment1
36+
[Documentation] this is
37+
Keyword1
38+
# comment2
39+
Keyword2
40+
# comment3
41+
Keyword3
42+
[Teardown] teardown
43+
3544
*** Keywords ***
3645
Keyword
3746
[Documentation] this is
@@ -79,4 +88,8 @@ Return first and comment last
7988
[Return] stuff
8089
# I want to be here
8190

91+
Comment on the same line # comment
92+
[Documentation] this is
93+
... doc
94+
8295
# what will happen with me?

tests/atest/transformers/OrderSettings/expected/custom_order_without_test_teardown.robot

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ Test case 4
3232
# comment2
3333
Keyword3
3434

35+
Test case 5 # comment1
36+
[Documentation] this is
37+
[Teardown] teardown
38+
Keyword1
39+
# comment2
40+
Keyword2
41+
# comment3
42+
Keyword3
43+
3544
*** Keywords ***
3645
Keyword
3746
[Documentation] this is
@@ -79,4 +88,8 @@ Return first and comment last
7988
[Return] stuff
8089
# I want to be here
8190

91+
Comment on the same line # comment
92+
[Documentation] this is
93+
... doc
94+
8295
# what will happen with me?

tests/atest/transformers/OrderSettings/expected/test.robot

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ Test case 4
3232
Keyword3
3333
[Teardown] teardown
3434

35+
Test case 5 # comment1
36+
[Documentation] this is
37+
Keyword1
38+
# comment2
39+
Keyword2
40+
# comment3
41+
Keyword3
42+
[Teardown] teardown
43+
3544
*** Keywords ***
3645
Keyword
3746
[Documentation] this is
@@ -79,4 +88,8 @@ Return first and comment last
7988
[Return] stuff
8089
# I want to be here
8190

91+
Comment on the same line # comment
92+
[Documentation] this is
93+
... doc
94+
8295
# what will happen with me?

tests/atest/transformers/OrderSettings/source/test.robot

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ Test case 4
3232
# comment2
3333
Keyword3
3434

35+
Test case 5 # comment1
36+
[Teardown] teardown
37+
[Documentation] this is
38+
Keyword1
39+
# comment2
40+
Keyword2
41+
# comment3
42+
Keyword3
43+
3544
*** Keywords ***
3645
Keyword
3746
[Teardown] Keyword
@@ -79,4 +88,8 @@ Return first and comment last
7988
Keyword
8089
# I want to be here
8190

91+
Comment on the same line # comment
92+
[Documentation] this is
93+
... doc
94+
8295
# what will happen with me?

0 commit comments

Comments
 (0)