Skip to content

Feature request: enforce alphabetical definitions #2795

Description

@N1ark

Is your feature request related to a problem? Please describe.
It would be nice to allow optionally linting for alphabetical declarations, if annotated. This allows enforcing long lists of independent declaration to remain alphabetically sorted. This facilitates looking for particular definitions. This also facilitates teamwork, as there is only one correct order in which to define things which avoids potential git conflicts over concurrent changes to the same list.

Describe the solution you'd like
Marking the start-end of the declaration list with comments would be sufficient:

(* @ocamlformat alphabetical-start *)
let pexp_apply x y = pexp_apply ~loc:(get_loc ()) x y
let pexp_construct x y = pexp_construct ~loc:(get_loc ()) x y
let pexp_field x y = pexp_field ~loc:(get_loc ()) x y
let pexp_ident x = pexp_ident ~loc:(get_loc ()) x
let pexp_match x y = pexp_match ~loc:(get_loc ()) x y
let pexp_record x y = pexp_record ~loc:(get_loc ()) x y
(* @ocamlformat alphabetical-end *)

I'm aware ocamlformat also supports attributes, so maybe something like this would be more OCaml-esque :)

[@@@ocamlformat "alphabetical=start"]
let pexp_apply x y = pexp_apply ~loc:(get_loc ()) x y
let pexp_construct x y = pexp_construct ~loc:(get_loc ()) x y
let pexp_field x y = pexp_field ~loc:(get_loc ()) x y
let pexp_ident x = pexp_ident ~loc:(get_loc ()) x
let pexp_match x y = pexp_match ~loc:(get_loc ()) x y
let pexp_record x y = pexp_record ~loc:(get_loc ()) x y
[@@@ocamlformat "alphabetical=end"]

Additional context
Rust has a similar feature, with tidy::alphabetical; it is widely used in the ecosystem and even within the compiler itself. it looks like this in practice:

// tidy-alphabetical-start
fn aaa() {}
fn eee() {}
fn z() {}
// tidy-alphabetical-end

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions