Skip to content

Commit bf35a64

Browse files
bhirszmnojek
andauthored
Add NormalizeComments transformer for fixing space after # (#500)
* Add NormalizeComments transformer for fixing space after # Co-authored-by: Mateusz Nojek <matnojek@gmail.com>
1 parent b663ce2 commit bf35a64

File tree

8 files changed

+300
-0
lines changed

8 files changed

+300
-0
lines changed

docs/releasenotes/4.0.0.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,33 @@ it will generate::
180180

181181
Read the transformer documentation for more details on configuring your own custom template.
182182

183+
New transformer NormalizeComments (#290)
184+
-----------------------------------------
185+
186+
``NormalizeComments`` handles comments formatting. For now, it only focuses on fixing ``missing-space-after-comment``
187+
rule violations from the Robocop::
188+
189+
*** Settings ***
190+
#linecomment
191+
### header
192+
193+
194+
*** Keywords ***
195+
Keyword
196+
Step #comment
197+
198+
will be transformed to::
199+
200+
*** Settings ***
201+
# linecomment
202+
### header
203+
204+
205+
*** Keywords ***
206+
Keyword
207+
Step # comment
208+
```
209+
183210
Group comments with settings in OrderSettings (#468)
184211
----------------------------------------------------
185212

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
.. _NormalizeComments:
2+
3+
NormalizeComments
4+
================================
5+
6+
Normalize comments.
7+
8+
.. |TRANSFORMERNAME| replace:: NormalizeComments
9+
.. include:: enabled_hint.txt
10+
11+
Normalizes spacing after the beginning of the comment. Fixes ``missing-space-after-comment`` rule violations
12+
from the Robocop.
13+
14+
.. tab-set::
15+
16+
.. tab-item:: Before
17+
18+
.. code:: robotframework
19+
20+
*** Settings ***
21+
#linecomment
22+
### header
23+
24+
25+
*** Keywords ***
26+
Keyword
27+
Step #comment
28+
29+
.. tab-item:: After
30+
31+
.. code:: robotframework
32+
33+
*** Settings ***
34+
# linecomment
35+
### header
36+
37+
38+
*** Keywords ***
39+
Keyword
40+
Step # comment
41+
42+
Skip formatting
43+
----------------
44+
45+
It is possible to use the following arguments to skip formatting of the code:
46+
47+
- :ref:`skip comments`
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from robot.api.parsing import Token
2+
3+
from robotidy.skip import Skip
4+
from robotidy.transformers import Transformer
5+
6+
7+
class NormalizeComments(Transformer):
8+
"""
9+
Normalize comments.
10+
11+
Normalizes spacing after beginning of the comment. Fixes ``missing-space-after-comment`` rule violations
12+
from the Robocop.
13+
14+
Following code:
15+
16+
```robotframework
17+
*** Settings ***
18+
#linecomment
19+
### header
20+
21+
22+
*** Keywords ***
23+
Keyword
24+
Step #comment
25+
```
26+
27+
will be transformed to:
28+
29+
```robotframework
30+
*** Settings ***
31+
# linecomment
32+
### header
33+
34+
35+
*** Keywords ***
36+
Keyword
37+
Step # comment
38+
```
39+
"""
40+
41+
HANDLES_SKIP = frozenset(
42+
{
43+
"skip_comments",
44+
"skip_block_comments",
45+
}
46+
)
47+
48+
def __init__(self, skip: Skip = None):
49+
super().__init__(skip=skip)
50+
51+
def visit_Comment(self, node): # noqa
52+
return self.handle_comments(node)
53+
54+
def visit_Statement(self, node): # noqa
55+
return self.handle_comments(node)
56+
57+
def handle_comments(self, node):
58+
if self.skip.comment(node):
59+
return node
60+
for line in node.lines:
61+
for token in line:
62+
if token.type == Token.COMMENT:
63+
self.fix_comment_spacing(token)
64+
break # ignore other comments in the same line
65+
return node
66+
67+
@staticmethod
68+
def fix_comment_spacing(comment):
69+
# for example content of whole *** Comments *** does not require #
70+
if len(comment.value) == 1 or not comment.value.startswith("#"):
71+
return
72+
if comment.value[1] not in (" ", "#"):
73+
comment.value = f"# {comment.value[1:]}"

robotidy/transformers/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"ReplaceBreakContinue",
6060
"InlineIf",
6161
"Translate",
62+
"NormalizeComments",
6263
]
6364

6465

tests/atest/transformers/NormalizeComments/__init__.py

Whitespace-only changes.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
##comment
2+
# comment
3+
# comment
4+
# comment
5+
# comment
6+
# robotidy: off
7+
# robotidy: on
8+
9+
10+
*** Settings ***
11+
# comment
12+
# comment
13+
# comment
14+
# comment
15+
# robotidy: off
16+
# robotidy: on
17+
Suite Setup Keyword # comment
18+
Suite Teardown Keyword # comment
19+
Default Tags tag
20+
... tag2 # comment
21+
22+
23+
*** Variables ***
24+
# comment
25+
# comment
26+
# comment
27+
# comment
28+
# robotidy: off
29+
# robotidy: on
30+
31+
${VARIABLE} # comment
32+
@{LIST
33+
... element # comment
34+
35+
*** Test Cases ***
36+
# comment
37+
# comment
38+
# comment
39+
# comment
40+
# robotidy: off
41+
# robotidy: on
42+
43+
Test Case
44+
# comment
45+
Step
46+
... arg # comment
47+
${assign} # comment
48+
... ${assign2} # Comment
49+
Keyword
50+
51+
*** Keywords ***
52+
# comment
53+
# comment
54+
# comment
55+
# comment
56+
# robotidy: off
57+
# robotidy: on
58+
59+
Keyword
60+
# comment
61+
Step
62+
... arg # comment
63+
FOR ${var} IN ${LIST} # comment
64+
# comment
65+
Step
66+
... arg # comment
67+
END
68+
TRY # comment
69+
# comment
70+
FINALLY
71+
# comment
72+
END
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
##comment
2+
#comment
3+
# comment
4+
# comment
5+
# comment
6+
# robotidy: off
7+
#robotidy: on
8+
9+
10+
*** Settings ***
11+
#comment
12+
# comment
13+
# comment
14+
# comment
15+
# robotidy: off
16+
#robotidy: on
17+
Suite Setup Keyword # comment
18+
Suite Teardown Keyword #comment
19+
Default Tags tag
20+
... tag2 #comment
21+
22+
23+
*** Variables ***
24+
#comment
25+
# comment
26+
# comment
27+
# comment
28+
# robotidy: off
29+
#robotidy: on
30+
31+
${VARIABLE} #comment
32+
@{LIST
33+
... element #comment
34+
35+
*** Test Cases ***
36+
#comment
37+
# comment
38+
# comment
39+
# comment
40+
# robotidy: off
41+
#robotidy: on
42+
43+
Test Case
44+
#comment
45+
Step
46+
... arg #comment
47+
${assign} #comment
48+
... ${assign2} #Comment
49+
Keyword
50+
51+
*** Keywords ***
52+
#comment
53+
# comment
54+
# comment
55+
# comment
56+
# robotidy: off
57+
#robotidy: on
58+
59+
Keyword
60+
#comment
61+
Step
62+
... arg #comment
63+
FOR ${var} IN ${LIST} #comment
64+
#comment
65+
Step
66+
... arg #comment
67+
END
68+
TRY #comment
69+
#comment
70+
FINALLY
71+
#comment
72+
END
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from .. import TransformerAcceptanceTest
2+
3+
4+
class TestNormalizeComments(TransformerAcceptanceTest):
5+
TRANSFORMER_NAME = "NormalizeComments"
6+
7+
def test_transformer(self):
8+
self.compare(source="test.robot", expected="test.robot")

0 commit comments

Comments
 (0)