ionide / ionide-analyzers

http://ionide.io/ionide-analyzers/
MIT License
6 stars 7 forks source link

Extend the postfix generics analyzer #34

Closed sheridanchris closed 9 months ago

sheridanchris commented 9 months ago

The style guide suggests that you should follow the .NET style for generics except for lists, arrays, sequences, options, value options, and reference cells. Right now there is an analyzer for arrays but this can be extended to cover other types as well.

Edit: May also be worth including an analyzer for the .NET style. When postfix is used when it shouldn't be.

https://learn.microsoft.com/en-us/dotnet/fsharp/style-guide/formatting#for-types-prefer-prefix-syntax-for-generics-foot-with-some-specific-exceptions

sheridanchris commented 9 months ago

Looking at the AST, something like this should do the trick. Still need to refactor and test.

let ts = ResizeArray<string * range>()

let collector =
    { new SyntaxCollectorBase() with
        override x.WalkType(t: SynType) =
            match t with
            | SynType.Array _ -> ts.Add("Prefer postfix syntax for arrays.", t.Range)
            | SynType.App(typeName, _, _, _, _, false, _) ->
                match typeName with
                | SynType.LongIdent synLongIdent ->
                    match synLongIdent.LongIdent with
                    | [ ident ] ->
                        match ident.idText with
                        | "seq" -> ts.Add("Prefer postfix syntax for sequences.", t.Range)
                        | "list" -> ts.Add("Prefer postfix syntax for lists.", t.Range)
                        | "option" -> ts.Add("Prefer postfix syntax for options.", t.Range)
                        | "ref" -> ts.Add("Prefer postfix syntax for reference cells.", t.Range)
                        | _ -> ()
                    | _ -> ()
                | _ -> ()
            | _ -> ()
    }

walkAst collector context.ParseFileResults.ParseTree