Skip to content

Commit 1e03c5d

Browse files
committed
Core: fix FSharpLint warning
Fixing the warning for maxNumberOfFunctionParameters rule.
1 parent 122ac97 commit 1e03c5d

File tree

9 files changed

+302
-73
lines changed

9 files changed

+302
-73
lines changed

src/FSharpLint.Core/Application/Lint.fs

+65-25
Original file line numberDiff line numberDiff line change
@@ -118,67 +118,87 @@ module Lint =
118118
{ IndentationRuleContext:Map<int,bool*int>
119119
NoTabCharactersRuleContext:(string * Range) list }
120120

121-
let runAstNodeRules (rules:RuleMetadata<AstNodeRuleConfig> []) (globalConfig:Rules.GlobalRuleConfig) typeCheckResults (filePath:string) (fileContent:string) (lines:string []) syntaxArray =
121+
type RunAstNodeRulesConfig =
122+
{
123+
Rules: RuleMetadata<AstNodeRuleConfig>[]
124+
GlobalConfig: Rules.GlobalRuleConfig
125+
TypeCheckResults: FSharpCheckFileResults option
126+
FilePath: string
127+
FileContent: string
128+
Lines: string[]
129+
SyntaxArray: AbstractSyntaxArray.Node array
130+
}
131+
132+
let runAstNodeRules (config: RunAstNodeRulesConfig) =
122133
let mutable indentationRuleState = Map.empty
123134
let mutable noTabCharactersRuleState = List.empty
124135

125136
let collect index (astNode: AbstractSyntaxArray.Node) =
126-
let getParents (depth:int) = AbstractSyntaxArray.getBreadcrumbs depth syntaxArray index
137+
let getParents (depth:int) = AbstractSyntaxArray.getBreadcrumbs depth config.SyntaxArray index
127138
let astNodeParams =
128139
{
129140
AstNode = astNode.Actual
130141
NodeHashcode = astNode.Hashcode
131142
NodeIndex = index
132-
SyntaxArray = syntaxArray
143+
SyntaxArray = config.SyntaxArray
133144
GetParents = getParents
134-
FilePath = filePath
135-
FileContent = fileContent
136-
Lines = lines
137-
CheckInfo = typeCheckResults
138-
GlobalConfig = globalConfig }
145+
FilePath = config.FilePath
146+
FileContent = config.FileContent
147+
Lines = config.Lines
148+
CheckInfo = config.TypeCheckResults
149+
GlobalConfig = config.GlobalConfig }
139150
// Build state for rules with context.
140151
indentationRuleState <- Indentation.ContextBuilder.builder indentationRuleState astNode.Actual
141152
noTabCharactersRuleState <- NoTabCharacters.ContextBuilder.builder noTabCharactersRuleState astNode.Actual
142153

143-
rules
154+
config.Rules
144155
|> Array.collect (fun rule -> runAstNodeRule rule astNodeParams)
145156

146157
// Collect suggestions for AstNode rules, and build context for following rules.
147158
let astNodeSuggestions =
148-
syntaxArray
159+
config.SyntaxArray
149160
|> Array.mapi (fun index astNode -> (index, astNode))
150161
|> Array.collect (fun (index, astNode) -> collect index astNode)
151162

152163
let context =
153164
{ IndentationRuleContext = indentationRuleState
154165
NoTabCharactersRuleContext = noTabCharactersRuleState }
155166

156-
rules |> Array.iter (fun rule -> rule.RuleConfig.Cleanup())
167+
config.Rules |> Array.iter (fun rule -> rule.RuleConfig.Cleanup())
157168
(astNodeSuggestions, context)
158169

