package dunolint

  1. Overview
  2. Docs
A linter for build files in dune projects

Install

dune-project
 Dependency

Authors

Maintainers

Sources

dunolint-0.0.20250804.tbz
sha256=e4ca7c98db73dd9ab2ae8cba37ee0645f580267484e9893dbce6e28f4f2f0170
sha512=7ca658fb96139a0c41724355ac6aaf83d75468c7df14569b8f6090711f73a8fb2408ed1145384e756418682f0fa660a915842fd2b3f8b42e5ed4990e795b384e

doc/src/dunolint.dune_linter/library__modes.ml.html

Source file library__modes.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
(*********************************************************************************)
(*  Dunolint - A tool to lint and help manage files in dune projects             *)
(*  Copyright (C) 2024-2025 Mathieu Barbin <mathieu.barbin@gmail.com>            *)
(*                                                                               *)
(*  This file is part of Dunolint.                                               *)
(*                                                                               *)
(*  Dunolint is free software; you can redistribute it and/or modify it          *)
(*  under the terms of the GNU Lesser General Public License as published by     *)
(*  the Free Software Foundation either version 3 of the License, or any later   *)
(*  version, with the LGPL-3.0 Linking Exception.                                *)
(*                                                                               *)
(*  Dunolint is distributed in the hope that it will be useful, but WITHOUT      *)
(*  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *)
(*  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *)
(*  and the file `NOTICE.md` at the root of this repository for more details.    *)
(*                                                                               *)
(*  You should have received a copy of the GNU Lesser General Public License     *)
(*  and the LGPL-3.0 Linking Exception along with this library. If not, see      *)
(*  <http://www.gnu.org/licenses/> and <https://spdx.org>, respectively.         *)
(*********************************************************************************)

module Ordered_set = struct
  type t = Dune.Compilation_mode.t Dunolinter.Ordered_set.t [@@deriving sexp_of]
end

type t = { mutable modes : Ordered_set.t } [@@deriving sexp_of]

let field_name = "modes"

module Handler =
  Dunolinter.Sexp_handler.Make_sexpable_ordered_set
    (struct
      let field_name = field_name
    end)
    (Dune.Compilation_mode)

let create ~modes = { modes }
let modes t = t.modes
let set_modes t ~modes = t.modes <- modes

let read ~sexps_rewriter ~field =
  let modes = Handler.read ~sexps_rewriter ~field in
  { modes = Dunolinter.Ordered_set.canonical_sort (module Dune.Compilation_mode) modes }
;;

let write t = Handler.write t.modes
let rewrite t ~sexps_rewriter ~field = Handler.rewrite t.modes ~sexps_rewriter ~field

type predicate = Dune.Library.Modes.Predicate.t

let has_mode t ~mode =
  match
    Dunolinter.Ordered_set.mem
      (module Dune.Compilation_mode)
      t.modes
      mode
      ~evaluator:Dunolinter.Ordered_set.Evaluator.static
  with
  | Known true -> true
  | Known false | Unknown -> false
;;

let eval t ~predicate =
  match (predicate : predicate) with
  | `has_modes modes ->
    Dunolint.Trilang.const (List.for_all modes ~f:(fun mode -> has_mode t ~mode))
  | `has_mode mode -> Dunolint.Trilang.const (has_mode t ~mode)
;;

let insert_mode t ~mode =
  match
    Dunolinter.Ordered_set.mem
      (module Dune.Compilation_mode)
      t.modes
      mode
      ~evaluator:Dunolinter.Ordered_set.Evaluator.static
  with
  | Known true -> ()
  | Known false | Unknown ->
    t.modes <- Dunolinter.Ordered_set.insert (module Dune.Compilation_mode) t.modes mode
;;

let remove_mode t ~mode =
  match
    Dunolinter.Ordered_set.mem
      (module Dune.Compilation_mode)
      t.modes
      mode
      ~evaluator:Dunolinter.Ordered_set.Evaluator.static
  with
  | Known false -> ()
  | Known true | Unknown ->
    t.modes <- Dunolinter.Ordered_set.remove (module Dune.Compilation_mode) t.modes mode
;;

let enforce =
  Dunolinter.Linter.enforce
    (module Dune.Library.Modes.Predicate)
    ~eval
    ~enforce:(fun t predicate ->
      match predicate with
      | T (`has_mode mode) ->
        insert_mode t ~mode;
        Ok
      | T (`has_modes modes) ->
        List.iter modes ~f:(fun mode -> insert_mode t ~mode);
        Ok
      | Not (`has_mode mode) ->
        remove_mode t ~mode;
        Ok
      | Not (`has_modes modes) ->
        List.iter modes ~f:(fun mode -> remove_mode t ~mode);
        Ok)
;;

let initialize ~condition =
  let modes =
    let set =
      Dunolinter.Linter.at_positive_enforcing_position condition
      |> List.concat_map ~f:(function
        | `has_modes modes -> modes
        | `has_mode mode -> [ mode ])
      |> Set.of_list (module Dune.Compilation_mode)
    in
    if Set.is_empty set then Set.singleton (module Dune.Compilation_mode) `best else set
  in
  { modes = Dunolinter.Ordered_set.of_set modes }
;;
OCaml

Innovation. Community. Security.