From 57b15b0070478af96c6596ee764268de06a36f90 Mon Sep 17 00:00:00 2001 From: Antoine Beyeler <49431240+abey79@users.noreply.github.com> Date: Tue, 15 Oct 2024 15:46:21 +0200 Subject: [PATCH] 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 --- scripts/lint.py | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/scripts/lint.py b/scripts/lint.py index ce9a83ec8392..980b0f1caa61 100755 --- a/scripts/lint.py +++ b/scripts/lint.py @@ -3,7 +3,8 @@ """ Runs custom linting on our code. -Adding "NOLINT" to any line makes the linter ignore that line. +Adding "NOLINT" to any line makes the linter ignore that line. Adding a pair of "NOLINT_START" and "NOLINT_END" makes +the linter ignore these lines, as well as all lines in between. """ from __future__ import annotations @@ -849,7 +850,7 @@ def fix_enforced_upper_case(s: str) -> str: return "".join(new_words) -def lint_markdown(filepath: str, lines_in: list[str]) -> tuple[list[str], list[str]]: +def lint_markdown(filepath: str, source: SourceFile) -> tuple[list[str], list[str]]: """Only for .md files.""" errors = [] @@ -863,12 +864,12 @@ def lint_markdown(filepath: str, lines_in: list[str]) -> tuple[list[str], list[s in_code_of_conduct = filepath.endswith("CODE_OF_CONDUCT.md") if in_code_of_conduct: - return errors, lines_in + return errors, source.lines in_code_block = False in_frontmatter = False in_metadata = False - for line_nr, line in enumerate(lines_in): + for line_nr, line in enumerate(source.lines): line_nr = line_nr + 1 if line.strip().startswith("```"): @@ -881,7 +882,7 @@ def lint_markdown(filepath: str, lines_in: list[str]) -> tuple[list[str], list[s if in_metadata and line.startswith("-->"): in_metadata = False - if not in_code_block: + if not in_code_block and not source.should_ignore(line_nr): if not in_metadata: # Check the casing on markdown headers if m := re.match(r"(\#+ )(.*)", line): @@ -973,7 +974,19 @@ def _update_content(self) -> None: self.content = "".join(self.lines) # gather lines with a `NOLINT` marker - self.no_lints = {i for i, line in enumerate(self.lines) if "NOLINT" in line} + self.nolints = set() + is_in_nolint_block = False + for i, line in enumerate(self.lines): + if "NOLINT" in line: + self.nolints.add(i) + + if "NOLINT_START" in line: + is_in_nolint_block = True + + if is_in_nolint_block: + self.nolints.add(i) + if "NOLINT_END" in line: + is_in_nolint_block = False def rewrite(self, new_lines: list[str]) -> None: """Rewrite the contents of the file.""" @@ -993,7 +1006,7 @@ def should_ignore(self, from_line: int, to_line: int | None = None) -> bool: if to_line is None: to_line = from_line - return any(i in self.no_lints for i in range(from_line - 1, to_line + 1)) + return any(i in self.nolints for i in range(from_line - 1, to_line + 1)) def should_ignore_index(self, start_idx: int, end_idx: int | None = None) -> bool: """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: prev_line = None for line_nr, line in enumerate(source.lines): + if source.should_ignore(line_nr): + continue + if line == "" or line[-1] != "\n": error = "Missing newline at end of file" else: @@ -1049,7 +1065,7 @@ def lint_file(filepath: str, args: Any) -> int: source.rewrite(lines_out) if filepath.endswith(".md"): - errors, lines_out = lint_markdown(filepath, source.lines) + errors, lines_out = lint_markdown(filepath, source) for error in errors: print(source.error(error))