Repository: MarketSquare/robotframework-robocop
Pull request #1340 (2025-03-23 13:45:40)

Fix comments section header removed when merging it
Fixes https://github.com/MarketSquare/robotframework-tidy/issues/738


Patch:
diff --git a/docs/releasenotes/6.0.0a3.rst b/docs/releasenotes/6.0.0a3.rst
index 49a14b1..5efe544 100644
--- a/docs/releasenotes/6.0.0a3.rst
+++ b/docs/releasenotes/6.0.0a3.rst
@@ -803,8 +803,6 @@ Integrations with other platforms
 Robocop could be already integrated with some CI/CD platforms. We made an active effort of documenting existing
 integrations and implementing it for missing, popular platforms such as Gitlab or Sonar Qube.
 
-Documentation is available at X # TODO .
-
 We are now supporting following integrations:
 
 - Gitlab Code Quality (with ``gitlab`` report) (#1116)
@@ -812,6 +810,9 @@ We are now supporting following integrations:
 - pre-commit
 - ... and more - see the `Integrations` documentation
 
+Note that due to changes to how Robocop is used, previous ``pre-commit`` configuration will not work. For Robocop 6.0+
+use configuration proposed in the documentation.
+
 Select all rules with ALL (#1316)
 ---------------------------------
 
@@ -1093,3 +1094,9 @@ RenameVariables capitalized arguments in [Template]
         [Template]    My Keyword ${embedded_arg}
         value1
         value2
+
+Comments section header is preserved when merging multiple sections
+-------------------------------------------------------------------
+
+When merging duplicated sections, formatter uses header of the first encountered header. In case of comments section
+it led to removing header from sections that had it. It should be fixed now.
diff --git a/docs/source/integrations.rst b/docs/source/integrations.rst
index 3fe09a1..52a2e99 100644
--- a/docs/source/integrations.rst
+++ b/docs/source/integrations.rst
@@ -29,6 +29,9 @@ To use Robocop as `pre-commit <https://pre-commit.com/>`_ hook, you need followi
 It will run both linter and formatter on your modified files when trying to commit changes. If any linter issue is
 found or file is modified, it will stop the commit.
 
+``rev`` is version of the Robocop, prefixed with ``v``. It matches the release tag created in our repository on each
+release.
+
 .. _github_integration:
 
 GitHub
diff --git a/src/robocop/formatter/formatters/MergeAndOrderSections.py b/src/robocop/formatter/formatters/MergeAndOrderSections.py
index bf9579b..66c418f 100644
--- a/src/robocop/formatter/formatters/MergeAndOrderSections.py
+++ b/src/robocop/formatter/formatters/MergeAndOrderSections.py
@@ -133,6 +133,10 @@ class MergeAndOrderSections(Formatter):
                         f"{node.source}: Merged duplicated section has section header comments. "
                         "Only header comments from first section header of the same type are preserved."
                     )
+                if not sections[section_type].header:
+                    # when merging 2+ comments sections, all but first are required to have header
+                    # so if there is more than one comments section, always add header to original
+                    sections[section_type].header = SectionHeader.from_params(section_type, "*** Comments ***")
                 sections[section_type].body += section.body
         node.sections = [sections[order] for order in self.sections_order if order in sections]
         return node


Test patch:
diff --git a/tests/formatter/formatters/MergeAndOrderSections/expected/comments_section.robot b/tests/formatter/formatters/MergeAndOrderSections/expected/comments_section.robot
new file mode 100644
index 0000000..a74b18c
--- /dev/null
+++ b/tests/formatter/formatters/MergeAndOrderSections/expected/comments_section.robot
@@ -0,0 +1,10 @@
+*** Comments ***
+
+comment
+
+I am a comment
+*** Keywords ***
+Keyword
+    Keyword Call
+
+
diff --git a/tests/formatter/formatters/MergeAndOrderSections/source/comments_section.robot b/tests/formatter/formatters/MergeAndOrderSections/source/comments_section.robot
new file mode 100644
index 0000000..7cb93ce
--- /dev/null
+++ b/tests/formatter/formatters/MergeAndOrderSections/source/comments_section.robot
@@ -0,0 +1,10 @@
+
+comment
+
+*** Keywords ***
+Keyword
+    Keyword Call
+
+
+*** Comments ***
+I am a comment
diff --git a/tests/formatter/formatters/MergeAndOrderSections/test_formatter.py b/tests/formatter/formatters/MergeAndOrderSections/test_formatter.py
index 7c35a1f..0340f70 100644
--- a/tests/formatter/formatters/MergeAndOrderSections/test_formatter.py
+++ b/tests/formatter/formatters/MergeAndOrderSections/test_formatter.py
@@ -84,3 +84,6 @@ class TestMergeAndOrderSections(FormatterAcceptanceTest):
 
     def test_invalid(self):
         self.compare(source="invalid.robot", not_modified=True, test_on_version=">=6.1")
+
+    def test_do_not_remove_comments_header(self):
+        self.compare(source="comments_section.robot", configure=["MergeAndOrderSections.create_comment_section=False"])