tools: run additional linters

This commit is contained in:
Marcin Kurczewski 2024-03-26 16:00:26 +01:00
parent e185465836
commit a5a047f608
12 changed files with 124 additions and 36 deletions

View file

@ -79,5 +79,6 @@ lint-format:
#!/usr/bin/env bash
shopt -s globstar
clang-format -i **/*.{c,h}
tools/additional_lint
lint: (lint-imports) (lint-format)

83
tools/additional_lint Executable file
View file

@ -0,0 +1,83 @@
#!/usr/bin/env python3
import sys
from collections.abc import Callable, Iterable
from dataclasses import dataclass
from pathlib import Path
from shared.common import DATA_DIR, REPO_DIR, SRC_DIR, TOOLS_DIR
EXTENSIONS = [
"Dockerfile",
".h",
".c",
".build",
".ninja",
".py",
".cs",
".xaml",
".csproj",
".txt",
".rc",
".glsl",
".md",
".json",
".json5",
"Dockerfile",
]
@dataclass
class LintWarning:
path: Path
message: str
line: int | None = None
def __str__(self) -> str:
prefix = str(self.path.relative_to(REPO_DIR))
if self.line is not None:
prefix += f":{self.line}"
return f"{prefix}: {self.message}"
def get_files() -> Iterable[Path]:
for root_dir in (DATA_DIR, SRC_DIR, TOOLS_DIR):
for path in root_dir.rglob("**/*"):
if path.is_file() and path.suffix in EXTENSIONS:
yield path
def lint_newlines(path: Path) -> Iterable[LintWarning]:
text = path.read_text()
if text and not text.endswith("\n"):
yield LintWarning(path, "missing newline character at end of file")
def lint_trailing_whitespace(path: Path) -> Iterable[LintWarning]:
for i, line in enumerate(path.open("r"), 1):
if line.rstrip("\n").endswith(" "):
yield LintWarning(path, "trailing whitespace", line=i)
LINTERS: list[Callable[[], Iterable[LintWarning]]] = [
lint_newlines,
lint_trailing_whitespace,
]
def main() -> None:
files = get_files()
exit_code = 0
for file in files:
for linter in LINTERS:
for lint_warning in linter(file):
print(
str(lint_warning),
file=sys.stderr,
)
exit_code = 1
exit(exit_code)
if __name__ == "__main__":
main()

View file

@ -1,4 +1,4 @@
<UserControl
<UserControl
x:Class="TR1X_ConfigTool.Controls.CategoryControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

View file

@ -1,4 +1,4 @@
<Window
<Window
x:Class="TR1X_ConfigTool.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

View file

@ -1,4 +1,4 @@
<UserControl
<UserControl
x:Class="TR1X_ConfigTool.Controls.NumericUpDown"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

View file

@ -1,4 +1,4 @@
<UserControl
<UserControl
x:Class="TR1X_ConfigTool.Controls.PropertyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

View file

@ -1,4 +1,4 @@
using Newtonsoft.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.IO;

View file

@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
namespace TR1X_ConfigTool.Models;

6
tools/shared/common.py Normal file
View file

@ -0,0 +1,6 @@
from pathlib import Path
TOOLS_DIR = Path(__file__).parent.parent
REPO_DIR = TOOLS_DIR.parent
DATA_DIR = REPO_DIR / "src"
SRC_DIR = REPO_DIR / "src"

View file

@ -5,9 +5,7 @@ from pathlib import Path
from shutil import which
from subprocess import run
TOOLS_DIR = Path(__file__).parent
REPO_DIR = TOOLS_DIR.parent
SRC_PATH = REPO_DIR / "src"
from shared.common import SRC_DIR
def fix_imports(path: Path) -> None:
@ -34,7 +32,7 @@ def custom_sort(source: list[str], forced_order: list[str]) -> list[str]:
def sort_imports(path: Path) -> None:
source = path.read_text()
rel_path = path.relative_to(SRC_PATH)
rel_path = path.relative_to(SRC_DIR)
own_include = str(rel_path.with_suffix(".h"))
own_include = {
# files headers of which are not a 1:1 match with their filename
@ -112,8 +110,8 @@ def main() -> None:
if not paths:
paths = sorted(
path
for path in SRC_PATH.glob("**/*.[ch]")
if path != SRC_PATH / "init.c"
for path in SRC_DIR.glob("**/*.[ch]")
if path != SRC_DIR / "init.c"
)
for path in paths: