Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/content/how-tos/rule-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ The following rules can be specified for linting.
- [FailwithBadUsage (FL0072)](rules/FL0072.html)
- [FavourReRaise (FL0073)](rules/FL0073.html)
- [FavourConsistentThis (FL0074)](rules/FL0074.html)
- [AvoidTooShortNames (FL0075)](rules/FL0075.html)
- [AvoidTooShortNaming (FL0075)](rules/FL0075.html)
- [FavourStaticEmptyFields (FL0076)](rules/FL0076.html)
- [AvoidSinglePipeOperator (FL0077)](rules/FL0077.html)
- [AsyncExceptionWithoutReturn (FL0078)](rules/FL0078.html)
Expand Down
4 changes: 2 additions & 2 deletions docs/content/how-tos/rules/FL0075.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ category: how-to
hide_menu: true
---

# AvoidTooShortNames (FL0075)
# AvoidTooShortNaming (FL0075)

*Introduced in `0.21.1`*

Expand All @@ -23,7 +23,7 @@ Use longer names for the flagged occurrences.
## Rule Settings

{
"avoidTooShortNames": {
"avoidTooShortNaming": {
"enabled": false
}
}
55 changes: 28 additions & 27 deletions src/FSharpLint.Console/Output.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,30 @@ module FSharpLint.Console.Output
open System
open FSharp.Compiler.Text
open FSharpLint.Framework
open FSharpLint.Framework.Violation

type IOutput =
/// Outputs informational text.
abstract member WriteInfo : string -> unit
/// Outputs a lint warning.
abstract member WriteWarning : Suggestion.LintWarning -> unit
/// Outputs an unexpected error in linting.
abstract member WriteError : string -> unit
/// Outputs a lint rule violation.
abstract member WriteViolationInfo : LintViolation -> unit
/// Outputs an unexpected failure when linting.
abstract member WriteFailure : string -> unit

type StandardOutput () =

let getErrorMessage (range:Range) =
let error = Resources.GetString("LintSourceError")
String.Format(error, range.StartLine, range.StartColumn)
let getRangeInfoMessage (range:Range) =
let msg = Resources.GetString "LintSourceViolation"
String.Format(msg, range.StartLine, range.StartColumn)

let highlightErrorText (range:Range) (errorLine:string) =
let highlightViolationText (range:Range) (sourceLineWithViolation:string) =
let highlightColumnLine =
if String.length errorLine = 0 then "^"
if String.length sourceLineWithViolation = 0 then "^"
else
errorLine
sourceLineWithViolation
|> Seq.mapi (fun index _ -> if index = range.StartColumn then "^" else " ")
|> Seq.reduce (+)
$"{getErrorMessage range}{Environment.NewLine}{errorLine}{Environment.NewLine}{highlightColumnLine}"
$"{getRangeInfoMessage range}{Environment.NewLine}{sourceLineWithViolation}{Environment.NewLine}{highlightColumnLine}"

let writeLine (str:string) (color:ConsoleColor) (writer:IO.TextWriter) =
let originalColour = Console.ForegroundColor
Expand All @@ -36,27 +37,27 @@ type StandardOutput () =
interface IOutput with

member _.WriteInfo (info:string) = writeLine info ConsoleColor.White Console.Out
member this.WriteWarning (warning:Suggestion.LintWarning) =
let highlightedErrorText = highlightErrorText warning.Details.Range warning.ErrorText
let ruleUrlHint = $"See https://fsprojects.github.io/FSharpLint/how-tos/rules/%s{warning.RuleIdentifier}.html"
let str = $"{warning.Details.Message}{Environment.NewLine}{highlightedErrorText}{Environment.NewLine}{ruleUrlHint}"
member this.WriteViolationInfo (violation: LintViolation) =
let highlightedViolatonText = highlightViolationText violation.Details.Range violation.SourceCodeFragment
let ruleUrlHint = $"See https://fsprojects.github.io/FSharpLint/how-tos/rules/%s{violation.RuleIdentifier}.html"
let str = $"{violation.Details.Message}{Environment.NewLine}{highlightedViolatonText}{Environment.NewLine}{ruleUrlHint}"
writeLine str ConsoleColor.Yellow Console.Out
String.replicate 80 "-" |> (this :> IOutput).WriteInfo
member _.WriteError (error:string) = writeLine error ConsoleColor.Red Console.Error
member _.WriteFailure (failure: string) = writeLine failure ConsoleColor.Red Console.Error

type MSBuildOutput () =

interface IOutput with

member _.WriteInfo (info:string) = Console.Out.WriteLine info
member _.WriteWarning (warning:Suggestion.LintWarning) =
fprintf Console.Out "%s(%d,%d,%d,%d):FSharpLint warning %s: %s"
<| warning.FilePath
<| warning.Details.Range.StartLine
<| warning.Details.Range.StartColumn
<| warning.Details.Range.EndLine
<| warning.Details.Range.EndColumn
<| warning.RuleIdentifier
<| warning.Details.Message
member _.WriteError (error:string) =
Console.Error.WriteLine $"FSharpLint error: {error}"
member _.WriteViolationInfo (violation: LintViolation) =
fprintf Console.Out "%s(%d,%d,%d,%d):FSharpLint violation %s: %s"
<| violation.FilePath
<| violation.Details.Range.StartLine
<| violation.Details.Range.StartColumn
<| violation.Details.Range.EndLine
<| violation.Details.Range.EndColumn
<| violation.RuleIdentifier
<| violation.Details.Message
member _.WriteFailure (failure: string) =
Console.Error.WriteLine $"FSharpLint failure: {failure}"
32 changes: 16 additions & 16 deletions src/FSharpLint.Console/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ with
| Lint _ -> "Runs FSharpLint against a file or a collection of files."
| Version -> "Prints current version."

// TODO: investigate erroneous warning on this type definition
// TODO: investigate erroneous violation on this type definition
// fsharplint:disable UnionDefinitionIndentation
and private LintArgs =
| [<MainCommand; Mandatory>] Target of target:string
Expand Down Expand Up @@ -89,11 +89,11 @@ let internal expandWildcard (pattern:string) =
let private parserProgress (output:Output.IOutput) = function
| Starting file ->
String.Format(Resources.GetString("ConsoleStartingFile"), file) |> output.WriteInfo
| ReachedEnd (_, warnings) ->
String.Format(Resources.GetString("ConsoleFinishedFile"), List.length warnings) |> output.WriteInfo
| ReachedEnd (_, violations) ->
String.Format(Resources.GetString("ConsoleFinishedFile"), List.length violations) |> output.WriteInfo
| Failed (file, parseException) ->
String.Format(Resources.GetString("ConsoleFailedToParseFile"), file) |> output.WriteError
output.WriteError
String.Format(Resources.GetString("ConsoleFailedToParseFile"), file) |> output.WriteFailure
output.WriteFailure
$"Exception Message:{Environment.NewLine}{parseException.Message}{Environment.NewLine}Exception Stack Trace:{Environment.NewLine}{parseException.StackTrace}{Environment.NewLine}"

/// Checks if a string contains wildcard characters.
Expand Down Expand Up @@ -130,20 +130,21 @@ let private start (arguments:ParseResults<ToolArgs>) (toolsPath:Ionide.ProjInfo.
output.WriteInfo $"Current version: {version}"
Environment.Exit 0

let handleError (str:string) =
output.WriteError str
let handleFailure (str: string) =
output.WriteFailure str
exitCode <- -1

match arguments.GetSubCommand() with
| Lint lintArgs ->

let handleLintResult = function
| LintResult.Success(warnings) ->
String.Format(Resources.GetString("ConsoleFinished"), List.length warnings)
| LintResult.Success violations ->
String.Format(Resources.GetString "ConsoleFinished", List.length violations)
|> output.WriteInfo
if not (List.isEmpty warnings) then exitCode <- -1
if not (List.isEmpty violations) then
exitCode <- -1
| LintResult.Failure(failure) ->
handleError failure.Description
handleFailure failure.Description

let lintConfig = lintArgs.TryGetResult Lint_Config

Expand All @@ -152,10 +153,9 @@ let private start (arguments:ParseResults<ToolArgs>) (toolsPath:Ionide.ProjInfo.
| Some configPath -> FromFile configPath
| None -> Default


let lintParams =
{ CancellationToken = None
ReceivedWarning = Some output.WriteWarning
ReceivedViolation = Some output.WriteViolationInfo
Configuration = configParam
ReportLinterProgress = Some (parserProgress output) }

Expand Down Expand Up @@ -183,7 +183,7 @@ let private start (arguments:ParseResults<ToolArgs>) (toolsPath:Ionide.ProjInfo.
with
| exn ->
let target = if fileType = FileType.Source then "source" else target
handleError
handleFailure
$"Lint failed while analysing %s{target}.{Environment.NewLine}Failed with: %s{exn.Message}{Environment.NewLine}Stack trace: {exn.StackTrace}"
| _ -> ()

Expand All @@ -195,9 +195,9 @@ let toolsPath = Ionide.ProjInfo.Init.init (DirectoryInfo <| Directory.GetCurrent

[<EntryPoint>]
let main argv =
let errorHandler = ProcessExiter(colorizer = function
let failureHandler = ProcessExiter(colorizer = function
| ErrorCode.HelpText -> None
| _ -> Some ConsoleColor.Red)
let parser = ArgumentParser.Create<ToolArgs>(programName = "fsharplint", errorHandler = errorHandler)
let parser = ArgumentParser.Create<ToolArgs>(programName = "fsharplint", errorHandler = failureHandler)
let parseResults = parser.ParseCommandLine argv
start parseResults toolsPath
10 changes: 5 additions & 5 deletions src/FSharpLint.Core/Application/Configuration.fs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ with

type ConventionsConfig =
{ recursiveAsyncFunction:EnabledConfig option
avoidTooShortNames:EnabledConfig option
avoidTooShortNaming:EnabledConfig option
redundantNewKeyword:EnabledConfig option
favourStaticEmptyFields:EnabledConfig option
asyncExceptionWithoutReturn:EnabledConfig option
Expand All @@ -325,7 +325,7 @@ with
Array.concat
[|
this.recursiveAsyncFunction |> Option.bind (constructRuleIfEnabled RecursiveAsyncFunction.rule) |> Option.toArray
this.avoidTooShortNames |> Option.bind (constructRuleIfEnabled AvoidTooShortNames.rule) |> Option.toArray
this.avoidTooShortNaming |> Option.bind (constructRuleIfEnabled AvoidTooShortNaming.rule) |> Option.toArray
this.redundantNewKeyword |> Option.bind (constructRuleIfEnabled RedundantNewKeyword.rule) |> Option.toArray
this.favourNonMutablePropertyInitialization |> Option.bind (constructRuleIfEnabled FavourNonMutablePropertyInitialization.rule) |> Option.toArray
this.favourReRaise |> Option.bind (constructRuleIfEnabled FavourReRaise.rule) |> Option.toArray
Expand Down Expand Up @@ -402,7 +402,7 @@ type Configuration =
PatternMatchClauseIndentation:RuleConfig<PatternMatchClauseIndentation.Config> option
PatternMatchExpressionIndentation:EnabledConfig option
RecursiveAsyncFunction:EnabledConfig option
AvoidTooShortNames:EnabledConfig option
AvoidTooShortNaming:EnabledConfig option
RedundantNewKeyword:EnabledConfig option
FavourNonMutablePropertyInitialization:EnabledConfig option
FavourReRaise:EnabledConfig option
Expand Down Expand Up @@ -497,7 +497,7 @@ with
PatternMatchClauseIndentation = None
PatternMatchExpressionIndentation = None
RecursiveAsyncFunction = None
AvoidTooShortNames = None
AvoidTooShortNaming = None
RedundantNewKeyword = None
FavourNonMutablePropertyInitialization = None
FavourReRaise = None
Expand Down Expand Up @@ -692,7 +692,7 @@ let flattenConfig (config:Configuration) =
config.PatternMatchClauseIndentation |> Option.bind (constructRuleWithConfig PatternMatchClauseIndentation.rule)
config.PatternMatchExpressionIndentation |> Option.bind (constructRuleIfEnabled PatternMatchExpressionIndentation.rule)
config.RecursiveAsyncFunction |> Option.bind (constructRuleIfEnabled RecursiveAsyncFunction.rule)
config.AvoidTooShortNames |> Option.bind (constructRuleIfEnabled AvoidTooShortNames.rule)
config.AvoidTooShortNaming |> Option.bind (constructRuleIfEnabled AvoidTooShortNaming.rule)
config.RedundantNewKeyword |> Option.bind (constructRuleIfEnabled RedundantNewKeyword.rule)
config.FavourNonMutablePropertyInitialization |> Option.bind (constructRuleIfEnabled FavourNonMutablePropertyInitialization.rule)
config.FavourReRaise |> Option.bind (constructRuleIfEnabled FavourReRaise.rule)
Expand Down
Loading
Loading