chore: format QML lint script
This commit is contained in:
parent
f5440eb190
commit
5912d54991
1 changed files with 39 additions and 32 deletions
|
|
@ -56,9 +56,7 @@ RULE_COLOURS = {
|
||||||
}
|
}
|
||||||
|
|
||||||
# Regexes
|
# Regexes
|
||||||
PROPERTY_DECL_RE = re.compile(
|
PROPERTY_DECL_RE = re.compile(r"^(?:required\s+|readonly\s+|default\s+)*property\s")
|
||||||
r"^(?:required\s+|readonly\s+|default\s+)*property\s"
|
|
||||||
)
|
|
||||||
SIGNAL_RE = re.compile(r"^signal\s")
|
SIGNAL_RE = re.compile(r"^signal\s")
|
||||||
FUNCTION_RE = re.compile(r"^function\s")
|
FUNCTION_RE = re.compile(r"^function\s")
|
||||||
ID_RE = re.compile(r"^id\s*:\s*[a-zA-Z_]\w*\s*$")
|
ID_RE = re.compile(r"^id\s*:\s*[a-zA-Z_]\w*\s*$")
|
||||||
|
|
@ -162,12 +160,15 @@ def check_file(filepath: Path) -> list[Violation]:
|
||||||
# Track blank lines per indent
|
# Track blank lines per indent
|
||||||
if not stripped:
|
if not stripped:
|
||||||
# Check: blank line right after opening brace of a QML object
|
# Check: blank line right after opening brace of a QML object
|
||||||
if (i > 0 and func_skip_depth == 0 and not in_block_comment
|
if i > 0 and func_skip_depth == 0 and not in_block_comment and lines[i - 1].strip().endswith("{"):
|
||||||
and lines[i - 1].strip().endswith("{")):
|
violations.append(
|
||||||
violations.append(Violation(
|
Violation(
|
||||||
rel, lineno, "blank-after-open-brace",
|
rel,
|
||||||
"no blank line expected after opening brace",
|
lineno,
|
||||||
))
|
"blank-after-open-brace",
|
||||||
|
"no blank line expected after opening brace",
|
||||||
|
)
|
||||||
|
)
|
||||||
for key in prev_blank:
|
for key in prev_blank:
|
||||||
prev_blank[key] = True
|
prev_blank[key] = True
|
||||||
continue
|
continue
|
||||||
|
|
@ -187,10 +188,14 @@ def check_file(filepath: Path) -> list[Violation]:
|
||||||
if stripped == "}":
|
if stripped == "}":
|
||||||
# Check: blank line right before closing brace
|
# Check: blank line right before closing brace
|
||||||
if i > 0 and not lines[i - 1].strip():
|
if i > 0 and not lines[i - 1].strip():
|
||||||
violations.append(Violation(
|
violations.append(
|
||||||
rel, lineno, "blank-before-close-brace",
|
Violation(
|
||||||
"no blank line expected before closing brace",
|
rel,
|
||||||
))
|
lineno,
|
||||||
|
"blank-before-close-brace",
|
||||||
|
"no blank line expected before closing brace",
|
||||||
|
)
|
||||||
|
)
|
||||||
scopes.pop(indent, None)
|
scopes.pop(indent, None)
|
||||||
prev_blank.pop(indent, None)
|
prev_blank.pop(indent, None)
|
||||||
to_remove = [k for k in scopes if len(k) > len(indent)]
|
to_remove = [k for k in scopes if len(k) > len(indent)]
|
||||||
|
|
@ -213,22 +218,27 @@ def check_file(filepath: Path) -> list[Violation]:
|
||||||
|
|
||||||
# --- Check 1: Section ordering ---
|
# --- Check 1: Section ordering ---
|
||||||
if tracker.last_section is not None and section < tracker.last_section:
|
if tracker.last_section is not None and section < tracker.last_section:
|
||||||
violations.append(Violation(
|
violations.append(
|
||||||
rel, lineno, "section-order",
|
Violation(
|
||||||
f"{SECTION_NAMES[section]} should appear before "
|
rel,
|
||||||
f"{SECTION_NAMES[tracker.last_section]} "
|
lineno,
|
||||||
f"(seen at line {tracker.last_section_line})",
|
"section-order",
|
||||||
))
|
f"{SECTION_NAMES[section]} should appear before "
|
||||||
|
f"{SECTION_NAMES[tracker.last_section]} "
|
||||||
|
f"(seen at line {tracker.last_section_line})",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
# --- Check 2: Missing blank line between different sections ---
|
# --- Check 2: Missing blank line between different sections ---
|
||||||
if (tracker.last_section is not None
|
if tracker.last_section is not None and section != tracker.last_section and not had_blank:
|
||||||
and section != tracker.last_section
|
violations.append(
|
||||||
and not had_blank):
|
Violation(
|
||||||
violations.append(Violation(
|
rel,
|
||||||
rel, lineno, "missing-section-separator",
|
lineno,
|
||||||
f"blank line expected between {SECTION_NAMES[tracker.last_section]} "
|
"missing-section-separator",
|
||||||
f"and {SECTION_NAMES[section]}",
|
f"blank line expected between {SECTION_NAMES[tracker.last_section]} and {SECTION_NAMES[section]}",
|
||||||
))
|
)
|
||||||
|
)
|
||||||
|
|
||||||
# Update tracker
|
# Update tracker
|
||||||
if tracker.last_section is None or section >= tracker.last_section:
|
if tracker.last_section is None or section >= tracker.last_section:
|
||||||
|
|
@ -246,7 +256,7 @@ def check_file(filepath: Path) -> list[Violation]:
|
||||||
# and expression blocks like `color: { ... }`)
|
# and expression blocks like `color: { ... }`)
|
||||||
if brace_count > 0 and section == Section.BINDING:
|
if brace_count > 0 and section == Section.BINDING:
|
||||||
colon_idx = stripped.index(":")
|
colon_idx = stripped.index(":")
|
||||||
after_colon = stripped[colon_idx + 1:].strip()
|
after_colon = stripped[colon_idx + 1 :].strip()
|
||||||
# If content after : doesn't start with an uppercase type name,
|
# If content after : doesn't start with an uppercase type name,
|
||||||
# it's a JS block (not an inline QML object like `contentItem: Rect {`)
|
# it's a JS block (not an inline QML object like `contentItem: Rect {`)
|
||||||
if not re.match(r"^[A-Z]", after_colon):
|
if not re.match(r"^[A-Z]", after_colon):
|
||||||
|
|
@ -263,10 +273,7 @@ def check_file(filepath: Path) -> list[Violation]:
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
qml_files = sorted(
|
qml_files = sorted(p for p in REPO_ROOT.rglob("*.qml") if "build" not in p.parts)
|
||||||
p for p in REPO_ROOT.rglob("*.qml")
|
|
||||||
if "build" not in p.parts
|
|
||||||
)
|
|
||||||
|
|
||||||
print(f"{BOLD}Checking {len(qml_files)} QML files for convention violations...{RESET}\n")
|
print(f"{BOLD}Checking {len(qml_files)} QML files for convention violations...{RESET}\n")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue