///|
/// Apply declarative line rules to successfully read source content.
///
/// Each matcher is compiled once per rule (see `MatchKind::prepare`), then
/// reused for every line — compiling per line dominates scan time otherwise.
///
/// Three views of the file are passed in: `code_lines` is the code view
/// (comments blanked, and literal contents blanked for the JS/TS family — see
/// `comment.mbt`), `literal_lines` is the comment-stripped view with literal
/// text intact, and `raw_lines` is the file exactly as written. A rule matches
/// against `code_lines` by default; `match_literals` selects `literal_lines`
/// for rules whose target only exists as a literal value, and `match_comments`
/// selects `raw_lines` — the opt-in a comment-targeting rule needs.
pub fn lint_lines(
  rel : String,
  code_lines : Array[(Int32, String)],
  literal_lines : Array[(Int32, String)],
  raw_lines : Array[(Int32, String)],
  rules : Array[Rule],
) -> Array[Finding] {
  let findings : Array[Finding] = []
  for rule in rules {
    if !rule_applies(rule, rel) {
      continue
    }
    let matcher = rule.match_.prepare()
    let unless = match rule.unless {
      None => None
      Some(unless_) => Some(unless_.prepare())
    }
    let view = if rule.match_comments {
      raw_lines
    } else if rule.match_literals {
      literal_lines
    } else {
      code_lines
    }
    for (lineno, text) in view {
      if matcher.matches(text) {
        let skip = match unless {
          None => false
          Some(prepared) => prepared.matches(text)
        }
        if !skip {
          findings.push({
            rule_id: rule.id,
            severity: rule.severity,
            message: rule.message,
            file: rel,
            line: lineno,
            column: rule.match_.column_of(text),
            snippet: text.trim().to_owned(),
          })
        }
      }
    }
  }
  findings
}

///|
fn rule_applies(rule : Rule, rel : String) -> Bool {
  has_wanted_ext(
    rel,
    Set::from_iter(rule.extensions.iter().map(e => e.to_lower())),
  ) &&
  rule.scope.allows(rel)
}

///|
/// Select and lint a directory tree under the supplied rules and global scope.
/// Selection failures return Err; source/context read failures stay in diagnostics.
pub fn lint_tree(
  root : String,
  rules : Array[Rule],
  file_rules : Array[FileRule],
  structural_rules : Array[StructuralRule],
  exts : Array[String],
  scope : Scope,
) -> Result[LintResult, Error] {
  lint_tree_with(root, rules, file_rules, structural_rules, exts, scope, [])
}

///|
/// Alias-aware variant of `lint_tree`. `aliases` are `(prefix, target)` pairs
/// used to resolve path-aliased imports during the structural pass.
pub fn lint_tree_with(
  root : String,
  rules : Array[Rule],
  file_rules : Array[FileRule],
  structural_rules : Array[StructuralRule],
  exts : Array[String],
  scope : Scope,
  aliases : Array[(String, String)],
) -> Result[LintResult, Error] {
  try {
    let absolute = canonical_root(root)
    let files = select_files(Tree, absolute, exts)
    Ok(
      lint_selected_with(
        absolute,
        Tree,
        files,
        rules,
        file_rules,
        structural_rules,
        scope,
        aliases,
      ),
    )
  } catch {
    error => Err(error)
  }
}

///|
/// Lint explicit working-tree paths. Paths are validated and deduplicated;
/// unreadable/missing files produce diagnostics instead of successful counters.
pub fn lint_files(
  root : String,
  files : Array[String],
  rules : Array[Rule],
  file_rules : Array[FileRule],
  structural_rules : Array[StructuralRule],
  scope : Scope,
) -> LintResult raise {
  lint_files_with(root, files, rules, file_rules, structural_rules, scope, [])
}

///|
/// Alias-aware variant of `lint_files`.
pub fn lint_files_with(
  root : String,
  files : Array[String],
  rules : Array[Rule],
  file_rules : Array[FileRule],
  structural_rules : Array[StructuralRule],
  scope : Scope,
  aliases : Array[(String, String)],
) -> LintResult raise {
  lint_selected_with(
    canonical_root(root),
    Tree,
    files,
    rules,
    file_rules,
    structural_rules,
    scope,
    aliases,
  )
}

///|
/// `Changed` uses committed HEAD blobs; `Staged` uses actual index blobs.
/// Other modes read regular working-tree files without following links.
///
/// The global `scope` is applied to the candidate list first: candidates it
/// rejects are counted in `files_out_of_scope` and never read. Rule-level
/// scope then decides which rules see each selected file.
///
/// The scan runs in two stages: the per-file stage applies line rules and
/// file rules, then — when structural rules are configured — the sources of
/// every scanned file are parsed and analysed as one project, which is what
/// cross-file checks (unused imports, import cycles, dead exports) need.
pub fn lint_selected(
  root : String,
  mode : ScanMode,
  files : Array[String],
  rules : Array[Rule],
  file_rules : Array[FileRule],
  structural_rules : Array[StructuralRule],
  scope : Scope,
) -> LintResult raise {
  lint_selected_with(
    root,
    mode,
    files,
    rules,
    file_rules,
    structural_rules,
    scope,
    [],
  )
}

///|
/// Alias-aware variant of `lint_selected`.
pub fn lint_selected_with(
  root : String,
  mode : ScanMode,
  files : Array[String],
  rules : Array[Rule],
  file_rules : Array[FileRule],
  structural_rules : Array[StructuralRule],
  scope : Scope,
  aliases : Array[(String, String)],
) -> LintResult raise {
  lint_selected_impl(
    root,
    mode,
    files,
    rules,
    file_rules,
    structural_rules,
    scope,
    aliases,
    None,
  ).0
}

///|
/// Cache-aware variant of `lint_selected`. Returns the lint result together
/// with the refreshed cache, which the caller persists. Passing a cache whose
/// fingerprint does not match the effective rule set (or a stale format
/// version) degrades to a full scan — correctness first, speed second.
pub fn lint_selected_cached(
  root : String,
  mode : ScanMode,
  files : Array[String],
  rules : Array[Rule],
  file_rules : Array[FileRule],
  structural_rules : Array[StructuralRule],
  scope : Scope,
  cache : Cache?,
) -> (LintResult, Cache) raise {
  lint_selected_cached_with(
    root,
    mode,
    files,
    rules,
    file_rules,
    structural_rules,
    scope,
    cache,
    [],
  )
}

///|
/// Alias-aware variant of `lint_selected_cached`.
pub fn lint_selected_cached_with(
  root : String,
  mode : ScanMode,
  files : Array[String],
  rules : Array[Rule],
  file_rules : Array[FileRule],
  structural_rules : Array[StructuralRule],
  scope : Scope,
  cache : Cache?,
  aliases : Array[(String, String)],
) -> (LintResult, Cache) raise {
  lint_selected_impl(
    root, mode, files, rules, file_rules, structural_rules, scope, aliases, cache,
  )
}

///|
/// The scan core, shared by the plain and cache-aware entry points. `cache`
/// is the previous run's cache (or `None`); the returned cache always carries
/// the current fingerprint and one entry per successfully scanned file.
fn lint_selected_impl(
  root : String,
  mode : ScanMode,
  files : Array[String],
  rules : Array[Rule],
  file_rules : Array[FileRule],
  structural_rules : Array[StructuralRule],
  scope : Scope,
  aliases : Array[(String, String)],
  cache : Cache?,
) -> (LintResult, Cache) raise {
  let candidates : Array[String] = []
  let seen : Set[String] = Set::from_iter([].iter())
  for file in files {
    let rel = normalize_relative(file)
    if !seen.contains(rel) {
      candidates.push(rel)
      seen.add(rel)
    }
  }
  candidates.sort()
  let selected = candidates.filter(rel => scope.allows(rel))
  let out_of_scope = candidates.length() - selected.length()
  let findings : Array[Finding] = []
  let diagnostics : Array[Diagnostic] = []
  let skipped_files : Array[SkippedFile] = []
  // Sources of successfully scanned files, kept for the project pass.
  let project_sources : Array[(String, String)] = []
  let disable_maps : Array[(String, DisableMap)] = []
  // The cache is only reusable when the effective rule set is unchanged.
  let fingerprint = config_fingerprint(
    rules, file_rules, structural_rules, scope,
  )
  // Rules that opted into literal text need the literal view built per file;
  // `match_comments` rules already see the raw view, so they do not count.
  let wants_literal_view = rules.any(rule => {
    rule.match_literals && !rule.match_comments
  })
  // The raw view is only consumed by `match_comments` rules and file rules.
  // Splitting it per file allocates one string per line, so skip it when
  // nothing can read it.
  let wants_raw_view = !file_rules.is_empty() ||
    rules.any(rule => rule.match_comments)
  let reusable : Cache? = match cache {
    Some(previous) =>
      if previous.is_current(fingerprint) {
        Some(previous)
      } else {
        None
      }
    None => None
  }
  let frozen : GitSnapshot? = match mode {
    Changed(_) => Some(capture_git_snapshot(root, false))
    Staged => Some(capture_git_snapshot(root, true))
    _ => None
  }
  let updated = Cache::new(fingerprint)
  // Structural models are content-addressed and rule-independent, so they are
  // reused from the previous cache even when the configuration changed.
  let previous_models : Map[String, ModelEntry] = match cache {
    Some(previous) => previous.models
    None => Map([])
  }
  let models_out : Map[String, ModelEntry] = Map([])
  let prebuilt_models : Map[String, FileModel] = Map([])
  // Every scanned file's path and content digest: the identity of the
  // project the structural pass sees.
  let scanned_ids : Array[(String, String)] = []
  let mut scanned = 0
  let mut skipped = 0
  let mut failed = 0
  for rel in selected {
    // No applicable rule is intentionally skipped; it is not a successful read.
    if !rules.any(rule => rule_applies(rule, rel)) &&
      !file_rules.any(fr => file_rule_applies(fr, rel)) &&
      !structural_rules.any(sr => sr.applies_to(rel)) {
      skipped += 1
      skipped_files.push({ file: rel, reason: "no-applicable-rule", })
      continue
    }
    try {
      let kind = match frozen {
        Some(snapshot) => snapshot.kind(rel)
        None => rooted_source_kind(root, rel)
      }
      if kind == 3 {
        skipped += 1
        skipped_files.push({ file: rel, reason: "not-a-regular-file", })
        continue
      }
      if kind != 1 {
        raise Failure("source is missing or is not a regular file")
      }
      let source = match frozen {
        Some(snapshot) => snapshot.source(root, rel)
        None => read_source(join_path(root, rel))
      }
      // A MoonBit file is lexed once and every consumer below reuses the same
      // view: validation, inline-disable comments, the code view, the comment
      // view and the project pass. Lexing it per consumer cost ~5x the work.
      let mb_view : MoonbitLexView? = if rel.has_suffix(".mbt") {
        let view = moonbit_lex(source)
        validate_moonbit_view(view)
        Some(view)
      } else {
        None
      }
      let hash = content_hash(source)
      // Parse the structural model once per file, reusing the cached model
      // when the content digest matches. MoonBit files are handled by their
      // own declaration scanner, so they are not parsed here.
      if !structural_rules.is_empty() && !rel.has_suffix(".mbt") {
        let reused = match previous_models.get(rel) {
          Some(entry) =>
            if entry.hash == hash {
              Some(entry.model)
            } else {
              None
            }
          None => None
        }
        let model = match reused {
          Some(m) => m
          None => parse_source_for(rel, source)
        }
        models_out.set(rel, { hash, model, })
        prebuilt_models.set(rel, model)
      }
      // A cache hit means identical content under an identical rule set, so
      // the previous findings are still exactly right: reuse them instead of
      // re-running the matchers. The source is still needed for the project
      // pass when structural rules are configured.
      let cached : CacheEntry? = match reusable {
        Some(previous) =>
          match previous.get(rel) {
            Some(entry) => if entry.hash == hash { Some(entry) } else { None }
            None => None
          }
        None => None
      }
      match cached {
        Some(entry) => {
          for finding in entry.findings {
            findings.push(finding)
          }
          if !structural_rules.is_empty() {
            project_sources.push((rel, source))
            scanned_ids.push((rel, hash))
            disable_maps.push(
              (
                rel,
                parse_disable_comments(
                  match mb_view {
                    Some(view) => view.comments
                    None => source
                  },
                ),
              ),
            )
          }
          updated.put(rel, hash, entry.findings)
          scanned += 1
          continue
        }
        None => ()
      }
      let disable_map = parse_disable_comments(
        match mb_view {
          Some(view) => view.comments
          None => source
        },
      )
      let raw_lines = if wants_raw_view {
        split_lines(source)
      } else {
        ([] : Array[(Int32, String)])
      }
      let mode = comment_mode_for(rel)
      // The code view blanks literal contents (JS/TS family); the literal view
      // keeps them for rules that opted in with `match_literals`. It is only
      // built when such a rule exists, and for `Plain`/MoonBit sources the two
      // views are identical anyway.
      let lines = split_lines(
        match mb_view {
          Some(view) => view.code
          None => strip_comments_code(source, mode)
        },
      )
      let literal_lines = if !wants_literal_view {
        lines
      } else {
        match mb_view {
          // MoonBit literal contents are already blanked in the code view, so
          // the literal view is the same view.
          Some(_) => lines
          None => split_lines(strip_comments(source, mode))
        }
      }
      let comment_lines = match mb_view {
        Some(view) => split_lines(moonbit_code_and_comments(view))
        None => raw_lines
      }
      // Per-file findings are collected separately so they can be stored in
      // the cache; the project pass appends its own afterwards.
      let file_findings : Array[Finding] = []
      for finding in lint_lines(rel, lines, literal_lines, comment_lines, rules) {
        if !disable_map.is_disabled(finding.rule_id, finding.line) {
          file_findings.push(finding)
        }
      }
      for finding in lint_file(rel, source, raw_lines, file_rules) {
        // Whole-file checks (BOM, line ending, size, empty file, naming) are
        // properties of the file, not of a line: a `// moonlint-disable` on
        // line 1 must not be able to mask them. Only per-line file checks
        // (trailing whitespace, tabs, max_line_length) are suppressible.
        let per_line = is_per_line_check(finding.rule_id, file_rules)
        if !per_line || !disable_map.is_disabled(finding.rule_id, finding.line) {
          file_findings.push(finding)
        }
      }
      for finding in file_findings {
        findings.push(finding)
      }
      updated.put(rel, hash, file_findings)
      if !structural_rules.is_empty() {
        project_sources.push((rel, source))
        disable_maps.push((rel, disable_map))
        scanned_ids.push((rel, hash))
      }
      scanned += 1
    } catch {
      error => {
        failed += 1
        diagnostics.push({ file: rel, message: error_message(error), })
      }
    }
  }
  // Persist the models parsed this run. Files outside this run's selection
  // keep no entry: a later full scan re-parses them rather than trusting a
  // model whose source it has not seen. Files inside the selection that were
  // not re-parsed (a line-only preset, a skipped file) keep their previous
  // model, which is safe because every reuse is hash-checked.
  for rel, entry in models_out {
    updated.put_model(rel, entry.hash, entry.model)
  }
  for rel in selected {
    if !models_out.contains(rel) {
      match previous_models.get(rel) {
        Some(entry) => updated.models.set(rel, entry)
        None => ()
      }
    }
  }
  let mut context_scanned = 0
  let mut context_failed = 0
  // The project pass only ever reports findings for selected files, so with an
  // empty selection it cannot produce anything. Reading the whole tree as
  // dependency context merely to discard every result would be wasted work (and
  // would turn an unrelated broken file into an operational failure), so skip
  // both the context read and the analysis.
  let project_pass = !structural_rules.is_empty() && !selected.is_empty()
  // A full-tree scan whose files were all read and all in scope has no
  // dependency context of its own: the verdict is a pure function of the
  // rule set and the file contents. That makes the whole structural pass
  // cacheable, not just its per-file pieces.
  let cacheable_pass = project_pass &&
    mode is Tree &&
    out_of_scope == 0 &&
    failed == 0
  let pass_key : String? = if cacheable_pass {
    Some(structural_key(fingerprint, scanned_ids))
  } else {
    None
  }
  // Reuse the previous verdict when the key matches exactly.
  let reused_pass : Array[Finding]? = match (pass_key, cache) {
    (Some(key), Some(previous)) =>
      match previous.get_structural() {
        Some(entry) =>
          if entry.key == key {
            Some(entry.findings)
          } else {
            None
          }
        None => None
      }
    _ => None
  }
  match reused_pass {
    Some(cached) => {
      // The cached findings carry no snippets: they are re-derived from the
      // sources that were just scanned.
      for finding in rehydrate_snippets(cached, project_sources) {
        findings.push(finding)
      }
      // Keep the verdict in the cache: a reused run must leave the cache in
      // the same state as a computing run, or the next run would recompute.
      match pass_key {
        Some(key) => updated.put_structural(key, cached)
        None => ()
      }
    }
    None => {
      if project_pass &&
        structural_rules.any(rule => needs_project_context(rule.check)) {
        let (count, failures, context_diagnostics) = add_project_context(
          root, project_sources, frozen,
        )
        context_scanned = count
        context_failed = failures
        for diagnostic in context_diagnostics {
          diagnostics.push(diagnostic)
        }
      }
      // Project pass: one index over every scanned file, then every structural
      // rule. Findings honour inline disables like per-file findings do.
      if project_pass {
        // The JS/TS index does not need to parse MoonBit files; handing them to
        // the JS parser would only produce empty models. MoonBit rules get the
        // raw sources and their own declaration scanner.
        let js_sources = project_sources.filter(pair => {
          let (rel, _) = pair
          !rel.has_suffix(".mbt")
        })
        let index = build_index_from_models(
          js_sources, aliases, prebuilt_models,
        )
        let structural_findings = if context_failed == 0 {
          analyze(index, structural_rules)
        } else {
          []
        }
        for finding in analyze_moonbit(project_sources, structural_rules) {
          structural_findings.push(finding)
        }
        let reported : Array[Finding] = []
        for finding in structural_findings {
          if !selected.contains(finding.file) {
            continue
          }
          let suppressed = disable_maps.any(pair => {
            let (rel, map) = pair
            rel == finding.file &&
            map.is_disabled(finding.rule_id, finding.line)
          })
          if !suppressed {
            reported.push(finding)
          }
        }
        for finding in reported {
          findings.push(finding)
        }
        // Store the verdict for the next run. The snippets are dropped: one
        // minified line can be larger than the whole cache, and the line is
        // re-derived from the scanned source.
        match pass_key {
          Some(key) =>
            if context_failed == 0 {
              updated.put_structural(
                key,
                reported.map(finding => { ..finding, snippet: "", }),
              )
            }
          None => ()
        }
      }
    }
  }
  findings.sort_by((a, b) => {
    let file_order = a.file.compare(b.file)
    if file_order != 0 {
      file_order
    } else {
      let line_order = a.line.compare(b.line)
      if line_order != 0 {
        line_order
      } else {
        a.rule_id.compare(b.rule_id)
      }
    }
  })
  let result : LintResult = {
    findings,
    files_selected: selected.length(),
    files_scanned: scanned,
    files_skipped: skipped,
    files_failed: failed,
    files_out_of_scope: out_of_scope,
    context_files_scanned: context_scanned,
    context_files_failed: context_failed,
    source_snapshot: frozen.map(snapshot => snapshot.revision),
    baseline_hits: 0,
    diagnostics,
    skipped_files,
  }
  (result, updated)
}

///|
/// Fill in the snippets of cached structural findings from the scanned
/// sources. The cache stores findings without snippets (a minified one-line
/// file can make a single snippet larger than the whole cache), and the line
/// is already in memory because the file was just scanned.
fn rehydrate_snippets(
  findings : Array[Finding],
  sources : Array[(String, String)],
) -> Array[Finding] {
  let source_by_file : Map[String, String] = Map([])
  for pair in sources {
    let (rel, source) = pair
    source_by_file.set(rel, source)
  }
  let lines_by_file : Map[String, Array[String]] = Map([])
  let out : Array[Finding] = []
  for finding in findings {
    let lines = match lines_by_file.get(finding.file) {
      Some(existing) => existing
      None => {
        let lines = match source_by_file.get(finding.file) {
          Some(source) => split_lines(source).map(pair => pair.1)
          None => ([] : Array[String])
        }
        lines_by_file.set(finding.file, lines)
        lines
      }
    }
    // The snippet is the trimmed raw line, exactly as a fresh scan reports it.
    let snippet = if finding.line >= 1 && finding.line <= lines.length() {
      lines[finding.line - 1].trim().to_owned()
    } else {
      ""
    }
    out.push({ ..finding, snippet, })
  }
  out
}

///|
/// Human-readable operational errors with their useful context retained.
pub fn error_message(error : Error) -> String {
  match error {
    Failure(message) => message
    @fs.IOError(message) => message
    NativeError(message) => message
    ConfigError(message) => message
    _ => "scan operation failed"
  }
}

///|
/// Whether a file rule applies to a given path.
fn file_rule_applies(fr : FileRule, rel : String) -> Bool {
  has_wanted_ext(
    rel,
    Set::from_iter(fr.extensions.iter().map(e => e.to_lower())),
  ) &&
  fr.scope.allows(rel)
}

///|
/// Public view of `file_rule_applies`, so the CLI's `--fix` path honours the
/// same extension and include/exclude scope as the scan.
pub fn FileRule::applies_to(self : FileRule, rel : String) -> Bool {
  file_rule_applies(self, rel)
}

///|
/// Whether a structural (project-level) rule applies to a given path.
pub fn StructuralRule::applies_to(self : StructuralRule, rel : String) -> Bool {
  file_rule_applies(
    {
      id: self.id,
      severity: self.severity,
      message: self.message,
      extensions: self.extensions,
      check: NoEmptyFiles,
      scope: self.scope,
    },
    rel,
  )
}

///|
/// Whether a file check reports per offending line (and so may be silenced by
/// an inline `moonlint-disable` for that line), as opposed to describing a
/// whole-file property.
fn is_per_line_check(rule_id : String, file_rules : Array[FileRule]) -> Bool {
  for fr in file_rules {
    if fr.id == rule_id {
      return match fr.check {
        MaxLineLength(_) | NoTrailingWhitespace | NoTabs => true
        _ => false
      }
    }
  }
  false
}

///|
/// Evaluate file-level rules against a file's content. Each finding points to
/// the specific line where the violation occurs (or line 1 for file-level
/// properties like naming).
pub fn lint_file(
  rel : String,
  source : String,
  lines : Array[(Int32, String)],
  file_rules : Array[FileRule],
) -> Array[Finding] {
  let findings : Array[Finding] = []
  for fr in file_rules {
    if !file_rule_applies(fr, rel) {
      continue
    }
    match fr.check {
      MaxLines(limit) =>
        if lines.length() > limit {
          findings.push(
            file_finding(
              fr,
              rel,
              lines.length(),
              "file has \{lines.length()} lines (limit: \{limit})",
            ),
          )
        }
      MaxLineLength(limit) =>
        for (lineno, text) in lines {
          if text.length() > limit {
            findings.push(
              file_finding_at(
                fr,
                rel,
                lineno,
                "line has \{text.length()} characters (limit: \{limit})",
                text.trim().to_owned(),
              ),
            )
          }
        }
      NoTrailingWhitespace =>
        for (lineno, text) in lines {
          if text.length() > 0 && text.has_suffix(" ") {
            findings.push(file_finding(fr, rel, lineno, "trailing whitespace"))
          } else if text.length() > 0 && text.has_suffix("\t") {
            findings.push(file_finding(fr, rel, lineno, "trailing whitespace"))
          }
        }
      NoTabs =>
        for (lineno, text) in lines {
          if text.contains("\t") {
            findings.push(file_finding(fr, rel, lineno, "tab character found"))
          }
        }
      FinalNewline =>
        if source.length() > 0 && !source.has_suffix("\n") {
          findings.push(
            file_finding(
              fr,
              rel,
              lines.length(),
              "file does not end with a newline",
            ),
          )
        }
      NoDuplicateLines(min_len) => {
        let seen : Set[String] = Set([])
        for (lineno, text) in lines {
          let trimmed = text.trim().to_owned()
          // Short structural lines (`}`, ``, `/**`) repeat in every
          // file by construction and say nothing about duplication; only
          // substantive lines are compared. Empty lines never count.
          if trimmed != "" && trimmed.length() >= min_len {
            if seen.contains(trimmed) {
              findings.push(
                file_finding(fr, rel, lineno, "duplicate line: \{trimmed}"),
              )
            } else {
              seen.add(trimmed)
            }
          }
        }
      }
      FileNaming(style) => {
        let name = file_stem(rel)
        if !naming_matches(style, name) {
          findings.push(
            file_finding(
              fr,
              rel,
              1,
              "file name \"\{name}\" does not match \{naming_style_name(style)} convention",
            ),
          )
        }
      }
      MaxFileSize(limit) => {
        let size = source.length()
        if size > limit {
          findings.push(
            file_finding(fr, rel, 1, "file is \{size} bytes (limit: \{limit})"),
          )
        }
      }
      NoEmptyFiles =>
        if source.trim().to_owned() == "" {
          findings.push(file_finding(fr, rel, 1, "file is empty"))
        }
      LineEnding(style) => {
        let has_crlf = source.contains("\r\n")
        let has_lf_only = source.contains("\n") && !source.contains("\r\n")
        match style {
          Lf =>
            if has_crlf {
              findings.push(
                file_finding(
                  fr, rel, 1, "file uses CRLF line endings; expected LF",
                ),
              )
            }
          Crlf =>
            if has_lf_only {
              findings.push(
                file_finding(
                  fr, rel, 1, "file uses LF line endings; expected CRLF",
                ),
              )
            }
        }
      }
      NoBom =>
        if source.length() >= 3 && source[0] == '\u{FEFF}' {
          findings.push(
            file_finding(fr, rel, 1, "file starts with a UTF-8 BOM"),
          )
        }
    }
  }
  findings
}

///|
/// Build a finding for a file-level rule.
fn file_finding(
  fr : FileRule,
  file : String,
  line : Int32,
  detail : String,
) -> Finding {
  file_finding_at(fr, file, line, detail, detail)
}

///|
/// A file-level finding with an explicit snippet. Per-line checks
/// (`max_line_length`, trailing whitespace, tabs) pass the offending source
/// line; whole-file properties pass the detail sentence instead.
fn file_finding_at(
  fr : FileRule,
  file : String,
  line : Int32,
  detail : String,
  snippet : String,
) -> Finding {
  {
    rule_id: fr.id,
    severity: fr.severity,
    message: "\{fr.message} (\{detail})",
    file,
    line,
    // Whole-file checks describe a property of the file, not a position on
    // one line, so they report the first column.
    column: 1,
    snippet,
  }
}

///|
/// Extract the file name without extension from a relative path.
fn file_stem(rel : String) -> String {
  let name = match rel.rev_split_once("/") {
    Some((_, n)) => n.to_owned()
    None => rel
  }
  match name.rev_split_once(".") {
    Some((stem, _)) => stem.to_owned()
    None => name
  }
}

///|
/// Whether a file name matches the given naming convention.
fn naming_matches(style : NamingStyle, name : String) -> Bool {
  if name == "" {
    return false
  }
  match style {
    CamelCase =>
      // First char lowercase, no underscores or hyphens
      name[0] >= 'a' &&
      name[0] <= 'z' &&
      !name.contains("_") &&
      !name.contains("-")
    PascalCase =>
      // First char uppercase, no underscores or hyphens
      name[0] >= 'A' &&
      name[0] <= 'Z' &&
      !name.contains("_") &&
      !name.contains("-")
    SnakeCase =>
      // All lowercase with underscores, no hyphens
      !name.contains("-") &&
      name
      .iter()
      .all(c => (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '_')
    KebabCase =>
      // All lowercase with hyphens, no underscores
      !name.contains("_") &&
      name
      .iter()
      .all(c => (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-')
  }
}

///|
/// Human-readable name for a naming style.
fn naming_style_name(style : NamingStyle) -> String {
  match style {
    CamelCase => "camelCase"
    PascalCase => "PascalCase"
    SnakeCase => "snake_case"
    KebabCase => "kebab-case"
  }
}