package dunolint
A linter for build files in dune projects
Install
Dune Dependency
Authors
Maintainers
Sources
dunolint-0.0.20250310.tbz
sha256=b44119c96aeabb960e852711c2f20ae43f0011e796f05e012141ba980a5b58f6
sha512=5bf76732adc940db6cac0875e94c3eaff436288faba95388b9675c68b3f30f5362e01f289fc8a096325f7db812ace273773a1e051ab7d7ace3b8db4a57a0e070
doc/src/dunolint.dune_linter/pps.ml.html
Source file pps.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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
(*********************************************************************************) (* 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 Arg = struct type t = | Pp of Dune.Pp.Name.t | Flag of { name : string ; param : string option } [@@deriving equal, sexp_of] end module Mutable_arg = struct module Applies_to = struct type t = [ `pp of Dune.Pp.Name.t | `driver ] [@@deriving sexp_of] end type t = | Pp of { mutable pp_name : Dune.Pp.Name.t } | Flag of { mutable name : string ; mutable param : string option ; mutable applies_to : Applies_to.t } [@@deriving sexp_of] let order_by_name_and_application t1 t2 = match t1, t2 with | Pp { pp_name = pp1 }, Pp { pp_name = pp2 } -> Dune.Pp.Name.compare pp1 pp2 | Pp { pp_name = _ }, Flag { name = _; param = _; applies_to = `driver } -> 1 | Pp { pp_name = pp1 }, Flag { name = _; param = _; applies_to = `pp pp2 } -> (match Dune.Pp.Name.compare pp1 pp2 |> Ordering.of_int with | Less | Equal -> -1 | Greater -> 1) | ( Flag { name = n1; param = _; applies_to = a1 } , Flag { name = n2; param = _; applies_to = a2 } ) -> (match a1, a2 with | `driver, `driver -> String.compare n1 n2 | `driver, `pp _ -> -1 | `pp _, `driver -> 1 | `pp pp1, `pp pp2 -> let res = Dune.Pp.Name.compare pp1 pp2 in if res <> 0 then res else String.compare n1 n2) | Flag { name = _; param = _; applies_to = `driver }, Pp { pp_name = _ } -> -1 | Flag { name = _; param = _; applies_to = `pp pp1 }, Pp { pp_name = pp2 } -> (match Dune.Pp.Name.compare pp1 pp2 |> Ordering.of_int with | Less -> -1 | Equal | Greater -> 1) ;; let of_arg (arg : Arg.t) ~applies_to = match arg with | Pp pp_name -> Pp { pp_name } | Flag { name; param } -> Flag { name; param; applies_to } ;; let parse str ~applies_to ~loc = if String.is_empty str then Err.raise ~loc [ Pp.text "Invalid empty pp." ]; if not (Char.equal str.[0] '-') then Pp { pp_name = Dune.Pp.Name.v str } else ( match String.lsplit2 str ~on:'=' with | None -> Flag { name = str; param = None; applies_to } | Some (name, param) -> Flag { name; param = Some param; applies_to }) ;; let to_string t = match t with | Pp { pp_name } -> Dune.Pp.Name.to_string pp_name | Flag { name; param = None; applies_to = _ } -> name | Flag { name; param = Some param; applies_to = _ } -> name ^ "=" ^ param ;; let write t = Sexp.Atom (to_string t) let init_fold args ~f = let (_ : Applies_to.t), args = List.fold_map args ~init:`driver ~f:(fun applies_to arg -> let arg = f arg ~applies_to in let applies_to = match arg with | Pp { pp_name } -> `pp pp_name | Flag _ -> applies_to in applies_to, arg) in args ;; end type t = { mutable args : Mutable_arg.t list } [@@deriving sexp_of] let create ~args = let args = Mutable_arg.init_fold args ~f:Mutable_arg.of_arg in { args } ;; let parse ~loc atoms = let args = Mutable_arg.init_fold atoms ~f:(fun atom ~applies_to -> Mutable_arg.parse ~applies_to ~loc atom) in { args } ;; let field_name = "pps" let read ~sexps_rewriter ~field = let args = Dunolinter.Sexp_handler.get_args ~field_name ~sexps_rewriter ~field in let args = Mutable_arg.init_fold args ~f:(fun arg ~applies_to -> let loc = Sexps_rewriter.loc sexps_rewriter arg in match arg with | Atom name -> Mutable_arg.parse ~applies_to ~loc name | List _ -> Err.raise ~loc Pp.O. [ Pp.text "Unexpected [Sexp.List]. " ++ Pp_tty.kwd (module String) "Pps" ++ Pp.text " expected to be atoms." ]) in { args } ;; let write (t : t) = let args = List.sort t.args ~compare:Mutable_arg.order_by_name_and_application in Sexp.List (Atom field_name :: List.map args ~f:Mutable_arg.write) ;; let rewrite t ~sexps_rewriter ~field = let args = Dunolinter.Sexp_handler.get_args ~field_name ~sexps_rewriter ~field in let file_rewriter = Sexps_rewriter.file_rewriter sexps_rewriter in let last_offset = match List.last args with | None -> Loc.stop_offset (Sexps_rewriter.loc sexps_rewriter field) | Some arg -> (Sexps_rewriter.range sexps_rewriter arg).stop in let new_args = List.sort t.args ~compare:Mutable_arg.order_by_name_and_application in let rec iter_fields args new_args = match args, new_args with | arg :: args, new_arg :: new_args -> File_rewriter.replace file_rewriter ~range:(Sexps_rewriter.range sexps_rewriter arg) ~text:(Mutable_arg.to_string new_arg); iter_fields args new_args | [], [] -> () | [], _ :: _ -> List.iter new_args ~f:(fun new_arg -> let value = Mutable_arg.to_string new_arg in File_rewriter.insert file_rewriter ~offset:last_offset ~text:(" " ^ value)) | _ :: _, [] -> List.iter args ~f:(fun arg -> File_rewriter.remove file_rewriter ~range:(Sexps_rewriter.range sexps_rewriter arg)) in iter_fields args new_args ;; type predicate = Dune.Pps.Predicate.t let eval t ~predicate = match (predicate : predicate) with | `pp pp_name -> List.exists t.args ~f:(function | Mutable_arg.Pp { pp_name = pp_name' } -> Dune.Pp.Name.equal pp_name pp_name' | Flag { name = _; param = _; applies_to = _ } -> false) |> Dunolint.Trilang.const | `flag { name; param = p_condition; applies_to = a_condition } -> List.exists t.args ~f:(function | Mutable_arg.Pp { pp_name = _ } -> false | Flag { name = name'; param; applies_to } -> [%equal: string] name name' && (match p_condition with | `any -> true | `none -> Option.is_none param | `some -> Option.is_some param | `equals param' -> [%equal: string option] param (Some param')) && (match a_condition, applies_to with | `any, _ | `driver, `driver -> true | `driver, `pp _ | `pp _, `driver -> false | `pp pp1, `pp pp2 -> Dune.Pp.Name.equal pp1 pp2)) |> Dunolint.Trilang.const | `pp_with_flag { pp; flag; param = p_condition } -> List.exists t.args ~f:(function | Mutable_arg.Pp { pp_name = _ } -> false | Flag { name; param; applies_to } -> [%equal: string] flag name && (match p_condition with | `any -> true | `none -> Option.is_none param | `some -> Option.is_some param | `equals param' -> [%equal: string option] param (Some param')) && (match applies_to with | `driver -> false | `pp pp2 -> Dune.Pp.Name.equal pp pp2)) |> Dunolint.Trilang.const ;; let rec enforce t ~condition = match (condition : predicate Blang.t) with | Base (`pp pp_name) -> let handled = List.exists t.args ~f:(function | Mutable_arg.Pp { pp_name = pp_name' } -> Dune.Pp.Name.equal pp_name pp_name' | Flag { name = _; param = _; applies_to = _ } -> false) in if not handled then t.args <- Mutable_arg.Pp { pp_name } :: t.args | Base (`flag { name; param; applies_to }) -> let handled = List.exists t.args ~f:(function | Mutable_arg.Pp { pp_name = _ } -> false | Flag ({ name = name'; param = _; applies_to = _ } as flag) -> if [%equal: string] name name' then ( let () = match param with | `any -> () | `equals param -> flag.param <- Some param | `none -> flag.param <- None | `some -> (match flag.param with | Some _ -> () | None -> Dunolinter.Handler.enforce_failure (module Dune.Pps.Predicate) ~loc:Loc.none ~condition) in let () = match applies_to with | `any -> () | (`pp _ | `driver) as applies_to -> flag.applies_to <- applies_to in true) else false) in if not handled then ( match param with | `some -> Dunolinter.Handler.enforce_failure (module Dune.Pps.Predicate) ~loc:Loc.none ~condition | (`equals _ | `any | `none) as param -> let param = match param with | `equals value -> Some value | `any | `none -> None in let applies_to = match applies_to with | `any | `driver -> `driver | `pp _ as pp -> pp in t.args <- Mutable_arg.Flag { name; param; applies_to } :: t.args) | Base (`pp_with_flag { pp; flag; param }) -> enforce t ~condition:(Blang.base (`pp pp)); enforce t ~condition: (Blang.base (`flag { Dunolint.Dune.Pps.Predicate.Flag.name = flag ; param ; applies_to = `pp pp })) | Not (Base (`pp pp)) -> t.args <- List.filter t.args ~f:(function | Pp { pp_name } -> not (Dune.Pp.Name.equal pp_name pp) | Flag { name = _; param = _; applies_to } -> (match applies_to with | `driver -> true | `pp pp_name -> not (Dune.Pp.Name.equal pp_name pp))) | Not (Base (`flag { name; param; applies_to = a_condition })) as condition -> (match param with | `none | `some | `equals _ -> Dunolinter.Linter.enforce_blang (module Dune.Pps.Predicate) t ~condition ~eval ~enforce | `any -> t.args <- List.filter t.args ~f:(function | Pp { pp_name = _ } -> true | Flag { name = name'; param = _; applies_to } -> if [%equal: string] name name' && match a_condition, applies_to with | `any, _ | `driver, `driver -> true | `driver, `pp _ | `pp _, `driver -> false | `pp pp1, `pp pp2 -> Dune.Pp.Name.equal pp1 pp2 then false else true)) | Not (Base (`pp_with_flag _)) as condition -> Dunolinter.Linter.enforce_blang (module Dune.Pps.Predicate) t ~condition ~eval ~enforce | (And _ | If _ | True | False | Not _ | Or _) as condition -> Dunolinter.Linter.enforce_blang (module Dune.Pps.Predicate) t ~condition ~eval ~enforce ;;
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>