Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions docs/releasenotes/4.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,33 @@ it will generate::

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

New transformer NormalizeComments (#290)
-----------------------------------------

``NormalizeComments`` handles comments formatting. For now, it only focuses on fixing ``missing-space-after-comment``
rule violations from the Robocop::

*** Settings ***
#linecomment
### header


*** Keywords ***
Keyword
Step #comment

will be transformed to::

*** Settings ***
# linecomment
### header


*** Keywords ***
Keyword
Step # comment
```

Group comments with settings in OrderSettings (#468)
----------------------------------------------------

Expand Down
47 changes: 47 additions & 0 deletions docs/source/transformers/NormalizeComments.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
.. _NormalizeComments:

NormalizeComments
================================

Normalize comments.

.. |TRANSFORMERNAME| replace:: NormalizeComments
.. include:: enabled_hint.txt

Normalizes spacing after the beginning of the comment. Fixes ``missing-space-after-comment`` rule violations
from the Robocop.

.. tab-set::

.. tab-item:: Before

.. code:: robotframework

*** Settings ***
#linecomment
### header


*** Keywords ***
Keyword
Step #comment

.. tab-item:: After

.. code:: robotframework

*** Settings ***
# linecomment
### header


*** Keywords ***
Keyword
Step # comment

Skip formatting
----------------

It is possible to use the following arguments to skip formatting of the code:

- :ref:`skip comments`
73 changes: 73 additions & 0 deletions robotidy/transformers/NormalizeComments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from robot.api.parsing import Token

from robotidy.skip import Skip
from robotidy.transformers import Transformer


class NormalizeComments(Transformer):
"""
Normalize comments.

Normalizes spacing after beginning of the comment. Fixes ``missing-space-after-comment`` rule violations
from the Robocop.

Following code:

```robotframework
*** Settings ***
#linecomment
### header


*** Keywords ***
Keyword
Step #comment
```

will be transformed to:

```robotframework
*** Settings ***
# linecomment
### header


*** Keywords ***
Keyword
Step # comment
```
"""

HANDLES_SKIP = frozenset(
{
"skip_comments",
"skip_block_comments",
}
)

def __init__(self, skip: Skip = None):
super().__init__(skip=skip)

def visit_Comment(self, node): # noqa
return self.handle_comments(node)

def visit_Statement(self, node): # noqa
return self.handle_comments(node)

def handle_comments(self, node):
if self.skip.comment(node):
return node
for line in node.lines:
for token in line:
if token.type == Token.COMMENT:
self.fix_comment_spacing(token)
break # ignore other comments in the same line
return node

@staticmethod
def fix_comment_spacing(comment):
# for example content of whole *** Comments *** does not require #
if len(comment.value) == 1 or not comment.value.startswith("#"):
return
if comment.value[1] not in (" ", "#"):
comment.value = f"# {comment.value[1:]}"
1 change: 1 addition & 0 deletions robotidy/transformers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"ReplaceBreakContinue",
"InlineIf",
"Translate",
"NormalizeComments",
]


Expand Down
Empty file.
72 changes: 72 additions & 0 deletions tests/atest/transformers/NormalizeComments/expected/test.robot
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
##comment
# comment
# comment
# comment
# comment
# robotidy: off
# robotidy: on


*** Settings ***
# comment
# comment
# comment
# comment
# robotidy: off
# robotidy: on
Suite Setup Keyword # comment
Suite Teardown Keyword # comment
Default Tags tag
... tag2 # comment


*** Variables ***
# comment
# comment
# comment
# comment
# robotidy: off
# robotidy: on

${VARIABLE} # comment
@{LIST
... element # comment

*** Test Cases ***
# comment
# comment
# comment
# comment
# robotidy: off
# robotidy: on

Test Case
# comment
Step
... arg # comment
${assign} # comment
... ${assign2} # Comment
Keyword

*** Keywords ***
# comment
# comment
# comment
# comment
# robotidy: off
# robotidy: on

Keyword
# comment
Step
... arg # comment
FOR ${var} IN ${LIST} # comment
# comment
Step
... arg # comment
END
TRY # comment
# comment
FINALLY
# comment
END
72 changes: 72 additions & 0 deletions tests/atest/transformers/NormalizeComments/source/test.robot
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
##comment
#comment
# comment
# comment
# comment
# robotidy: off
#robotidy: on


*** Settings ***
#comment
# comment
# comment
# comment
# robotidy: off
#robotidy: on
Suite Setup Keyword # comment
Suite Teardown Keyword #comment
Default Tags tag
... tag2 #comment


*** Variables ***
#comment
# comment
# comment
# comment
# robotidy: off
#robotidy: on

${VARIABLE} #comment
@{LIST
... element #comment

*** Test Cases ***
#comment
# comment
# comment
# comment
# robotidy: off
#robotidy: on

Test Case
#comment
Step
... arg #comment
${assign} #comment
... ${assign2} #Comment
Keyword

*** Keywords ***
#comment
# comment
# comment
# comment
# robotidy: off
#robotidy: on

Keyword
#comment
Step
... arg #comment
FOR ${var} IN ${LIST} #comment
#comment
Step
... arg #comment
END
TRY #comment
#comment
FINALLY
#comment
END
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from .. import TransformerAcceptanceTest


class TestNormalizeComments(TransformerAcceptanceTest):
TRANSFORMER_NAME = "NormalizeComments"

def test_transformer(self):
self.compare(source="test.robot", expected="test.robot")