Files
leleka/scripts/limits/model.py
T
Pavlo Ratushnyi 334af80d3d STRA-367: limits gate for lawbook 301 (function 30 lines, complexity 10, nesting 4, width 120)
New and changed code is checked against the base (merge-base with the integration branch):
every added or touched function and every added or changed line must be within the limits,
unless limits-allowlist.json lists the function with a reason and an owner; an allowlisted
function may only shrink. The legacy that exists today is a per-crate census in
limits-baseline.json whose totals may only fall (same --update-baseline semantics as the
architecture gate). Rust is measured from the rustsplit lexer, the gate's own Python from ast.
56 self-tests, including a temporary git repository.
2026-11-06 11:26:53 +01:00

45 lines
1.4 KiB
Python

"""What is measured, and the dispatch from a file to its language scanner."""
from dataclasses import dataclass
KINDS = ("fn_lines", "complexity", "nesting", "width")
@dataclass(frozen=True)
class Fn:
"""One function: its qualified key, its span (1-based, inclusive) and its four measures."""
key: str
start: int
end: int
fn_lines: int
complexity: int
nesting: int
width: int # lines wider than the limit inside the span
test: bool
def measures(self):
return {kind: getattr(self, kind) for kind in KINDS}
def line_width(line, tab_width):
"""Columns of a line: a tab counts `tab_width`, the line ending does not count."""
return len(line.rstrip("\r\n").replace("\t", " " * tab_width))
def wide_lines(text, limit, tab_width):
"""1-based numbers of the lines wider than `limit` columns."""
return {n for n, line in enumerate(text.splitlines(), 1) if line_width(line, tab_width) > limit}
def allowed(kind, limits):
"""What a measure may be without an exemption: the limit, except `width`, which counts wide lines (none allowed)."""
return 0 if kind == "width" else limits[kind]
def over(measures, limits):
"""{kind: value} for the measures above what is allowed."""
return {kind: value for kind, value in measures.items() if value > allowed(kind, limits)}
def count_wide(wide, start, end):
return sum(1 for n in wide if start <= n <= end)