|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# lint-qml.sh - Static analysis and runtime type-error check for QML files. |
| 4 | +# |
| 5 | +# Runs qmllint (static) on all QML source files, then launches the preview |
| 6 | +# under xvfb for a few seconds to catch runtime type-assignment warnings. |
| 7 | +# |
| 8 | +# Usage: ./scripts/lint-qml.sh |
| 9 | +# |
| 10 | +# Exit codes: |
| 11 | +# 0 All checks passed |
| 12 | +# 1 qmllint found issues or runtime errors detected |
| 13 | + |
| 14 | +set -euo pipefail |
| 15 | + |
| 16 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 17 | +PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)" |
| 18 | + |
| 19 | +cd "$PROJECT_DIR" |
| 20 | + |
| 21 | +# ── Locate Qt 6 tools ──────────────────────────────────────────── |
| 22 | +# Binary names and paths vary across distros: |
| 23 | +# Arch: qmllint, qml6 (on PATH) |
| 24 | +# Ubuntu: qmllint, qml (in /usr/lib/qt6/bin) |
| 25 | +for candidate in /usr/lib/qt6/bin /usr/lib64/qt6/bin; do |
| 26 | + [[ -d "$candidate" ]] && export PATH="$candidate:$PATH" |
| 27 | +done |
| 28 | + |
| 29 | +find_tool() { |
| 30 | + for name in "$@"; do |
| 31 | + if command -v "$name" &>/dev/null; then |
| 32 | + echo "$name" |
| 33 | + return |
| 34 | + fi |
| 35 | + done |
| 36 | + echo "" |
| 37 | +} |
| 38 | + |
| 39 | +QMLLINT=$(find_tool qmllint qmllint6) |
| 40 | +QML_RUNTIME=$(find_tool qml6 qml) |
| 41 | + |
| 42 | +FAILED=0 |
| 43 | + |
| 44 | +# ── Static analysis with qmllint ────────────────────────────────── |
| 45 | +echo "=== qmllint: static analysis ===" |
| 46 | + |
| 47 | +if [[ -z "$QMLLINT" ]]; then |
| 48 | + echo "SKIP: qmllint not found." |
| 49 | +else |
| 50 | + QML_FILES=$(find . -name '*.qml' -not -path './.git/*') |
| 51 | + echo "Checking: $QML_FILES" |
| 52 | + echo "" |
| 53 | + |
| 54 | + # qmllint emits warnings for SDDM context properties (sddm, config, |
| 55 | + # userModel, etc.) that only exist at runtime. These are expected and |
| 56 | + # unavoidable. We capture the output and only fail on actual errors |
| 57 | + # (syntax errors, unknown components from our own code), not warnings |
| 58 | + # about unqualified/unresolved access to SDDM injected globals. |
| 59 | + LINT_OUTPUT=$($QMLLINT $QML_FILES 2>&1) || true |
| 60 | + if echo "$LINT_OUTPUT" | grep -qiE '^Error:'; then |
| 61 | + echo "$LINT_OUTPUT" |
| 62 | + echo "FAIL: qmllint found errors." |
| 63 | + FAILED=1 |
| 64 | + else |
| 65 | + echo "PASS: qmllint clean (warnings only)." |
| 66 | + fi |
| 67 | +fi |
| 68 | +echo "" |
| 69 | + |
| 70 | +# ── Runtime type-error check ────────────────────────────────────── |
| 71 | +echo "=== Runtime: checking for type errors ===" |
| 72 | + |
| 73 | +if [[ -z "$QML_RUNTIME" ]]; then |
| 74 | + echo "SKIP: qml runtime not found (tried qml6, qml)." |
| 75 | + echo "" |
| 76 | + exit $FAILED |
| 77 | +fi |
| 78 | + |
| 79 | +# Use the Basic style to avoid Breeze/Plasma-specific errors that only |
| 80 | +# occur outside a full Plasma session (e.g. T.Overlay in ComboBox). |
| 81 | +export QT_QUICK_CONTROLS_STYLE=Basic |
| 82 | + |
| 83 | +STDERR_LOG=$(mktemp) |
| 84 | +trap 'rm -f "$STDERR_LOG"' EXIT |
| 85 | + |
| 86 | +# Run preview for 3 seconds under a virtual framebuffer, capture stderr. |
| 87 | +if command -v xvfb-run &>/dev/null; then |
| 88 | + timeout 3 xvfb-run -a "$QML_RUNTIME" preview/Preview.qml 2>"$STDERR_LOG" || true |
| 89 | +elif [ -n "${DISPLAY:-}" ] || [ -n "${WAYLAND_DISPLAY:-}" ]; then |
| 90 | + timeout 3 "$QML_RUNTIME" preview/Preview.qml 2>"$STDERR_LOG" || true |
| 91 | +else |
| 92 | + echo "SKIP: no display and xvfb-run not available." |
| 93 | + echo "" |
| 94 | + exit $FAILED |
| 95 | +fi |
| 96 | + |
| 97 | +# Filter for errors we care about (type assignment, ReferenceError, TypeError). |
| 98 | +if grep -E 'Unable to assign|ReferenceError|TypeError' "$STDERR_LOG" \ |
| 99 | + | grep -v 'Cannot open:' \ |
| 100 | + | grep -q .; then |
| 101 | + echo "FAIL: runtime type errors detected:" |
| 102 | + grep -E 'Unable to assign|ReferenceError|TypeError' "$STDERR_LOG" \ |
| 103 | + | grep -v 'Cannot open:' |
| 104 | + FAILED=1 |
| 105 | +else |
| 106 | + echo "PASS: no runtime type errors." |
| 107 | +fi |
| 108 | +echo "" |
| 109 | + |
| 110 | +exit $FAILED |
0 commit comments