3
3
"""
4
4
Runs custom linting on our code.
5
5
6
- Adding "NOLINT" to any line makes the linter ignore that line.
6
+ Adding "NOLINT" to any line makes the linter ignore that line. Adding a pair of "NOLINT_START" and "NOLINT_END" makes
7
+ the linter ignore these lines, as well as all lines in between.
7
8
"""
8
9
9
10
from __future__ import annotations
@@ -849,7 +850,7 @@ def fix_enforced_upper_case(s: str) -> str:
849
850
return "" .join (new_words )
850
851
851
852
852
- def lint_markdown (filepath : str , lines_in : list [ str ] ) -> tuple [list [str ], list [str ]]:
853
+ def lint_markdown (filepath : str , source : SourceFile ) -> tuple [list [str ], list [str ]]:
853
854
"""Only for .md files."""
854
855
855
856
errors = []
@@ -863,12 +864,12 @@ def lint_markdown(filepath: str, lines_in: list[str]) -> tuple[list[str], list[s
863
864
in_code_of_conduct = filepath .endswith ("CODE_OF_CONDUCT.md" )
864
865
865
866
if in_code_of_conduct :
866
- return errors , lines_in
867
+ return errors , source . lines
867
868
868
869
in_code_block = False
869
870
in_frontmatter = False
870
871
in_metadata = False
871
- for line_nr , line in enumerate (lines_in ):
872
+ for line_nr , line in enumerate (source . lines ):
872
873
line_nr = line_nr + 1
873
874
874
875
if line .strip ().startswith ("```" ):
@@ -881,7 +882,7 @@ def lint_markdown(filepath: str, lines_in: list[str]) -> tuple[list[str], list[s
881
882
if in_metadata and line .startswith ("-->" ):
882
883
in_metadata = False
883
884
884
- if not in_code_block :
885
+ if not in_code_block and not source . should_ignore ( line_nr ) :
885
886
if not in_metadata :
886
887
# Check the casing on markdown headers
887
888
if m := re .match (r"(\#+ )(.*)" , line ):
@@ -973,7 +974,19 @@ def _update_content(self) -> None:
973
974
self .content = "" .join (self .lines )
974
975
975
976
# gather lines with a `NOLINT` marker
976
- self .no_lints = {i for i , line in enumerate (self .lines ) if "NOLINT" in line }
977
+ self .nolints = set ()
978
+ is_in_nolint_block = False
979
+ for i , line in enumerate (self .lines ):
980
+ if "NOLINT" in line :
981
+ self .nolints .add (i )
982
+
983
+ if "NOLINT_START" in line :
984
+ is_in_nolint_block = True
985
+
986
+ if is_in_nolint_block :
987
+ self .nolints .add (i )
988
+ if "NOLINT_END" in line :
989
+ is_in_nolint_block = False
977
990
978
991
def rewrite (self , new_lines : list [str ]) -> None :
979
992
"""Rewrite the contents of the file."""
@@ -993,7 +1006,7 @@ def should_ignore(self, from_line: int, to_line: int | None = None) -> bool:
993
1006
994
1007
if to_line is None :
995
1008
to_line = from_line
996
- return any (i in self .no_lints for i in range (from_line - 1 , to_line + 1 ))
1009
+ return any (i in self .nolints for i in range (from_line - 1 , to_line + 1 ))
997
1010
998
1011
def should_ignore_index (self , start_idx : int , end_idx : int | None = None ) -> bool :
999
1012
"""Same as `should_ignore` but takes 0-based indices instead of line numbers."""
@@ -1022,6 +1035,9 @@ def lint_file(filepath: str, args: Any) -> int:
1022
1035
1023
1036
prev_line = None
1024
1037
for line_nr , line in enumerate (source .lines ):
1038
+ if source .should_ignore (line_nr ):
1039
+ continue
1040
+
1025
1041
if line == "" or line [- 1 ] != "\n " :
1026
1042
error = "Missing newline at end of file"
1027
1043
else :
@@ -1049,7 +1065,7 @@ def lint_file(filepath: str, args: Any) -> int:
1049
1065
source .rewrite (lines_out )
1050
1066
1051
1067
if filepath .endswith (".md" ):
1052
- errors , lines_out = lint_markdown (filepath , source . lines )
1068
+ errors , lines_out = lint_markdown (filepath , source )
1053
1069
1054
1070
for error in errors :
1055
1071
print (source .error (error ))
0 commit comments