Skip to content

Commit 57b15b0

Browse files
abey79Wumpf
andauthored
Add NOLINT block to lint.py (#7720)
### What Add `NOLINT_START`/`NOLINT_END` markers to `lint.py`, to exclude entire blocks. Useful eg. for documentation, where output of commands is shown verbatim. TODO: - [x] support NOLINT, etc. for markdown docs too ### Checklist * [x] I have read and agree to [Contributor Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and the [Code of Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md) * [x] I've included a screenshot or gif (if applicable) * [x] I have tested the web demo (if applicable): * Using examples from latest `main` build: [rerun.io/viewer](https://rerun.io/viewer/pr/7720?manifest_url=https://app.rerun.io/version/main/examples_manifest.json) * Using full set of examples from `nightly` build: [rerun.io/viewer](https://rerun.io/viewer/pr/7720?manifest_url=https://app.rerun.io/version/nightly/examples_manifest.json) * [x] The PR title and labels are set such as to maximize their usefulness for the next release's CHANGELOG * [x] If applicable, add a new check to the [release checklist](https://github.com/rerun-io/rerun/blob/main/tests/python/release_checklist)! * [x] If have noted any breaking changes to the log API in `CHANGELOG.md` and the migration guide - [PR Build Summary](https://build.rerun.io/pr/7720) - [Recent benchmark results](https://build.rerun.io/graphs/crates.html) - [Wasm size tracking](https://build.rerun.io/graphs/sizes.html) To run all checks from `main`, comment on the PR with `@rerun-bot full-check`. --------- Co-authored-by: Andreas Reich <[email protected]>
1 parent 381dee7 commit 57b15b0

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

scripts/lint.py

+24-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"""
44
Runs custom linting on our code.
55
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.
78
"""
89

910
from __future__ import annotations
@@ -849,7 +850,7 @@ def fix_enforced_upper_case(s: str) -> str:
849850
return "".join(new_words)
850851

851852

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]]:
853854
"""Only for .md files."""
854855

855856
errors = []
@@ -863,12 +864,12 @@ def lint_markdown(filepath: str, lines_in: list[str]) -> tuple[list[str], list[s
863864
in_code_of_conduct = filepath.endswith("CODE_OF_CONDUCT.md")
864865

865866
if in_code_of_conduct:
866-
return errors, lines_in
867+
return errors, source.lines
867868

868869
in_code_block = False
869870
in_frontmatter = False
870871
in_metadata = False
871-
for line_nr, line in enumerate(lines_in):
872+
for line_nr, line in enumerate(source.lines):
872873
line_nr = line_nr + 1
873874

874875
if line.strip().startswith("```"):
@@ -881,7 +882,7 @@ def lint_markdown(filepath: str, lines_in: list[str]) -> tuple[list[str], list[s
881882
if in_metadata and line.startswith("-->"):
882883
in_metadata = False
883884

884-
if not in_code_block:
885+
if not in_code_block and not source.should_ignore(line_nr):
885886
if not in_metadata:
886887
# Check the casing on markdown headers
887888
if m := re.match(r"(\#+ )(.*)", line):
@@ -973,7 +974,19 @@ def _update_content(self) -> None:
973974
self.content = "".join(self.lines)
974975

975976
# 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
977990

978991
def rewrite(self, new_lines: list[str]) -> None:
979992
"""Rewrite the contents of the file."""
@@ -993,7 +1006,7 @@ def should_ignore(self, from_line: int, to_line: int | None = None) -> bool:
9931006

9941007
if to_line is None:
9951008
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))
9971010

9981011
def should_ignore_index(self, start_idx: int, end_idx: int | None = None) -> bool:
9991012
"""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:
10221035

10231036
prev_line = None
10241037
for line_nr, line in enumerate(source.lines):
1038+
if source.should_ignore(line_nr):
1039+
continue
1040+
10251041
if line == "" or line[-1] != "\n":
10261042
error = "Missing newline at end of file"
10271043
else:
@@ -1049,7 +1065,7 @@ def lint_file(filepath: str, args: Any) -> int:
10491065
source.rewrite(lines_out)
10501066

10511067
if filepath.endswith(".md"):
1052-
errors, lines_out = lint_markdown(filepath, source.lines)
1068+
errors, lines_out = lint_markdown(filepath, source)
10531069

10541070
for error in errors:
10551071
print(source.error(error))

0 commit comments

Comments
 (0)