|
| 1 | +module FSharpLint.Rules.PreferStringInterpolationWithSprintf |
| 2 | + |
| 3 | +open System |
| 4 | +open FSharpLint.Framework |
| 5 | +open FSharpLint.Framework.Suggestion |
| 6 | +open FSharp.Compiler.Syntax |
| 7 | +open FSharpLint.Framework.Ast |
| 8 | +open FSharpLint.Framework.Rules |
| 9 | + |
| 10 | +let mutable moduleIdentifiers = Set.empty |
| 11 | +let mutable letIdentifiers = Set.empty |
| 12 | + |
| 13 | +let private isStringFormat (identifiers: List<Ident>) = |
| 14 | + "String" = identifiers.[0].idText && "Format" = identifiers.[1].idText |
| 15 | + |
| 16 | +let private genereateErrorMessage range = |
| 17 | + { Range = range |
| 18 | + Message = Resources.GetString "RulesPreferStringInterpolationWithSprintf" |
| 19 | + SuggestedFix = None |
| 20 | + TypeChecks = List.empty } |
| 21 | + |> Array.singleton |
| 22 | + |
| 23 | +let runner args = |
| 24 | + match args.AstNode with |
| 25 | + | AstNode.Expression(SynExpr.App(_, _, SynExpr.LongIdent(_, LongIdentWithDots(ids, _), _, _), paren, range)) when ids.Length = 2 && isStringFormat ids -> |
| 26 | + let isTopMember (text: string) = |
| 27 | + moduleIdentifiers.Contains text |
| 28 | + match paren with |
| 29 | + | SynExpr.Paren(SynExpr.Tuple(_, [SynExpr.Const(SynConst.String(_, _, _), _); _], _, _), _, _, _) -> |
| 30 | + genereateErrorMessage range |
| 31 | + | SynExpr.Paren(SynExpr.Tuple(_, [SynExpr.Ident identifier; _], _, _), _, _, range) -> |
| 32 | + |
| 33 | + if isTopMember identifier.idText then |
| 34 | + genereateErrorMessage range |
| 35 | + else |
| 36 | + let isNamedBinding binding = |
| 37 | + match binding with |
| 38 | + | SynBinding(_, _, _, _, _, _, _, SynPat.Named(_, ident, _, _, _), _, _, _, _) -> |
| 39 | + identifier.idText = ident.idText |
| 40 | + | _ -> false |
| 41 | + |
| 42 | + let isVisible asts = |
| 43 | + let rec loop asts = |
| 44 | + match asts with |
| 45 | + | AstNode.Expression (SynExpr.LetOrUse (_, _, [binding], _, _)) :: rest -> |
| 46 | + isNamedBinding binding || loop rest |
| 47 | + | _ :: rest -> loop rest |
| 48 | + | [] -> false |
| 49 | + loop asts |
| 50 | + |
| 51 | + let getTopLevelParent index = |
| 52 | + let rec loop index = |
| 53 | + let parents = List.rev (args.GetParents index) |
| 54 | + match parents with |
| 55 | + | AstNode.ModuleDeclaration (SynModuleDecl.Let _) :: rest -> rest |
| 56 | + | _ -> loop (index + 1) |
| 57 | + loop index |
| 58 | + |
| 59 | + if letIdentifiers.Contains identifier.idText && isVisible (getTopLevelParent 2) then |
| 60 | + genereateErrorMessage range |
| 61 | + else |
| 62 | + Array.empty |
| 63 | + | _ -> Array.empty |
| 64 | + | AstNode.Binding(SynBinding(_, _, _, _, _, _, _, SynPat.Named(_, identifier, _, _, _), _, SynExpr.Const(SynConst.String(value, _, _), _), range, _)) when value.Contains "{0}" -> |
| 65 | + let parents = args.GetParents 1 |
| 66 | + match parents with |
| 67 | + | AstNode.ModuleDeclaration(SynModuleDecl.Let _) :: _ -> |
| 68 | + moduleIdentifiers <- moduleIdentifiers.Add(identifier.idText) |
| 69 | + | _ -> letIdentifiers <- letIdentifiers.Add(identifier.idText) |
| 70 | + Array.empty |
| 71 | + | AstNode.ModuleDeclaration(SynModuleDecl.Let _) -> |
| 72 | + letIdentifiers <- Set.empty |
| 73 | + Array.empty |
| 74 | + | AstNode.ModuleDeclaration(SynModuleDecl.NestedModule _) -> |
| 75 | + moduleIdentifiers <- Set.empty |
| 76 | + letIdentifiers <- Set.empty |
| 77 | + Array.empty |
| 78 | + | _ -> Array.empty |
| 79 | + |
| 80 | +let cleanup () = |
| 81 | + moduleIdentifiers <- Set.empty |
| 82 | + letIdentifiers <- Set.empty |
| 83 | + |
| 84 | +let rule = |
| 85 | + { Name = "PreferStringInterpolationWithSprintf" |
| 86 | + Identifier = Identifiers.PreferStringInterpolationWithSprintf |
| 87 | + RuleConfig = { AstNodeRuleConfig.Runner = runner; Cleanup = cleanup } } |
| 88 | + |> AstNodeRule |
0 commit comments