Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend the postfix generics analyzer #34

Closed
sheridanchris opened this issue Nov 15, 2023 · 1 comment · Fixed by #39
Closed

Extend the postfix generics analyzer #34

sheridanchris opened this issue Nov 15, 2023 · 1 comment · Fixed by #39

Comments

@sheridanchris
Copy link
Contributor

sheridanchris commented Nov 15, 2023

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 sheridanchris changed the title Extend the postfix analyzer Extend the postfix generics analyzer Nov 15, 2023
@sheridanchris
Copy link
Contributor Author

sheridanchris commented Nov 16, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant