Skip to content

Commit

Permalink
FavourIgnoreOverLetWild: implement suggestions
Browse files Browse the repository at this point in the history
Co-authored-by: webwarrior <reg@webwarrior.ws>
  • Loading branch information
Mersho and webwarrior-ws committed Dec 26, 2023
1 parent 0e7ea0e commit 0d8cbcd
Showing 1 changed file with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,39 @@ open FSharp.Compiler.Syntax
open FSharpLint.Framework.Ast
open FSharpLint.Framework.Rules

let private checkForBindingToAWildcard pattern range =
let private checkForBindingToAWildcard pattern range fileContent (expr: SynExpr) letBindingRange =
let rec findWildAndIgnoreParens = function
| SynPat.Paren(pattern, _) -> findWildAndIgnoreParens pattern
| SynPat.Wild(_) -> true
| _ -> false

if findWildAndIgnoreParens pattern then
{ Range = range
Message = Resources.GetString("RulesFavourIgnoreOverLetWildError")
SuggestedFix = None
TypeChecks = [] } |> Array.singleton
else
Array.empty
match ExpressionUtilities.tryFindTextOfRange expr.Range fileContent with
| Some exprText ->
if findWildAndIgnoreParens pattern then
{ Range = range
Message = Resources.GetString("RulesFavourIgnoreOverLetWildError")
SuggestedFix = Some (lazy (Some({ FromRange = letBindingRange
FromText = fileContent
ToText = sprintf "(%s) |> ignore" exprText })))
TypeChecks = [] } |> Array.singleton
else
Array.empty
| None -> Array.empty

let private runner (args:AstNodeRuleParams) =
match args.AstNode with
| AstNode.Binding(SynBinding(_, _, _, _, _, _, _, pattern, _, _, range, _))
when Helper.Binding.isLetBinding args.NodeIndex args.SyntaxArray ->
checkForBindingToAWildcard pattern range
| AstNode.Binding(SynBinding(_, _, _, _, _, _, _, pattern, _, expr, range, _)) ->
let bindingRange =
match args.GetParents(args.NodeIndex) with
| AstNode.ModuleDeclaration(SynModuleDecl.Let(_, _, range)) :: _
| AstNode.Expression(SynExpr.LetOrUse(_, false, _, _, range)) :: _ ->
Some(range)
| _ -> None

match bindingRange with
| Some letBindingRange ->
checkForBindingToAWildcard pattern range args.FileContent expr letBindingRange
| None -> Array.empty
| _ -> Array.empty

/// Checks if any code uses 'let _ = ...' and suggests to use the ignore function.
Expand Down

0 comments on commit 0d8cbcd

Please sign in to comment.