Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Acknowledge file disablers in the whole comments section instead of only first line #666

Merged
merged 2 commits into from
Mar 11, 2024
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
21 changes: 21 additions & 0 deletions docs/releasenotes/unreleased/other.1.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
File disablers in Comments section (#587)
-----------------------------------------

Previously file formatting disablers were only recognized if they were placed in the first line of file::

# robotidy: off
*** Settings ***

Now Robotidy will acknowledge any disabler in the first comment section (with or without header)::

Following line disables formatting of this file with Robotidy
# robotidy: off

*** Settings ***

Or::

*** Comments ***
# robotidy: off

*** Test Cases ***
12 changes: 9 additions & 3 deletions robotidy/disablers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import functools
import re

from robot.api.parsing import Comment, ModelVisitor, Token
from robot.api.parsing import Comment, CommentSection, ModelVisitor, Token


def skip_if_disabled(func):
Expand Down Expand Up @@ -120,6 +120,7 @@ def __init__(self, start_line, end_line):
self.disabler_pattern = re.compile(r"\s*#\s?robotidy:\s?(?P<disabler>on|off)")
self.stack = []
self.file_disabled = False
self.file_level_disablers = False

def any_disabler_open(self):
return any(disabler for disabler in self.stack)
Expand All @@ -132,15 +133,20 @@ def get_disabler(self, comment):
def close_disabler(self, end_line):
disabler = self.stack.pop()
if disabler:
if self.file_level_disablers:
self.file_disabled = True
self.disablers.add_disabler(disabler, end_line)

def visit_File(self, node): # noqa
self.file_level_disablers = False
self.disablers = DisabledLines(self.start_line, self.end_line, node.end_lineno)
self.disablers.parse_global_disablers()
self.stack = []
self.generic_visit(node)
for index, section in enumerate(node.sections):
self.file_level_disablers = index == 0 and isinstance(section, CommentSection)
self.visit_Section(section)
self.disablers.sort_disablers()
self.file_disabled = self.disablers.file_disabled
self.file_disabled = self.file_disabled or self.disablers.file_disabled

def visit_SectionHeader(self, node): # noqa
for comment in node.get_tokens(Token.COMMENT):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# comment
# comment
# robotidy: off
# robotidy: on
# robotidy: on


*** Settings ***
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# comment
# comment
# robotidy: off
#robotidy: on
# robotidy: on


*** Settings ***
Expand Down
3 changes: 3 additions & 0 deletions tests/utest/test_disablers.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ def test_is_node_disabled(check_start, check_end, start_line, end_line, lines, f
"test_file, expected_lines, file_disabled, rf_version",
[
("file_disabled.robot", [(1, 1), (14, 15)], True, 4),
("file_disabled_in_comments.robot", [(2, 4), (17, 18)], True, 4),
("file_disabled_in_comments_no_header.robot", [(2, 4), (17, 18)], True, 4),
("file_disabled_enabled_in_comments.robot", [(2, 6), (21, 22)], False, 4),
("file_disabled_and_enabled.robot", [(1, 2), (15, 16)], False, 4),
("test.robot", [(13, 14), (25, 37), (30, 33), (40, 41), (46, 48), (57, 58), (67, 67)], False, 5),
("open_disabler_in_section.robot", [(5, 8), (13, 15), (20, 23)], False, 4),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
*** Comments ***
# robotidy: off

Some comments that should not be formatted

# robotidy: on


*** Settings ***
Library SeleniumLibrary

Metadata Key Value

*** Test Cases ***
Test
[Documentation] This is doc
Step
FOR ${var} IN RANGE 10
IF $condition
WHILE ${arg}
${return} Keyword ${value}
... ${other_value} # robotidy: off
END
ELSE IF
Step ${arg}
... value
Step 2
# comment
END
END
26 changes: 26 additions & 0 deletions tests/utest/testdata/disablers/file_disabled_in_comments.robot
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
*** Comments ***
# robotidy: off


*** Settings ***
Library SeleniumLibrary

Metadata Key Value

*** Test Cases ***
Test
[Documentation] This is doc
Step
FOR ${var} IN RANGE 10
IF $condition
WHILE ${arg}
${return} Keyword ${value}
... ${other_value} # robotidy: off
END
ELSE IF
Step ${arg}
... value
Step 2
# comment
END
END
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

# robotidy: off


*** Settings ***
Library SeleniumLibrary

Metadata Key Value

*** Test Cases ***
Test
[Documentation] This is doc
Step
FOR ${var} IN RANGE 10
IF $condition
WHILE ${arg}
${return} Keyword ${value}
... ${other_value} # robotidy: off
END
ELSE IF
Step ${arg}
... value
Step 2
# comment
END
END
Loading