159-
let runLineRules (lineRules:Configuration.LineRules) (globalConfig:Rules.GlobalRuleConfig) (filePath:string) (fileContent:string) (lines:string []) (context:Context) =
170+
type RunLineRulesConfig =
171+
{
172+
LineRules: Configuration.LineRules
173+
GlobalConfig: Rules.GlobalRuleConfig
174+
FilePath: string
175+
FileContent: string
176+
Lines: string[]
177+
Context: Context
178+
}
179+
let runLineRules (config: RunLineRulesConfig) =
160180
let collectErrors (line: string) (lineNumber: int) (isLastLine: bool) =
161181
let lineParams =
162-
{
182+
{
163183
LineRuleParams.Line = line
164184
LineNumber = lineNumber + 1
165185
IsLastLine = isLastLine
166-
FilePath = filePath
167-
FileContent = fileContent
168-
Lines = lines
169-
GlobalConfig = globalConfig
186+
FilePath = config.FilePath
187+
FileContent = config.FileContent
188+
Lines = config.Lines
189+
GlobalConfig = config.GlobalConfig
170190
}
171191

172192
let indentationError =
173-
lineRules.IndentationRule
174-
|> Option.map (fun rule -> runLineRuleWithContext rule context.IndentationRuleContext lineParams)
193+
config.LineRules.IndentationRule
194+
|> Option.map (fun rule -> runLineRuleWithContext rule config.Context.IndentationRuleContext lineParams)
175195

176196
let noTabCharactersError =
177-
lineRules.NoTabCharactersRule
178-
|> Option.map (fun rule -> runLineRuleWithContext rule context.NoTabCharactersRuleContext lineParams)
197+
config.LineRules.NoTabCharactersRule
198+
|> Option.map (fun rule -> runLineRuleWithContext rule config.Context.NoTabCharactersRuleContext lineParams)
179199

180200
let lineErrors =
181-
lineRules.GenericLineRules
201+
config.LineRules.GenericLineRules
182202
|> Array.collect (fun rule -> runLineRule rule lineParams)
183203

184204
[|
@@ -187,7 +207,7 @@ module Lint =
187207
lineErrors |> Array.singleton
188208
|]
189209

190-
fileContent
210+
config.FileContent
191211
|> String.toLines
192212
|> Array.collect (fun (line, lineNumber, isLastLine) -> collectErrors line lineNumber isLastLine)
193213
|> Array.concat
@@ -230,8 +250,28 @@ module Lint =
230250
let syntaxArray = AbstractSyntaxArray.astToArray fileInfo.Ast
231251

232252
// Collect suggestions for AstNode rules
233-
let (astNodeSuggestions, context) = runAstNodeRules enabledRules.AstNodeRules enabledRules.GlobalConfig fileInfo.TypeCheckResults fileInfo.File fileInfo.Text lines syntaxArray
234-
let lineSuggestions = runLineRules enabledRules.LineRules enabledRules.GlobalConfig fileInfo.File fileInfo.Text lines context
253+
let (astNodeSuggestions, context) =
254+
runAstNodeRules
255+
{
256+
Rules = enabledRules.AstNodeRules
257+
GlobalConfig = enabledRules.GlobalConfig
258+
TypeCheckResults = fileInfo.TypeCheckResults
259+
FilePath = fileInfo.File
260+
FileContent = fileInfo.Text
261+
Lines = lines
262+
SyntaxArray = syntaxArray
263+
}
264+
265+
let lineSuggestions =
266+
runLineRules
267+
{
268+
LineRules = enabledRules.LineRules
269+
GlobalConfig = enabledRules.GlobalConfig
270+
FilePath = fileInfo.File
271+
FileContent = fileInfo.Text
272+
Lines = lines
273+
Context = context
274+
}
235275

236276
[| lineSuggestions; astNodeSuggestions |]
237277
|> Array.concat

src/FSharpLint.Core/Application/Lint.fsi

+23-2
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,32 @@ module Lint =
119119
member TryGetSuccess : byref<Suggestion.LintWarning list> -> bool
120120
member TryGetFailure : byref<LintFailure> -> bool
121121

122+
type RunAstNodeRulesConfig =
123+
{
124+
Rules: RuleMetadata<AstNodeRuleConfig>[]
125+
GlobalConfig: Rules.GlobalRuleConfig
126+
TypeCheckResults: FSharpCheckFileResults option
127+
FilePath: string
128+
FileContent: string
129+
Lines: string[]
130+
SyntaxArray: AbstractSyntaxArray.Node array
131+
}
132+
122133
/// Runs all rules which take a node of the AST as input.
123-
val runAstNodeRules : RuleMetadata<AstNodeRuleConfig> [] -> Rules.GlobalRuleConfig -> FSharpCheckFileResults option -> string -> string -> string [] -> AbstractSyntaxArray.Node [] -> Suggestion.LintWarning [] * Context
134+
val runAstNodeRules : RunAstNodeRulesConfig -> Suggestion.LintWarning [] * Context
135+
136+
type RunLineRulesConfig =
137+
{
138+
LineRules: Configuration.LineRules
139+
GlobalConfig: Rules.GlobalRuleConfig
140+
FilePath: string
141+
FileContent: string
142+
Lines: string[]
143+
Context: Context
144+
}
124145

125146
/// Runs all rules which take a line of text as input.
126-
val runLineRules : LineRules -> Rules.GlobalRuleConfig -> string -> string -> string [] -> Context -> Suggestion.LintWarning []
147+
val runLineRules : RunLineRulesConfig -> Suggestion.LintWarning []
127148

128149
/// Lints an entire F# solution by linting all projects specified in the `.sln` file.
129150
val lintSolution : optionalParams:OptionalLintParameters -> solutionFilePath:string -> toolsPath:Ionide.ProjInfo.Types.ToolsPath -> LintResult

src/FSharpLint.Core/Rules/Formatting/TypePrefixing.fs

+35-17
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,25 @@ type Mode =
1616
[<RequireQualifiedAccess>]
1717
type Config = { Mode: Mode }
1818

19-
let checkTypePrefixing (config:Config) (args:AstNodeRuleParams) range typeName typeArgs isPostfix =
19+
type CheckTypePrefixingConfig =
20+
{
21+
Config: Config
22+
Args: AstNodeRuleParams
23+
Range: FSharp.Compiler.Text.Range
24+
TypeName: SynType
25+
TypeArgs: string option
26+
IsPostfix: bool
27+
}
28+
29+
let checkTypePrefixing (typePrefixingConfig: CheckTypePrefixingConfig) =
2030
let recommendPostfixErrMsg = lazy(Resources.GetString("RulesFormattingF#PostfixGenericError"))
21-
match typeName with
31+
match typePrefixingConfig.TypeName with
2232
| SynType.LongIdent lid ->
2333
let prefixSuggestion typeName =
2434
let suggestedFix = lazy(
25-
(ExpressionUtilities.tryFindTextOfRange range args.FileContent, typeArgs)
26-
||> Option.map2 (fun fromText typeArgs -> { FromText = fromText; FromRange = range; ToText = typeName + "<" + typeArgs + ">" }))
27-
{ Range = range
35+
(ExpressionUtilities.tryFindTextOfRange typePrefixingConfig.Range typePrefixingConfig.Args.FileContent, typePrefixingConfig.TypeArgs)
36+
||> Option.map2 (fun fromText typeArgs -> { FromText = fromText; FromRange = typePrefixingConfig.Range; ToText = typeName + "<" + typeArgs + ">" }))
37+
{ Range = typePrefixingConfig.Range
2838
Message = Resources.GetString("RulesFormattingGenericPrefixError")
2939
SuggestedFix = Some suggestedFix
3040
TypeChecks = [] } |> Some
@@ -38,39 +48,39 @@ let checkTypePrefixing (config:Config) (args:AstNodeRuleParams) range typeName t
3848
| "Ref" as typeName ->
3949

4050
// Prefer postfix.
41-
if not isPostfix && config.Mode <> Mode.Always
51+
if not typePrefixingConfig.IsPostfix && typePrefixingConfig.Config.Mode <> Mode.Always
4252
then
4353
let suggestedFix = lazy(
44-
(ExpressionUtilities.tryFindTextOfRange range args.FileContent, typeArgs)
45-
||> Option.map2 (fun fromText typeArgs -> { FromText = fromText; FromRange = range; ToText = typeArgs + " " + typeName }))
46-
{ Range = range
54+
(ExpressionUtilities.tryFindTextOfRange typePrefixingConfig.Range typePrefixingConfig.Args.FileContent, typePrefixingConfig.TypeArgs)
55+
||> Option.map2 (fun fromText typeArgs -> { FromText = fromText; FromRange = typePrefixingConfig.Range; ToText = typeArgs + " " + typeName }))
56+
{ Range = typePrefixingConfig.Range
4757
Message = String.Format(recommendPostfixErrMsg.Value, typeName)
4858
SuggestedFix = Some suggestedFix
4959
TypeChecks = [] } |> Some
5060
else
51-
if isPostfix && config.Mode = Mode.Always then
61+
if typePrefixingConfig.IsPostfix && typePrefixingConfig.Config.Mode = Mode.Always then
5262
prefixSuggestion typeName
5363
else
5464
None
5565

56-
| "array" when config.Mode <> Mode.Always ->
66+
| "array" when typePrefixingConfig.Config.Mode <> Mode.Always ->
5767
// Prefer special postfix (e.g. int []).
5868
let suggestedFix = lazy(
59-
(ExpressionUtilities.tryFindTextOfRange range args.FileContent, typeArgs)
60-
||> Option.map2 (fun fromText typeArgs -> { FromText = fromText; FromRange = range; ToText = typeArgs + " []" }))
61-
{ Range = range
69+
(ExpressionUtilities.tryFindTextOfRange typePrefixingConfig.Range typePrefixingConfig.Args.FileContent, typePrefixingConfig.TypeArgs)
70+
||> Option.map2 (fun fromText typeArgs -> { FromText = fromText; FromRange = typePrefixingConfig.Range; ToText = typeArgs + " []" }))
71+
{ Range = typePrefixingConfig.Range
6272
Message = Resources.GetString("RulesFormattingF#ArrayPostfixError")
6373
SuggestedFix = Some suggestedFix
6474
TypeChecks = [] } |> Some
6575

6676
| typeName ->
67-
match (isPostfix, config.Mode) with
77+
match (typePrefixingConfig.IsPostfix, typePrefixingConfig.Config.Mode) with
6878
| true, Mode.Never ->
6979
None
7080
| true, _ ->
7181
prefixSuggestion typeName
7282
| false, Mode.Never ->
73-
{ Range = range
83+
{ Range = typePrefixingConfig.Range
7484
Message = String.Format(recommendPostfixErrMsg.Value, typeName)
7585
// TODO
7686
SuggestedFix = None
@@ -84,7 +94,15 @@ let runner (config:Config) args =
8494
match args.AstNode with
8595
| AstNode.Type (SynType.App (typeName, _, typeArgs, _, _, isPostfix, range)) ->
8696
let typeArgs = typeArgsToString args.FileContent typeArgs
87-
checkTypePrefixing config args range typeName typeArgs isPostfix
97+
checkTypePrefixing
98+
{
99+
Config = config
100+
Args = args
101+
Range = range
102+
TypeName = typeName
103+
TypeArgs = typeArgs
104+
IsPostfix = isPostfix
105+
}
88106
|> Option.toArray
89107
| AstNode.Type (SynType.Array (1, _elementType, range)) when config.Mode = Mode.Always ->
90108
{ Range = range

0 commit comments

Comments
 (0)