ionide / ionide-analyzers

Home Page:http://ionide.io/ionide-analyzers/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Extend the postfix generics analyzer

sheridanchris opened this issue · comments

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

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