From 95cc2cf7685464fd8d3e4b0765eef9967ffc0a54 Mon Sep 17 00:00:00 2001 From: nojaf Date: Fri, 24 Jan 2020 11:52:35 +0100 Subject: [PATCH 01/12] Split up setting SpaceBeforeArgument into multiple settings. --- src/Fantomas.Tests/AttributeTests.fs | 2 +- src/Fantomas.Tests/Fantomas.Tests.fsproj | 1 + src/Fantomas.Tests/LetBindingTests.fs | 5 +- src/Fantomas.Tests/RecordTests.fs | 2 +- .../SpaceBeforeArgumentsAndParametersTests.fs | 181 ++++++++++++++++++ src/Fantomas.Tests/SpecialConstructsTests.fs | 3 +- src/Fantomas/CodePrinter.fs | 77 +++++--- src/Fantomas/Context.fs | 3 - src/Fantomas/FormatConfig.fs | 64 ++----- src/Fantomas/SourceParser.fs | 18 +- 10 files changed, 266 insertions(+), 90 deletions(-) create mode 100644 src/Fantomas.Tests/SpaceBeforeArgumentsAndParametersTests.fs diff --git a/src/Fantomas.Tests/AttributeTests.fs b/src/Fantomas.Tests/AttributeTests.fs index f730a22d48..4c56a8ea98 100644 --- a/src/Fantomas.Tests/AttributeTests.fs +++ b/src/Fantomas.Tests/AttributeTests.fs @@ -292,7 +292,7 @@ let main argv = SpaceAfterComma = false SpaceAfterSemicolon = false SpaceAroundDelimiter = false - SpaceBeforeArgument = false }) + SpaceBeforeParenthesisArgumentInLowercaseFunctionCall = false }) |> prepend newline |> should equal """ open System diff --git a/src/Fantomas.Tests/Fantomas.Tests.fsproj b/src/Fantomas.Tests/Fantomas.Tests.fsproj index f2964061ae..2acce5bef8 100644 --- a/src/Fantomas.Tests/Fantomas.Tests.fsproj +++ b/src/Fantomas.Tests/Fantomas.Tests.fsproj @@ -58,6 +58,7 @@ + diff --git a/src/Fantomas.Tests/LetBindingTests.fs b/src/Fantomas.Tests/LetBindingTests.fs index 921015b995..c603823ac2 100644 --- a/src/Fantomas.Tests/LetBindingTests.fs +++ b/src/Fantomas.Tests/LetBindingTests.fs @@ -330,8 +330,7 @@ let ``line comment before return type info should indent before colon, 565`` () """ ({ config with SpaceAfterComma = false SpaceAfterSemicolon = false - SpaceAroundDelimiter = false - SpaceBeforeArgument = false }) + SpaceAroundDelimiter = false }) |> prepend newline |> should equal """ module Bar = @@ -350,7 +349,7 @@ let ``has symbol in signature requires paren, 564`` () = SpaceAfterComma = false SpaceAfterSemicolon = false SpaceAroundDelimiter = false - SpaceBeforeArgument = false }) + SpaceBeforeParenthesisInLowercaseFunctionDefinition = false }) |> prepend newline |> should equal """ module Bar = diff --git a/src/Fantomas.Tests/RecordTests.fs b/src/Fantomas.Tests/RecordTests.fs index fac78775f4..8aa40e94e7 100644 --- a/src/Fantomas.Tests/RecordTests.fs +++ b/src/Fantomas.Tests/RecordTests.fs @@ -284,7 +284,7 @@ let ``meaningful space should be preserved, 353`` () = |> should equal """to'.WithCommon(fun o' -> { dotnetOptions o' with WorkingDirectory = Path.getFullName "RegressionTesting/issue29" - Verbosity = SomeDotNet.Verbosity.Minimal }).WithParameters + Verbosity = Some DotNet.Verbosity.Minimal }).WithParameters """ [] diff --git a/src/Fantomas.Tests/SpaceBeforeArgumentsAndParametersTests.fs b/src/Fantomas.Tests/SpaceBeforeArgumentsAndParametersTests.fs new file mode 100644 index 0000000000..9a1bd5b04b --- /dev/null +++ b/src/Fantomas.Tests/SpaceBeforeArgumentsAndParametersTests.fs @@ -0,0 +1,181 @@ +module Fantomas.Tests.SpaceBeforeArgumentsAndParametersTests + +open NUnit.Framework +open FsUnit +open Fantomas.Tests.TestHelper + +/// Space before () in Uppercase function call + +[] +let ``default config should not add space before unit in uppercase function call`` () = + formatSourceString false "let value = MyFunction()" config + |> should equal """let value = MyFunction() +""" + +[] +let ``SpaceBeforeUnitArgumentInUppercaseFunctionCall should add space before unit in uppercase function call`` () = + formatSourceString false "let value = MyFunction()" ({ config with SpaceBeforeUnitArgumentInUppercaseFunctionCall = true }) + |> should equal """let value = MyFunction () +""" + +[] +let ``SpaceBeforeUnitArgumentInUppercaseFunctionCall should add space before unit in chained uppercase function call`` () = + formatSourceString false "let value = person.ToString()" ({ config with SpaceBeforeUnitArgumentInUppercaseFunctionCall = true }) + |> should equal """let value = person.ToString () +""" + +/// Space before () in lowercase function call + +[] +let ``default config should not add space before unit in lowercase function call`` () = + formatSourceString false "let value = myFunction()" config + |> should equal """let value = myFunction() +""" + +[] +let ``SpaceBeforeUnitArgumentInLowercaseFunctionCall should add space before unit in lowercase function call`` () = + formatSourceString false "let value = myFunction()" ({ config with SpaceBeforeUnitArgumentInLowercaseFunctionCall = true }) + |> should equal """let value = myFunction () +""" + +// Exception to the rule + +[] +let ``SpaceBeforeUnitArgumentInUppercaseFunctionCall and SpaceBeforeUnitArgumentInLowercaseFunctionCall should not have impact when member is called after unit`` () = + formatSourceString false """let v1 = myFunction().Member +let v2 = OtherFunction().Member +""" ({ config with + SpaceBeforeUnitArgumentInUppercaseFunctionCall = true + SpaceBeforeUnitArgumentInLowercaseFunctionCall = true }) + |> prepend newline + |> should equal """ +let v1 = myFunction().Member +let v2 = OtherFunction().Member +""" + +// Space before parenthesis (a+b) in Uppercase function call + +[] +let ``default config should not add space before parenthesis in uppercase function call`` () = + formatSourceString false "let value = MyFunction(a+b)" config + |> should equal """let value = MyFunction(a + b) +""" + +[] +let ``SpaceBeforeParenthesisArgumentInUppercaseFunctionCall should add space before parenthesis in uppercase function call`` () = + formatSourceString false "let value = MyFunction(a+b)" ({ config with SpaceBeforeParenthesisArgumentInUppercaseFunctionCall = true }) + |> should equal """let value = MyFunction (a + b) +""" + +// Space before parenthesis (a+b) in lowercase function call + +[] +let ``default config should add space before parenthesis in lowercase function call`` () = + formatSourceString false "let value = myFunction(a+b)" config + |> should equal """let value = myFunction (a + b) +""" + +[] +let ``SpaceBeforeParenthesisArgumentInUppercaseFunctionCall = false, should not add space before parenthesis in lowercase function call`` () = + formatSourceString false "let value = myFunction(a+b)" ({ config with SpaceBeforeParenthesisArgumentInLowercaseFunctionCall = false }) + |> should equal """let value = myFunction(a + b) +""" + +// Space before unit in Uppercase function signature + +[] +let ``default config should not add space before unit in uppercase function definition`` () = + formatSourceString false "let Value () = x" config + |> should equal """let Value() = x +""" + +[] +let ``SpaceBeforeUnitParameterInUppercaseFunctionDefinition config should not add space before unit in uppercase function definition`` () = + formatSourceString false "let Value() = x" ({ config with SpaceBeforeUnitParameterInUppercaseFunctionDefinition = true }) + |> should equal """let Value () = x +""" + +[] +let ``default config should not add space after empty constructor of class`` () = + formatSourceString false """type Person () = + class end +""" config + |> prepend newline + |> should equal """ +type Person() = + class + end +""" + +[] +let ``default config should not add space after constructor of class`` () = + formatSourceString false """type Person (a:int) = + class end +""" config + |> prepend newline + |> should equal """ +type Person(a: int) = + class + end +""" + +[] +let ``SpaceBeforeUnitParameterInUppercaseFunctionDefinition should add space after constructor of class`` () = + formatSourceString false """type Person () = + class end + +type Animal (length:int) = + class end +""" ({ config with SpaceBeforeUnitParameterInUppercaseFunctionDefinition = true }) + |> prepend newline + |> should equal """ +type Person() = + class + end + +type Animal(length: int) = + class + end +""" + +// Space before unit in lowercase function definition + +[] +let ``default config should not add space before unit in lowercase function definition`` () = + formatSourceString false "let value () = x" config + |> should equal """let value() = x +""" + +[] +let ``SpaceBeforeUnitParameterInLowercaseFunctionDefinition config should add space before unit in lowercase function definition`` () = + formatSourceString false "let value() = x" ({ config with SpaceBeforeUnitParameterInLowercaseFunctionDefinition = true }) + |> should equal """let value () = x +""" + +// Space before parenthesis (a+b) in Uppercase function definition + +[] +let ``default config should not add space before parenthesis in uppercase function definition`` () = + formatSourceString false "let Value (a:int) = x" config + |> should equal """let Value(a: int) = x +""" + +[] +let ``SpaceBeforeParenthesisInUppercaseFunctionDefinition config should add space before parenthesis in uppercase function definition`` () = + formatSourceString false "let Value(a:int) = x" ({ config with SpaceBeforeParenthesisInUppercaseFunctionDefinition = true }) + |> should equal """let Value (a: int) = x +""" + +// Space before parenthesis (a+b) in lowercase function definition + +[] +let ``default config should not add space before parenthesis in lowercase function definition`` () = + formatSourceString false "let value (a:int) = x" config + |> should equal """let value (a: int) = x +""" + +[] +let ``SpaceBeforeParenthesisInLowercaseFunctionDefinition = false, should not add space before parenthesis in lowercase function definition`` () = + formatSourceString false "let value(a:int) = x" ({ config with SpaceBeforeParenthesisInLowercaseFunctionDefinition = false }) + |> should equal """let value(a: int) = x +""" \ No newline at end of file diff --git a/src/Fantomas.Tests/SpecialConstructsTests.fs b/src/Fantomas.Tests/SpecialConstructsTests.fs index 0bf10f21cd..09e878153e 100644 --- a/src/Fantomas.Tests/SpecialConstructsTests.fs +++ b/src/Fantomas.Tests/SpecialConstructsTests.fs @@ -23,8 +23,7 @@ x.G[].TryFind 3 """ ({ config with SpaceAfterComma = false SpaceAfterSemicolon = false - SpaceAroundDelimiter = false - SpaceBeforeArgument = false }) + SpaceAroundDelimiter = false }) |> prepend newline |> should equal """ type F = diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index 323604ae7c..63978ff345 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -41,28 +41,39 @@ type ASTContext = HasVerticalBar = false; IsUnionField = false IsFirstTypeParam = false; IsInsideDotGet = false } -let rec addSpaceBeforeParensInFunCall functionOrMethod arg = +let rec addSpaceBeforeParensInFunCall functionOrMethod arg (ctx:Context) = match functionOrMethod, arg with - | _, ConstExpr(Const "()", _) -> - false - | SynExpr.LongIdent(_, LongIdentWithDots s, _, _), _ -> - let parts = s.Split '.' - not <| Char.IsUpper parts.[parts.Length - 1].[0] + | SynExpr.TypeApp(e, _, _, _, _, _, _), _ -> + addSpaceBeforeParensInFunCall e arg ctx + | UppercaseSynExpr, ConstExpr(Const "()", _) -> + ctx.Config.SpaceBeforeUnitArgumentInUppercaseFunctionCall + | LowercaseSynExpr, ConstExpr(Const "()", _) -> + ctx.Config.SpaceBeforeUnitArgumentInLowercaseFunctionCall | SynExpr.Ident(_), SynExpr.Ident(_) -> true - | SynExpr.Ident(Ident s), _ -> - not <| Char.IsUpper s.[0] - | SynExpr.TypeApp(e, _, _, _, _, _, _), _ -> - addSpaceBeforeParensInFunCall e arg + | UppercaseSynExpr, Paren(_) -> + ctx.Config.SpaceBeforeParenthesisArgumentInUppercaseFunctionCall + | LowercaseSynExpr, Paren(_) -> + ctx.Config.SpaceBeforeParenthesisArgumentInLowercaseFunctionCall | _ -> true -let addSpaceBeforeParensInFunDef functionOrMethod args = +let addSpaceBeforeParensInFunDef (functionOrMethod:string) args (ctx:Context) = + let isLastPartUppercase = + let parts = functionOrMethod.Split '.' + Char.IsUpper parts.[parts.Length - 1].[0] + match functionOrMethod, args with - | _, PatParen (PatConst(Const "()", _)) -> false | "new", _ -> false - | (s:string), _ -> - let parts = s.Split '.' - not <| Char.IsUpper parts.[parts.Length - 1].[0] + | _, PatParen (PatConst(Const "()", _)) -> + if isLastPartUppercase + then ctx.Config.SpaceBeforeUnitParameterInUppercaseFunctionDefinition + else ctx.Config.SpaceBeforeUnitParameterInLowercaseFunctionDefinition + + | _, PatParen(_) -> + if isLastPartUppercase + then ctx.Config.SpaceBeforeParenthesisInUppercaseFunctionDefinition + else ctx.Config.SpaceBeforeParenthesisInLowercaseFunctionDefinition + | (_:string), _ -> not isLastPartUppercase | _ -> true let rec genParsedInput astContext = function @@ -1049,15 +1060,18 @@ and genExpr astContext synExpr = // Unlike infix app, function application needs a level of indentation | App(e1, [e2]) -> - let hasPar = hasParenthesis e2 - let addSpaceBefore = addSpaceBeforeParensInFunCall e1 e2 - atCurrentColumn (genExpr astContext e1 +> - ifElse (not astContext.IsInsideDotGet) - (ifElse hasPar - (ifElse addSpaceBefore sepBeforeArg sepNone) - sepSpace) - sepNone - +> indent +> (ifElse (not hasPar && addSpaceBefore) sepSpace sepNone) +> appNlnFun e2 (genExpr astContext e2) +> unindent) + fun (ctx:Context) -> + let hasPar = hasParenthesis e2 + let addSpaceBefore = addSpaceBeforeParensInFunCall e1 e2 ctx + let genApp = + atCurrentColumn (genExpr astContext e1 +> + ifElse (not astContext.IsInsideDotGet) + (ifElse hasPar + (ifElse addSpaceBefore sepSpace sepNone) + sepSpace) + sepNone + +> indent +> (ifElse (not hasPar && addSpaceBefore) sepSpace sepNone) +> appNlnFun e2 (genExpr astContext e2) +> unindent) + genApp ctx // Always spacing in multiple arguments | App(e, es) -> @@ -2170,24 +2184,25 @@ and genPat astContext pat = (genPat astContext p +> sepColon +> genType astContext false t) | PatNamed(ao, PatNullary PatWild, s) -> opt sepSpace ao genAccess +> infixOperatorFromTrivia pat.Range s | PatNamed(ao, p, s) -> opt sepSpace ao genAccess +> genPat astContext p -- sprintf " as %s" s - | PatLongIdent(ao, s, ps, tpso) -> + | PatLongIdent(ao, s, ps, tpso) -> let aoc = opt sepSpace ao genAccess let tpsoc = opt sepNone tpso (fun (ValTyparDecls(tds, _, tcs)) -> genTypeParamPostfix astContext tds tcs) // Override escaped new keyword let s = if s = "``new``" then "new" else s + match ps with | [] -> aoc -- s +> tpsoc - | [(_, PatTuple [p1; p2])] when s = "(::)" -> + | [(_, PatTuple [p1; p2])] when s = "(::)" -> aoc +> genPat astContext p1 -- " :: " +> genPat astContext p2 | [(ido, p) as ip] -> - aoc +> infixOperatorFromTrivia pat.Range s +> tpsoc +> - ifElse (hasParenInPat p || Option.isSome ido) (ifElse (addSpaceBeforeParensInFunDef s p) sepBeforeArg sepNone) sepSpace + aoc +> infixOperatorFromTrivia pat.Range s +> tpsoc +> + ifElse (hasParenInPat p || Option.isSome ido) (ifElseCtx (addSpaceBeforeParensInFunDef s p) sepSpace sepNone) sepSpace +> ifElse (Option.isSome ido) (sepOpenT +> genPatWithIdent astContext ip +> sepCloseT) (genPatWithIdent astContext ip) // This pattern is potentially long - | ps -> + | ps -> let hasBracket = ps |> Seq.map fst |> Seq.exists Option.isSome - atCurrentColumn (aoc -- s +> tpsoc +> sepSpace - +> ifElse hasBracket sepOpenT sepNone + atCurrentColumn (aoc -- s +> tpsoc +> sepSpace + +> ifElse hasBracket sepOpenT sepNone +> colAutoNlnSkip0 (ifElse hasBracket sepSemi sepSpace) ps (genPatWithIdent astContext) +> ifElse hasBracket sepCloseT sepNone) diff --git a/src/Fantomas/Context.fs b/src/Fantomas/Context.fs index 6aaf24fd62..ef19c0083a 100644 --- a/src/Fantomas/Context.fs +++ b/src/Fantomas/Context.fs @@ -478,9 +478,6 @@ let internal sepSemiNln (ctx : Context) = // sepNln part is essential to indentation if ctx.Config.SemicolonAtEndOfLine then (!- ";" +> sepNln) ctx else sepNln ctx -let internal sepBeforeArg (ctx : Context) = - if ctx.Config.SpaceBeforeArgument then str " " ctx else str "" ctx - /// Conditional indentation on with keyword let internal indentOnWith (ctx : Context) = if ctx.Config.IndentOnTryWith then indent ctx else ctx diff --git a/src/Fantomas/FormatConfig.fs b/src/Fantomas/FormatConfig.fs index 0a46536459..994045c227 100644 --- a/src/Fantomas/FormatConfig.fs +++ b/src/Fantomas/FormatConfig.fs @@ -15,7 +15,14 @@ type FormatConfig = /// The column where we break to new lines PageWidth : Num SemicolonAtEndOfLine : bool - SpaceBeforeArgument : bool + SpaceBeforeUnitArgumentInUppercaseFunctionCall : bool + SpaceBeforeUnitArgumentInLowercaseFunctionCall : bool + SpaceBeforeParenthesisArgumentInUppercaseFunctionCall : bool + SpaceBeforeParenthesisArgumentInLowercaseFunctionCall : bool + SpaceBeforeUnitParameterInUppercaseFunctionDefinition : bool + SpaceBeforeUnitParameterInLowercaseFunctionDefinition : bool + SpaceBeforeParenthesisInUppercaseFunctionDefinition : bool + SpaceBeforeParenthesisInLowercaseFunctionDefinition : bool SpaceBeforeColon : bool SpaceAfterComma : bool SpaceAfterSemicolon : bool @@ -32,7 +39,14 @@ type FormatConfig = { IndentSpaceNum = 4 PageWidth = 120 SemicolonAtEndOfLine = false - SpaceBeforeArgument = true + SpaceBeforeUnitArgumentInUppercaseFunctionCall = false + SpaceBeforeUnitArgumentInLowercaseFunctionCall = false + SpaceBeforeParenthesisArgumentInUppercaseFunctionCall = false + SpaceBeforeParenthesisArgumentInLowercaseFunctionCall = true + SpaceBeforeUnitParameterInUppercaseFunctionDefinition = false + SpaceBeforeUnitParameterInLowercaseFunctionDefinition = false + SpaceBeforeParenthesisInUppercaseFunctionDefinition = false + SpaceBeforeParenthesisInLowercaseFunctionDefinition = true SpaceBeforeColon = false SpaceAfterComma = true SpaceAfterSemicolon = true @@ -43,52 +57,6 @@ type FormatConfig = MaxIfThenElseShortWidth = 40 StrictMode = false } - static member create(indentSpaceNum, pageWith, semicolonAtEndOfLine, - spaceBeforeArgument, spaceBeforeColon, spaceAfterComma, - spaceAfterSemicolon, indentOnTryWith, reorderOpenDeclaration) = - { FormatConfig.Default with - IndentSpaceNum = indentSpaceNum; - PageWidth = pageWith; - SemicolonAtEndOfLine = semicolonAtEndOfLine; - SpaceBeforeArgument = spaceBeforeArgument; - SpaceBeforeColon = spaceBeforeColon; - SpaceAfterComma = spaceAfterComma; - SpaceAfterSemicolon = spaceAfterSemicolon; - IndentOnTryWith = indentOnTryWith; - ReorderOpenDeclaration = reorderOpenDeclaration } - - static member create(indentSpaceNum, pageWith, semicolonAtEndOfLine, - spaceBeforeArgument, spaceBeforeColon, spaceAfterComma, - spaceAfterSemicolon, indentOnTryWith, reorderOpenDeclaration, spaceAroundDelimiter) = - { FormatConfig.Default with - IndentSpaceNum = indentSpaceNum; - PageWidth = pageWith; - SemicolonAtEndOfLine = semicolonAtEndOfLine; - SpaceBeforeArgument = spaceBeforeArgument; - SpaceBeforeColon = spaceBeforeColon; - SpaceAfterComma = spaceAfterComma; - SpaceAfterSemicolon = spaceAfterSemicolon; - IndentOnTryWith = indentOnTryWith; - ReorderOpenDeclaration = reorderOpenDeclaration; - SpaceAroundDelimiter = spaceAroundDelimiter } - - static member create(indentSpaceNum, pageWith, semicolonAtEndOfLine, - spaceBeforeArgument, spaceBeforeColon, spaceAfterComma, - spaceAfterSemicolon, indentOnTryWith, reorderOpenDeclaration, - spaceAroundDelimiter, strictMode) = - { FormatConfig.Default with - IndentSpaceNum = indentSpaceNum; - PageWidth = pageWith; - SemicolonAtEndOfLine = semicolonAtEndOfLine; - SpaceBeforeArgument = spaceBeforeArgument; - SpaceBeforeColon = spaceBeforeColon; - SpaceAfterComma = spaceAfterComma; - SpaceAfterSemicolon = spaceAfterSemicolon; - IndentOnTryWith = indentOnTryWith; - ReorderOpenDeclaration = reorderOpenDeclaration; - SpaceAroundDelimiter = spaceAroundDelimiter; - StrictMode = strictMode } - type FormatConfigFileParseResult = | Success of FormatConfig | PartialSuccess of config: FormatConfig * warnings: string list diff --git a/src/Fantomas/SourceParser.fs b/src/Fantomas/SourceParser.fs index 74c377f21d..cb12820776 100644 --- a/src/Fantomas/SourceParser.fs +++ b/src/Fantomas/SourceParser.fs @@ -1304,4 +1304,20 @@ let getRangesFromAttributesFromSynMemberDefinition (mdn: SynMemberDefn) = match mdn with | SynMemberDefn.Member(mb,_) -> getRangesFromAttributesFromSynBinding mb | SynMemberDefn.AbstractSlot(valSig, _, _) -> getRangesFromAttributesFromSynValSig valSig - | _ -> [] \ No newline at end of file + | _ -> [] + +let (|UppercaseSynExpr|LowercaseSynExpr|) (synExpr:SynExpr) = + let upperOrLower (v: string) = + let isUpper = Seq.tryHead v |> Option.map (Char.IsUpper) |> Option.defaultValue false + if isUpper then UppercaseSynExpr else LowercaseSynExpr + + match synExpr with + | SynExpr.Ident(Ident(id)) -> upperOrLower id + + | SynExpr.LongIdent(_, LongIdentWithDots lid, _, _) -> + let lastPart = Array.tryLast (lid.Split('.')) + match lastPart with + | Some lp -> upperOrLower lp + | None -> LowercaseSynExpr + + | _ -> failwithf "cannot determine if synExpr %A is uppercase or lowercase" synExpr From 3040e39cc168cf365843e1508721d9faba3a3376 Mon Sep 17 00:00:00 2001 From: nojaf Date: Fri, 24 Jan 2020 12:08:53 +0100 Subject: [PATCH 02/12] Print warning for --noSpaceBeforeArgument. --- src/Fantomas.CoreGlobalTool/Arg.fs | 153 ------------------------- src/Fantomas.CoreGlobalTool/Program.fs | 14 ++- 2 files changed, 10 insertions(+), 157 deletions(-) delete mode 100644 src/Fantomas.CoreGlobalTool/Arg.fs diff --git a/src/Fantomas.CoreGlobalTool/Arg.fs b/src/Fantomas.CoreGlobalTool/Arg.fs deleted file mode 100644 index 541888a2be..0000000000 --- a/src/Fantomas.CoreGlobalTool/Arg.fs +++ /dev/null @@ -1,153 +0,0 @@ -(* - -Copyright 2005-2009 Microsoft Corporation - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. - -*) - -namespace Microsoft.FSharp.Text.Args -open System.Reflection - -type ArgType = - | ClearArg of bool ref - | FloatArg of (float -> unit) - | IntArg of (int -> unit) - | RestArg of (string -> unit) - | SetArg of bool ref - | StringArg of (string -> unit) - | UnitArg of (unit -> unit) - static member Clear r = ClearArg r - static member Float r = FloatArg r - static member Int r = IntArg r - static member Rest r = RestArg r - static member Set r = SetArg r - static member String r = StringArg r - static member Unit r = UnitArg r - - -type ArgInfo (name,action,help) = - member x.Name = name - member x.ArgType = action - member x.HelpText = help - -exception Bad of string -exception HelpText of string - -[] -type ArgParser() = - static let getUsage specs u = - let sbuf = System.Text.StringBuilder 100 - let pstring (s:string) = sbuf.Append s |> ignore - let pendline s = pstring s; pstring "\n" - pendline u; - List.iter (fun (arg:ArgInfo) -> - match arg.Name, arg.ArgType, arg.HelpText with - | (s, (UnitArg _ | SetArg _ | ClearArg _), helpText) -> pstring "\t"; pstring s; pstring ": "; pendline helpText - | (s, StringArg _, helpText) -> pstring "\t"; pstring s; pstring " : "; pendline helpText - | (s, IntArg _, helpText) -> pstring "\t"; pstring s; pstring " : "; pendline helpText - | (s, FloatArg _, helpText) -> pstring "\t"; pstring s; pstring " : "; pendline helpText - | (s, RestArg _, helpText) -> pstring "\t"; pstring s; pstring " ...: "; pendline helpText) - specs; - pstring "\t"; pstring "--help"; pstring ": "; pendline "display this list of options"; - pstring "\t"; pstring "-help"; pstring ": "; pendline "display this list of options"; - sbuf.ToString() - - - static member ParsePartial(cursor,argv,argSpecs:seq,?other,?usageText) = - let other = defaultArg other (fun _ -> ()) - let usageText = defaultArg usageText "" - let nargs = Array.length argv - incr cursor; - let argSpecs = argSpecs |> Seq.toList - let specs = argSpecs |> List.map (fun (arg:ArgInfo) -> arg.Name, arg.ArgType) - while !cursor < nargs do - let arg = argv.[!cursor] - let rec findMatchingArg args = - match args with - | ((s, action) :: _) when s = arg -> - let getSecondArg () = - if !cursor + 1 >= nargs then - raise(Bad("option "+s+" needs an argument.\n"+getUsage argSpecs usageText)); - argv.[!cursor+1] - - match action with - | UnitArg f -> - f (); - incr cursor - | SetArg f -> - f := true; - incr cursor - | ClearArg f -> - f := false; - incr cursor - | StringArg f-> - let arg2 = getSecondArg() - f arg2; - cursor := !cursor + 2 - | IntArg f -> - let arg2 = getSecondArg () - let arg2 = try int32 arg2 with _ -> raise(Bad(getUsage argSpecs usageText)) in - f arg2; - cursor := !cursor + 2; - | FloatArg f -> - let arg2 = getSecondArg() - let arg2 = try float arg2 with _ -> raise(Bad(getUsage argSpecs usageText)) in - f arg2; - cursor := !cursor + 2; - | RestArg f -> - incr cursor; - while !cursor < nargs do - f (argv.[!cursor]); - incr cursor; - - | (_ :: more) -> findMatchingArg more - | [] -> - if arg = "--version" then - let version = - Assembly.GetExecutingAssembly().GetCustomAttribute() - |> Option.ofObj - |> Option.map (fun a -> a.InformationalVersion) - |> Option.defaultValue (Assembly.GetExecutingAssembly().GetName().Version.ToString()) - raise (HelpText (sprintf "Fantomas %A" version)) - - if arg = "-help" || arg = "--help" || arg = "/help" || arg = "/help" || arg = "/?" then - raise (HelpText (getUsage argSpecs usageText)) - // Note: for '/abc/def' does not count as an argument - // Note: '/abc' does - elif arg.Length>0 && (arg.[0] = '-' || (arg.[0] = '/' && not (arg.Length > 1 && arg.[1..].Contains ("/")))) then - raise (Bad ("unrecognized argument: "+ arg + "\n" + getUsage argSpecs usageText)) - else - other arg; - incr cursor - findMatchingArg specs - - static member Usage (specs,?usage) = - let usage = defaultArg usage "" - System.Console.Error.WriteLine (getUsage (Seq.toList specs) usage) - - #if FX_NO_COMMAND_LINE_ARGS - #else - static member Parse (specs,?other,?usageText) = - let current = ref 0 - let argv = System.Environment.GetCommandLineArgs() - try ArgParser.ParsePartial (current, argv, specs, ?other=other, ?usageText=usageText) - with - | Bad h - | HelpText h -> - System.Console.Error.WriteLine h; - System.Console.Error.Flush(); - System.Environment.Exit(1); - | _ -> - reraise() - #endif diff --git a/src/Fantomas.CoreGlobalTool/Program.fs b/src/Fantomas.CoreGlobalTool/Program.fs index 5d8d36e9f8..734213d700 100644 --- a/src/Fantomas.CoreGlobalTool/Program.fs +++ b/src/Fantomas.CoreGlobalTool/Program.fs @@ -122,6 +122,8 @@ let private writeInColor consoleColor (content:string) = Console.ForegroundColor <- consoleColor Console.WriteLine(content) Console.ForegroundColor <- currentColor +let private writeError = writeInColor ConsoleColor.DarkRed +let private writeWarning = writeInColor ConsoleColor.DarkYellow [] let main argv = @@ -172,11 +174,11 @@ let main argv = match configResult with | Success s -> s | PartialSuccess (ps, warnings) -> - List.iter (writeInColor ConsoleColor.DarkYellow) warnings + List.iter writeWarning warnings ps | Failure e -> - writeInColor ConsoleColor.DarkRed "Couldn't process one or more Fantomas configuration files, falling back to the default configuration" - writeInColor ConsoleColor.DarkRed (e.ToString()) + writeError "Couldn't process one or more Fantomas configuration files, falling back to the default configuration" + writeError (e.ToString()) FormatConfig.Default ) |> Option.defaultValue FormatConfig.Default @@ -197,7 +199,11 @@ let main argv = | Indent i -> { acc with IndentSpaceNum = i } | PageWidth pw -> { acc with PageWidth = pw } | SemicolonEOL -> { acc with SemicolonAtEndOfLine = true } - | NoSpaceBeforeArgument -> { acc with SpaceBeforeArgument = false } + | NoSpaceBeforeArgument -> + writeWarning "--noSpaceBeforeArgument has been split up into multiple settings." + writeWarning "These can be configured using a fantomas-config.json file." + writeWarning "Check out https://github.com/fsprojects/fantomas/blob/master/docs/Documentation.md for more information." + acc | SpaceBeforeColon -> { acc with SpaceBeforeColon = true } | NoSpaceAfterComma -> { acc with SpaceAfterComma = false } | NoSpaceAfterSemiColon -> { acc with SpaceAfterSemicolon = false } From 446ddfc1176e577d9dbe1403c27392c7244ae311 Mon Sep 17 00:00:00 2001 From: nojaf Date: Fri, 24 Jan 2020 12:11:59 +0100 Subject: [PATCH 03/12] Update SpaceBeforeArgument in json schema --- src/Fantomas/Context.fs | 2 +- src/Fantomas/schema.json | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Fantomas/Context.fs b/src/Fantomas/Context.fs index ef19c0083a..0b69b43118 100644 --- a/src/Fantomas/Context.fs +++ b/src/Fantomas/Context.fs @@ -336,7 +336,7 @@ let internal sepSpace = // TODO: this is inefficient - maybe remember last char written? fun (ctx: Context) -> if (not ctx.WriterInitModel.IsDummy && let s = dump ctx in s = "" || s.EndsWith " " || s.EndsWith Environment.NewLine) then ctx - else (!- " ") ctx + else (!- " ") ctx let internal sepNln = !+ "" let internal sepStar = !- " * " let internal sepEq = !- " =" diff --git a/src/Fantomas/schema.json b/src/Fantomas/schema.json index 4202733adb..272063469b 100644 --- a/src/Fantomas/schema.json +++ b/src/Fantomas/schema.json @@ -11,7 +11,28 @@ "SemicolonAtEndOfLine": { "type": "boolean" }, - "SpaceBeforeArgument": { + "SpaceBeforeUnitArgumentInUppercaseFunctionCall": { + "type": "boolean" + }, + "SpaceBeforeUnitArgumentInLowercaseFunctionCall": { + "type": "boolean" + }, + "SpaceBeforeParenthesisArgumentInUppercaseFunctionCall": { + "type": "boolean" + }, + "SpaceBeforeParenthesisArgumentInLowercaseFunctionCall": { + "type": "boolean" + }, + "SpaceBeforeUnitParameterInUppercaseFunctionDefinition": { + "type": "boolean" + }, + "SpaceBeforeUnitParameterInLowercaseFunctionDefinition": { + "type": "boolean" + }, + "SpaceBeforeParenthesisInUppercaseFunctionDefinition": { + "type": "boolean" + }, + "SpaceBeforeParenthesisInLowercaseFunctionDefinition": { "type": "boolean" }, "SpaceBeforeColon": { From 36093e158ad3fc114b7d6ba0ebc52e7437b7bc81 Mon Sep 17 00:00:00 2001 From: nojaf Date: Fri, 24 Jan 2020 15:43:31 +0100 Subject: [PATCH 04/12] Adding extra settings for Class constructors --- src/Fantomas.Tests/AttributeTests.fs | 2 +- src/Fantomas.Tests/LetBindingTests.fs | 2 +- .../SpaceBeforeArgumentsAndParametersTests.fs | 184 +++++++++++++----- src/Fantomas/CodePrinter.fs | 33 +++- src/Fantomas/FormatConfig.fs | 24 ++- src/Fantomas/SourceParser.fs | 5 + 6 files changed, 182 insertions(+), 68 deletions(-) diff --git a/src/Fantomas.Tests/AttributeTests.fs b/src/Fantomas.Tests/AttributeTests.fs index 4c56a8ea98..e6096511bf 100644 --- a/src/Fantomas.Tests/AttributeTests.fs +++ b/src/Fantomas.Tests/AttributeTests.fs @@ -292,7 +292,7 @@ let main argv = SpaceAfterComma = false SpaceAfterSemicolon = false SpaceAroundDelimiter = false - SpaceBeforeParenthesisArgumentInLowercaseFunctionCall = false }) + SpaceBeforeParenthesesArgumentInLowercaseFunctionCall = false }) |> prepend newline |> should equal """ open System diff --git a/src/Fantomas.Tests/LetBindingTests.fs b/src/Fantomas.Tests/LetBindingTests.fs index c603823ac2..8726ee7844 100644 --- a/src/Fantomas.Tests/LetBindingTests.fs +++ b/src/Fantomas.Tests/LetBindingTests.fs @@ -349,7 +349,7 @@ let ``has symbol in signature requires paren, 564`` () = SpaceAfterComma = false SpaceAfterSemicolon = false SpaceAroundDelimiter = false - SpaceBeforeParenthesisInLowercaseFunctionDefinition = false }) + SpaceBeforeParenthesesInLowercaseFunctionDefinition = false }) |> prepend newline |> should equal """ module Bar = diff --git a/src/Fantomas.Tests/SpaceBeforeArgumentsAndParametersTests.fs b/src/Fantomas.Tests/SpaceBeforeArgumentsAndParametersTests.fs index 9a1bd5b04b..01331e7416 100644 --- a/src/Fantomas.Tests/SpaceBeforeArgumentsAndParametersTests.fs +++ b/src/Fantomas.Tests/SpaceBeforeArgumentsAndParametersTests.fs @@ -53,31 +53,31 @@ let v1 = myFunction().Member let v2 = OtherFunction().Member """ -// Space before parenthesis (a+b) in Uppercase function call +// Space before parentheses (a+b) in Uppercase function call [] -let ``default config should not add space before parenthesis in uppercase function call`` () = +let ``default config should not add space before parentheses in uppercase function call`` () = formatSourceString false "let value = MyFunction(a+b)" config |> should equal """let value = MyFunction(a + b) """ [] -let ``SpaceBeforeParenthesisArgumentInUppercaseFunctionCall should add space before parenthesis in uppercase function call`` () = - formatSourceString false "let value = MyFunction(a+b)" ({ config with SpaceBeforeParenthesisArgumentInUppercaseFunctionCall = true }) +let ``SpaceBeforeParenthesisArgumentInUppercaseFunctionCall should add space before parentheses in uppercase function call`` () = + formatSourceString false "let value = MyFunction(a+b)" ({ config with SpaceBeforeParenthesesArgumentInUppercaseFunctionCall = true }) |> should equal """let value = MyFunction (a + b) """ -// Space before parenthesis (a+b) in lowercase function call +// Space before parentheses (a+b) in lowercase function call [] -let ``default config should add space before parenthesis in lowercase function call`` () = +let ``default config should add space before parentheses in lowercase function call`` () = formatSourceString false "let value = myFunction(a+b)" config |> should equal """let value = myFunction (a + b) """ [] -let ``SpaceBeforeParenthesisArgumentInUppercaseFunctionCall = false, should not add space before parenthesis in lowercase function call`` () = - formatSourceString false "let value = myFunction(a+b)" ({ config with SpaceBeforeParenthesisArgumentInLowercaseFunctionCall = false }) +let ``SpaceBeforeParenthesisArgumentInUppercaseFunctionCall = false, should not add space before parentheses in lowercase function call`` () = + formatSourceString false "let value = myFunction(a+b)" ({ config with SpaceBeforeParenthesesArgumentInLowercaseFunctionCall = false }) |> should equal """let value = myFunction(a + b) """ @@ -95,87 +95,165 @@ let ``SpaceBeforeUnitParameterInUppercaseFunctionDefinition config should not ad |> should equal """let Value () = x """ +// Space before unit in lowercase function definition + [] -let ``default config should not add space after empty constructor of class`` () = - formatSourceString false """type Person () = - class end -""" config - |> prepend newline - |> should equal """ -type Person() = - class - end +let ``default config should not add space before unit in lowercase function definition`` () = + formatSourceString false "let value () = x" config + |> should equal """let value() = x """ [] -let ``default config should not add space after constructor of class`` () = - formatSourceString false """type Person (a:int) = - class end -""" config +let ``SpaceBeforeUnitParameterInLowercaseFunctionDefinition config should add space before unit in lowercase function definition`` () = + formatSourceString false "let value() = x" ({ config with SpaceBeforeUnitParameterInLowercaseFunctionDefinition = true }) + |> should equal """let value () = x +""" + +// Space before parentheses (a+b) in Uppercase function definition + +[] +let ``default config should not add space before parentheses in uppercase function definition`` () = + formatSourceString false "let Value (a:int) = x" config + |> should equal """let Value(a: int) = x +""" + +[] +let ``SpaceBeforeParenthesisInUppercaseFunctionDefinition config should add space before parentheses in uppercase function definition`` () = + formatSourceString false "let Value(a:int) = x" ({ config with SpaceBeforeParenthesesInUppercaseFunctionDefinition = true }) + |> should equal """let Value (a: int) = x +""" + +[] +let ``SpaceBeforeParenthesisInUppercaseFunctionDefinition should add space after discrimintation union member`` () = + formatSourceString false """match x with +| Zero() -> () +| One (o) -> () +| Two(o,t) -> () +""" ({ config with SpaceBeforeParenthesesInUppercaseFunctionDefinition = true }) |> prepend newline |> should equal """ -type Person(a: int) = +match x with +| Zero() -> () +| One (o) -> () +| Two (o, t) -> () +""" + +// Space before parentheses (a+b) in lowercase function definition + +[] +let ``default config should not add space before parentheses in lowercase function definition`` () = + formatSourceString false "let value (a:int) = x" config + |> should equal """let value (a: int) = x +""" + +[] +let ``SpaceBeforeParenthesisInLowercaseFunctionDefinition = false, should not add space before parentheses in lowercase function definition`` () = + formatSourceString false "let value(a:int) = x" ({ config with SpaceBeforeParenthesesInLowercaseFunctionDefinition = false }) + |> should equal """let value(a: int) = x +""" + +// Space before unit in Uppercase class definition + +[] +let ``default config should not add space before unit in uppercase class definition`` () = + formatSourceString false "type Person () = class end" config + |> should equal """type Person() = class end """ [] -let ``SpaceBeforeUnitParameterInUppercaseFunctionDefinition should add space after constructor of class`` () = +let ``SpaceBeforeUnitParameterInUppercaseClassConstructor should add space after constructor of class`` () = formatSourceString false """type Person () = class end - -type Animal (length:int) = - class end -""" ({ config with SpaceBeforeUnitParameterInUppercaseFunctionDefinition = true }) +""" ({ config with SpaceBeforeUnitParameterInUppercaseClassConstructor = true }) |> prepend newline |> should equal """ -type Person() = - class - end - -type Animal(length: int) = +type Person () = class end """ -// Space before unit in lowercase function definition +// Space before unit in lowercase class definition [] -let ``default config should not add space before unit in lowercase function definition`` () = - formatSourceString false "let value () = x" config - |> should equal """let value() = x +let ``default config should not add space before unit in lowercase class definition`` () = + formatSourceString false """type t () = + class + end +""" config + |> prepend newline + |> should equal """ +type t() = + class + end """ [] -let ``SpaceBeforeUnitParameterInLowercaseFunctionDefinition config should add space before unit in lowercase function definition`` () = - formatSourceString false "let value() = x" ({ config with SpaceBeforeUnitParameterInLowercaseFunctionDefinition = true }) - |> should equal """let value () = x +let ``SpaceBeforeUnitParameterInLowercaseClassConstructor should add space before unit in lowercase class definition`` () = + formatSourceString false """type t() = + class + end +""" ({ config with SpaceBeforeUnitParameterInLowercaseClassConstructor = true }) + |> prepend newline + |> should equal """ +type t () = + class + end """ -// Space before parenthesis (a+b) in Uppercase function definition +// Space before parentheses in Uppercase class definition [] -let ``default config should not add space before parenthesis in uppercase function definition`` () = - formatSourceString false "let Value (a:int) = x" config - |> should equal """let Value(a: int) = x +let ``default config should not add space before uppercase constructor of class`` () = + formatSourceString false """ +type Animal(length:int) = + class end +""" config + |> prepend newline + |> should equal """ +type Animal(length: int) = + class + end """ [] -let ``SpaceBeforeParenthesisInUppercaseFunctionDefinition config should add space before parenthesis in uppercase function definition`` () = - formatSourceString false "let Value(a:int) = x" ({ config with SpaceBeforeParenthesisInUppercaseFunctionDefinition = true }) - |> should equal """let Value (a: int) = x +let ``SpaceBeforeParenthesisParameterInUppercaseClassConstructor should add space before uppercase constructor of class`` () = + formatSourceString false """ +type Animal(length:int) = + class end +""" ({ config with SpaceBeforeParenthesesParameterInUppercaseClassConstructor = true }) + |> prepend newline + |> should equal """ +type Animal (length: int) = + class + end """ -// Space before parenthesis (a+b) in lowercase function definition +// Space before parentheses in lowercase class definition [] -let ``default config should not add space before parenthesis in lowercase function definition`` () = - formatSourceString false "let value (a:int) = x" config - |> should equal """let value (a: int) = x +let ``default config should not add space before lowercase constructor of class`` () = + formatSourceString false """ +type animal(length:int) = + class end +""" config + |> prepend newline + |> should equal """ +type animal(length: int) = + class + end """ [] -let ``SpaceBeforeParenthesisInLowercaseFunctionDefinition = false, should not add space before parenthesis in lowercase function definition`` () = - formatSourceString false "let value(a:int) = x" ({ config with SpaceBeforeParenthesisInLowercaseFunctionDefinition = false }) - |> should equal """let value(a: int) = x +let ``SpaceBeforeParenthesisParameterInUppercaseClassConstructor should add space before lowercase constructor of class`` () = + formatSourceString false """ +type animal(length:int) = + class end +""" ({ config with SpaceBeforeParenthesesParameterInLowercaseClassConstructor = true }) + |> prepend newline + |> should equal """ +type animal (length: int) = + class + end """ \ No newline at end of file diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index 63978ff345..b11d9ffe2c 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -52,9 +52,9 @@ let rec addSpaceBeforeParensInFunCall functionOrMethod arg (ctx:Context) = | SynExpr.Ident(_), SynExpr.Ident(_) -> true | UppercaseSynExpr, Paren(_) -> - ctx.Config.SpaceBeforeParenthesisArgumentInUppercaseFunctionCall + ctx.Config.SpaceBeforeParenthesesArgumentInUppercaseFunctionCall | LowercaseSynExpr, Paren(_) -> - ctx.Config.SpaceBeforeParenthesisArgumentInLowercaseFunctionCall + ctx.Config.SpaceBeforeParenthesesArgumentInLowercaseFunctionCall | _ -> true let addSpaceBeforeParensInFunDef (functionOrMethod:string) args (ctx:Context) = @@ -71,8 +71,8 @@ let addSpaceBeforeParensInFunDef (functionOrMethod:string) args (ctx:Context) = | _, PatParen(_) -> if isLastPartUppercase - then ctx.Config.SpaceBeforeParenthesisInUppercaseFunctionDefinition - else ctx.Config.SpaceBeforeParenthesisInLowercaseFunctionDefinition + then ctx.Config.SpaceBeforeParenthesesInUppercaseFunctionDefinition + else ctx.Config.SpaceBeforeParenthesesInLowercaseFunctionDefinition | (_:string), _ -> not isLastPartUppercase | _ -> true @@ -1601,12 +1601,35 @@ and genTypeDefn astContext (TypeDef(ats, px, ao, tds, tcs, tdr, ms, s, preferPos |> genTrivia tdr.Range | ObjectModel(TCSimple (TCInterface | TCClass) as tdk, MemberDefnList(impCtor, others), range) -> + let foo = s + let interfaceRange = match tdk with | TCSimple TCInterface -> Some range | _ -> None + let astContext = { astContext with InterfaceRange = interfaceRange } - typeName +> opt sepNone impCtor (genMemberDefn astContext) +> sepEq + + let addSpaceAfterTypeName ctx = + let (unitLower, unitUpper, parenLower, parenUpper) = + (ctx.Config.SpaceBeforeUnitParameterInLowercaseClassConstructor, + ctx.Config.SpaceBeforeUnitParameterInUppercaseClassConstructor, + ctx.Config.SpaceBeforeParenthesesParameterInLowercaseClassConstructor, + ctx.Config.SpaceBeforeParenthesesParameterInUppercaseClassConstructor) + let isUpper = Char.IsUpper foo.[0] + + match impCtor with + | Some(SynMemberDefn.ImplicitCtor(_,_, pats,_,_)) -> + let hasNoParameters = isEmptySynSimplePats pats + match hasNoParameters, isUpper with + | true, true -> unitUpper + | true, false -> unitLower + | false, true -> parenUpper + | false, false -> parenLower + | _ -> false + + + typeName +> ifElseCtx addSpaceAfterTypeName sepSpace sepNone +> opt sepNone impCtor (genMemberDefn astContext) +> sepEq +> indent +> sepNln +> genTrivia tdr.Range (genTypeDefKind tdk diff --git a/src/Fantomas/FormatConfig.fs b/src/Fantomas/FormatConfig.fs index 994045c227..f240d56db8 100644 --- a/src/Fantomas/FormatConfig.fs +++ b/src/Fantomas/FormatConfig.fs @@ -17,12 +17,16 @@ type FormatConfig = SemicolonAtEndOfLine : bool SpaceBeforeUnitArgumentInUppercaseFunctionCall : bool SpaceBeforeUnitArgumentInLowercaseFunctionCall : bool - SpaceBeforeParenthesisArgumentInUppercaseFunctionCall : bool - SpaceBeforeParenthesisArgumentInLowercaseFunctionCall : bool + SpaceBeforeParenthesesArgumentInUppercaseFunctionCall : bool + SpaceBeforeParenthesesArgumentInLowercaseFunctionCall : bool SpaceBeforeUnitParameterInUppercaseFunctionDefinition : bool SpaceBeforeUnitParameterInLowercaseFunctionDefinition : bool - SpaceBeforeParenthesisInUppercaseFunctionDefinition : bool - SpaceBeforeParenthesisInLowercaseFunctionDefinition : bool + SpaceBeforeParenthesesInUppercaseFunctionDefinition : bool + SpaceBeforeParenthesesInLowercaseFunctionDefinition : bool + SpaceBeforeUnitParameterInUppercaseClassConstructor : bool + SpaceBeforeUnitParameterInLowercaseClassConstructor : bool + SpaceBeforeParenthesesParameterInUppercaseClassConstructor : bool + SpaceBeforeParenthesesParameterInLowercaseClassConstructor : bool SpaceBeforeColon : bool SpaceAfterComma : bool SpaceAfterSemicolon : bool @@ -41,12 +45,16 @@ type FormatConfig = SemicolonAtEndOfLine = false SpaceBeforeUnitArgumentInUppercaseFunctionCall = false SpaceBeforeUnitArgumentInLowercaseFunctionCall = false - SpaceBeforeParenthesisArgumentInUppercaseFunctionCall = false - SpaceBeforeParenthesisArgumentInLowercaseFunctionCall = true + SpaceBeforeParenthesesArgumentInUppercaseFunctionCall = false + SpaceBeforeParenthesesArgumentInLowercaseFunctionCall = true SpaceBeforeUnitParameterInUppercaseFunctionDefinition = false SpaceBeforeUnitParameterInLowercaseFunctionDefinition = false - SpaceBeforeParenthesisInUppercaseFunctionDefinition = false - SpaceBeforeParenthesisInLowercaseFunctionDefinition = true + SpaceBeforeParenthesesInUppercaseFunctionDefinition = false + SpaceBeforeParenthesesInLowercaseFunctionDefinition = true + SpaceBeforeUnitParameterInUppercaseClassConstructor = false + SpaceBeforeUnitParameterInLowercaseClassConstructor = false + SpaceBeforeParenthesesParameterInUppercaseClassConstructor = false + SpaceBeforeParenthesesParameterInLowercaseClassConstructor = false SpaceBeforeColon = false SpaceAfterComma = true SpaceAfterSemicolon = true diff --git a/src/Fantomas/SourceParser.fs b/src/Fantomas/SourceParser.fs index cb12820776..9ddafa9ccc 100644 --- a/src/Fantomas/SourceParser.fs +++ b/src/Fantomas/SourceParser.fs @@ -1321,3 +1321,8 @@ let (|UppercaseSynExpr|LowercaseSynExpr|) (synExpr:SynExpr) = | None -> LowercaseSynExpr | _ -> failwithf "cannot determine if synExpr %A is uppercase or lowercase" synExpr + +let rec isEmptySynSimplePats (ssp:SynSimplePats) = + match ssp with + | SynSimplePats.SimplePats(pats,_) -> List.isEmpty pats + | SynSimplePats.Typed (ssp,_,_) -> isEmptySynSimplePats ssp \ No newline at end of file From 79484ddd30546fca4196fef861a617c2f7231d60 Mon Sep 17 00:00:00 2001 From: nojaf Date: Fri, 24 Jan 2020 15:44:19 +0100 Subject: [PATCH 05/12] Update documentation and schema.json --- docs/Documentation.md | 235 +++++++++++++++++++++++++++++++++++++-- src/Fantomas/schema.json | 20 +++- 2 files changed, 244 insertions(+), 11 deletions(-) diff --git a/docs/Documentation.md b/docs/Documentation.md index b5516bc561..f05905960a 100644 --- a/docs/Documentation.md +++ b/docs/Documentation.md @@ -1,6 +1,7 @@ ## Fantomas: How to use ### Using the command line tool + --- For the overview how to use the tool, you can type the command @@ -102,11 +103,220 @@ add semicolons at the end of lines e.g. Mass = 0.0002858859807 * solarMass } ``` - ##### `--noSpaceBeforeArgument` +##### `--noSpaceBeforeArgument` + +This option has been split into multiple settings. +These can be configured by passing a `fantomas-config.json` path to the `--config` flag. + +###### `"SpaceBeforeUnitArgumentInUppercaseFunctionCall"` + +> default false + +Will add a whitespace before `()` argument when the function name starts with an uppercase character. e.g. + +```fsharp +let value = MyFunction() +let value = person.ToString() +``` + +becomes + +```fsharp +let value = MyFunction () +let value = person.ToString () +``` + +###### `"SpaceBeforeUnitArgumentInLowercaseFunctionCall"` + +Will add a whitespace before `()` argument when the function name starts with a lowercase character. e.g. + +> default false + +```fsharp +let value = myFunction() +``` + +becomes + +```fsharp +let value = myFunction () +``` + +###### `"SpaceBeforeParenthesesArgumentInUppercaseFunctionCall"` + +> default false + +Will add a whitespace before an argument wrapped with parentheses when the function name starts with an uppercase character. e.g. + +```fsharp +let value = MyFunction(a+b) +``` + +becomes + +```fsharp +let value = MyFunction (a + b) +``` + +###### `"SpaceBeforeParenthesesArgumentInLowercaseFunctionCall"` + +> default true + +Will add a whitespace before an argument wrapped with parentheses when the function name starts with an lowercase character. e.g. + +```fsharp +let value = myFunction(a+b) +``` + +becomes + +```fsharp +let value = myFunction (a+b) +``` + +To remove the space, set `"SpaceBeforeParenthesesArgumentInLowercaseFunctionCall"` to `false`. + +###### `"SpaceBeforeUnitParameterInUppercaseFunctionDefinition"` + +> default false + +Will add a whitespace before a `()` parameter in a function definition that starts with an uppercase letter. e.g. + +```fsharp +let Value() = x +``` + +becomes + +```fsharp +let Value () = x +``` + +###### `"SpaceBeforeUnitParameterInLowercaseFunctionDefinition"` + +> default false + +Will add a whitespace before a `()` parameter in a function definition that starts with an lowercase letter. e.g. + +```fsharp +let value() = x +``` + +becomes + +```fsharp +let value () = x +``` + +###### `"SpaceBeforeParenthesesInUppercaseFunctionDefinition"` + +> default false + +Will add a whitespace before a parameters wrapped with parentheses that starts with an uppercase letter. e.g. + +```fsharp +let Value(a:int) = x +``` + +becomes -if being set, no space is inserted before a function name and its first argument. -For example, `Seq.filter (fun x -> x > 2)` becomes `Seq.filter(fun x -> x > 2)`. -This doesn't affect methods and constructors, e.g. `Console.WriteLine("Hello World")`. +```fsharp +let Value (a:int) = x +``` + +###### `SpaceBeforeParenthesesInLowercaseFunctionDefinition":true` + +> default true + +Will add a whitespace before a parameters wrapped with parentheses that starts with an uppercase letter. e.g. + +```fsharp +let value(a:int) = x +``` + +becomes + +```fsharp +let value (a:int) = x +``` + +To remove the space, set `"SpaceBeforeParenthesesInLowercaseFunctionDefinition"` to `false`. + +###### `"SpaceBeforeUnitParameterInUppercaseClassConstructor"` + +> default false + +Will add a whitespace before an empty constructor of class definition that starts with an uppercase letter. e.g. + +```fsharp +type Person() = + class end +``` + +becomes + +```fsharp +type Person () = + class + end +``` + +###### `"SpaceBeforeUnitParameterInLowercaseClassConstructor"` + +> default false + +Will add a whitespace before an empty constructor of class definition that starts with a lowercase letter. e.g. + +```fsharp +type t() = + class + end +``` + +becomes + +```fsharp +type t () = + class + end +``` + +###### `"SpaceBeforeParenthesesParameterInUppercaseClassConstructor"` + +> default false + +Will add a whitespace before an non-empty constructor of class definition that starts with an uppercase letter. e.g. + +```fsharp +type Animal(length:int) = + class end +``` + +becomes + +```fsharp +type Animal (length: int) = + class + end +``` + +###### `"SpaceBeforeParenthesesParameterInLowercaseClassConstructor"` + +> default false + +Will add a whitespace before an non-empty constructor of class definition that starts with a lowercase letter. e.g. + +```fsharp +type animal(length:int) = + class end +``` + +becomes + +```fsharp +type animal (length:int) = + class end +``` ##### `--spaceBeforeColon` @@ -257,11 +467,22 @@ Use a JSON configuration file based on a [schema](../src/Fantomas/schema.json) t A default configuration file would look like ```json -{ +{ "IndentSpaceNum":4, "PageWidth":120, "SemicolonAtEndOfLine":false, - "SpaceBeforeArgument":true , + "SpaceBeforeUnitArgumentInUppercaseFunctionCall":false, + "SpaceBeforeUnitArgumentInLowercaseFunctionCall":false, + "SpaceBeforeParenthesesArgumentInUppercaseFunctionCall":false, + "SpaceBeforeParenthesesArgumentInLowercaseFunctionCall":true , + "SpaceBeforeUnitParameterInUppercaseFunctionDefinition":false, + "SpaceBeforeUnitParameterInLowercaseFunctionDefinition":false, + "SpaceBeforeParenthesesInUppercaseFunctionDefinition":false, + "SpaceBeforeParenthesesInLowercaseFunctionDefinition":true , + "SpaceBeforeUnitParameterInUppercaseClassConstructor":false, + "SpaceBeforeUnitParameterInLowercaseClassConstructor":false, + "SpaceBeforeParenthesesParameterInUppercaseClassConstructor":false, + "SpaceBeforeParenthesesParameterInLowercaseClassConstructor":false, "SpaceBeforeColon":false, "SpaceAfterComma":true , "SpaceAfterSemicolon":true , @@ -270,7 +491,7 @@ A default configuration file would look like "SpaceAroundDelimiter":true , "KeepNewlineAfter":false, "MaxIfThenElseShortWidth":40, - "StrictMode":false + "StrictMode":false } ``` diff --git a/src/Fantomas/schema.json b/src/Fantomas/schema.json index 272063469b..e8e2b6f4aa 100644 --- a/src/Fantomas/schema.json +++ b/src/Fantomas/schema.json @@ -17,10 +17,10 @@ "SpaceBeforeUnitArgumentInLowercaseFunctionCall": { "type": "boolean" }, - "SpaceBeforeParenthesisArgumentInUppercaseFunctionCall": { + "SpaceBeforeParenthesesArgumentInUppercaseFunctionCall": { "type": "boolean" }, - "SpaceBeforeParenthesisArgumentInLowercaseFunctionCall": { + "SpaceBeforeParenthesesArgumentInLowercaseFunctionCall": { "type": "boolean" }, "SpaceBeforeUnitParameterInUppercaseFunctionDefinition": { @@ -29,10 +29,22 @@ "SpaceBeforeUnitParameterInLowercaseFunctionDefinition": { "type": "boolean" }, - "SpaceBeforeParenthesisInUppercaseFunctionDefinition": { + "SpaceBeforeParenthesesInUppercaseFunctionDefinition": { "type": "boolean" }, - "SpaceBeforeParenthesisInLowercaseFunctionDefinition": { + "SpaceBeforeParenthesesInLowercaseFunctionDefinition": { + "type": "boolean" + }, + "SpaceBeforeUnitParameterInUppercaseClassConstructor": { + "type": "boolean" + }, + "SpaceBeforeUnitParameterInLowercaseClassConstructor": { + "type": "boolean" + }, + "SpaceBeforeParenthesesParameterInUppercaseClassConstructor": { + "type": "boolean" + }, + "SpaceBeforeParenthesesParameterInLowercaseClassConstructor": { "type": "boolean" }, "SpaceBeforeColon": { From 446c5cf19c11d64e22d9b2ddfa36cbf7286e01b0 Mon Sep 17 00:00:00 2001 From: nojaf Date: Fri, 24 Jan 2020 15:49:33 +0100 Subject: [PATCH 06/12] Add empty line at the end of file. --- src/Fantomas.Tests/SpaceBeforeArgumentsAndParametersTests.fs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Fantomas.Tests/SpaceBeforeArgumentsAndParametersTests.fs b/src/Fantomas.Tests/SpaceBeforeArgumentsAndParametersTests.fs index 01331e7416..7def2c25a1 100644 --- a/src/Fantomas.Tests/SpaceBeforeArgumentsAndParametersTests.fs +++ b/src/Fantomas.Tests/SpaceBeforeArgumentsAndParametersTests.fs @@ -256,4 +256,4 @@ type animal(length:int) = type animal (length: int) = class end -""" \ No newline at end of file +""" From 4b51b00d6309e968b664360507f06ecd83ed355e Mon Sep 17 00:00:00 2001 From: nojaf Date: Mon, 3 Feb 2020 19:21:42 +0100 Subject: [PATCH 07/12] Updated month in release notes --- RELEASE_NOTES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index e5d9e8625c..4cf38b2e91 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -1,4 +1,4 @@ -### 3.2.0 - 01/2020 +### 3.2.0 - 02/2020 * Added support for settings configuration file. [#354](https://github.com/fsprojects/fantomas/issues/354) * Use Argu for commandline argument parsing. [#607](https://github.com/fsprojects/fantomas/pull/607) From cbfcbcda81cab695a4805feefb9b0907cba27374 Mon Sep 17 00:00:00 2001 From: nojaf Date: Fri, 21 Feb 2020 09:36:17 +0100 Subject: [PATCH 08/12] Reduced class constructor setting to one setting. --- docs/Documentation.md | 81 +++---------------- src/Fantomas.Tests/AttributeTests.fs | 2 +- .../SpaceBeforeArgumentsAndParametersTests.fs | 30 +++---- src/Fantomas/CodePrinter.fs | 29 ++----- src/Fantomas/FormatConfig.fs | 26 +++--- src/Fantomas/schema.json | 19 ++--- 6 files changed, 48 insertions(+), 139 deletions(-) diff --git a/docs/Documentation.md b/docs/Documentation.md index 1c82552d55..a40fff9977 100644 --- a/docs/Documentation.md +++ b/docs/Documentation.md @@ -108,7 +108,7 @@ add semicolons at the end of lines e.g. This option has been split into multiple settings. These can be configured by passing a `fantomas-config.json` path to the `--config` flag. -###### `"SpaceBeforeUnitArgumentInUppercaseFunctionCall"` +###### `"SpaceBeforeUnitArgumentInUppercaseInvocation"` > default false @@ -126,7 +126,7 @@ let value = MyFunction () let value = person.ToString () ``` -###### `"SpaceBeforeUnitArgumentInLowercaseFunctionCall"` +###### `"SpaceBeforeUnitArgumentInLowercaseInvocation"` Will add a whitespace before `()` argument when the function name starts with a lowercase character. e.g. @@ -142,7 +142,7 @@ becomes let value = myFunction () ``` -###### `"SpaceBeforeParenthesesArgumentInUppercaseFunctionCall"` +###### `"SpaceBeforeParenthesesInUppercaseInvocation"` > default false @@ -158,7 +158,7 @@ becomes let value = MyFunction (a + b) ``` -###### `"SpaceBeforeParenthesesArgumentInLowercaseFunctionCall"` +###### `"SpaceBeforeParenthesesInLowercaseInvocation"` > default true @@ -174,7 +174,7 @@ becomes let value = myFunction (a+b) ``` -To remove the space, set `"SpaceBeforeParenthesesArgumentInLowercaseFunctionCall"` to `false`. +To remove the space, set `"SpaceBeforeParenthesesInLowercaseInvocation"` to `false`. ###### `"SpaceBeforeUnitParameterInUppercaseFunctionDefinition"` @@ -224,7 +224,7 @@ becomes let Value (a:int) = x ``` -###### `SpaceBeforeParenthesesInLowercaseFunctionDefinition":true` +###### `SpaceBeforeParenthesesInLowercaseFunctionDefinition"` > default true @@ -242,11 +242,11 @@ let value (a:int) = x To remove the space, set `"SpaceBeforeParenthesesInLowercaseFunctionDefinition"` to `false`. -###### `"SpaceBeforeUnitParameterInUppercaseClassConstructor"` +###### `"SpaceBeforeClassConstructor"` > default false -Will add a whitespace before an empty constructor of class definition that starts with an uppercase letter. e.g. +Will add a whitespace before a constructor of class definition. ```fsharp type Person() = @@ -261,63 +261,6 @@ type Person () = end ``` -###### `"SpaceBeforeUnitParameterInLowercaseClassConstructor"` - -> default false - -Will add a whitespace before an empty constructor of class definition that starts with a lowercase letter. e.g. - -```fsharp -type t() = - class - end -``` - -becomes - -```fsharp -type t () = - class - end -``` - -###### `"SpaceBeforeParenthesesParameterInUppercaseClassConstructor"` - -> default false - -Will add a whitespace before an non-empty constructor of class definition that starts with an uppercase letter. e.g. - -```fsharp -type Animal(length:int) = - class end -``` - -becomes - -```fsharp -type Animal (length: int) = - class - end -``` - -###### `"SpaceBeforeParenthesesParameterInLowercaseClassConstructor"` - -> default false - -Will add a whitespace before an non-empty constructor of class definition that starts with a lowercase letter. e.g. - -```fsharp -type animal(length:int) = - class end -``` - -becomes - -```fsharp -type animal (length:int) = - class end -``` - ##### `--spaceBeforeColon` if being set, there is a space before `:` e.g. @@ -474,10 +417,10 @@ A default configuration file would look like "IndentSpaceNum":4, "PageWidth":120, "SemicolonAtEndOfLine":false, - "SpaceBeforeUnitArgumentInUppercaseFunctionCall":false, - "SpaceBeforeUnitArgumentInLowercaseFunctionCall":false, - "SpaceBeforeParenthesesArgumentInUppercaseFunctionCall":false, - "SpaceBeforeParenthesesArgumentInLowercaseFunctionCall":true , + "SpaceBeforeUnitArgumentInUppercaseInvocation":false, + "SpaceBeforeUnitArgumentInLowercaseInvocation":false, + "SpaceBeforeParenthesesInUppercaseInvocation":false, + "SpaceBeforeParenthesesInLowercaseInvocation":true , "SpaceBeforeUnitParameterInUppercaseFunctionDefinition":false, "SpaceBeforeUnitParameterInLowercaseFunctionDefinition":false, "SpaceBeforeParenthesesInUppercaseFunctionDefinition":false, diff --git a/src/Fantomas.Tests/AttributeTests.fs b/src/Fantomas.Tests/AttributeTests.fs index e6096511bf..9d39a18b04 100644 --- a/src/Fantomas.Tests/AttributeTests.fs +++ b/src/Fantomas.Tests/AttributeTests.fs @@ -292,7 +292,7 @@ let main argv = SpaceAfterComma = false SpaceAfterSemicolon = false SpaceAroundDelimiter = false - SpaceBeforeParenthesesArgumentInLowercaseFunctionCall = false }) + SpaceBeforeParenthesesInLowercaseInvocation = false }) |> prepend newline |> should equal """ open System diff --git a/src/Fantomas.Tests/SpaceBeforeArgumentsAndParametersTests.fs b/src/Fantomas.Tests/SpaceBeforeArgumentsAndParametersTests.fs index 7def2c25a1..758efddf38 100644 --- a/src/Fantomas.Tests/SpaceBeforeArgumentsAndParametersTests.fs +++ b/src/Fantomas.Tests/SpaceBeforeArgumentsAndParametersTests.fs @@ -13,14 +13,14 @@ let ``default config should not add space before unit in uppercase function call """ [] -let ``SpaceBeforeUnitArgumentInUppercaseFunctionCall should add space before unit in uppercase function call`` () = - formatSourceString false "let value = MyFunction()" ({ config with SpaceBeforeUnitArgumentInUppercaseFunctionCall = true }) +let ``SpaceBeforeUnitArgumentInUppercaseInvocation should add space before unit in uppercase function call`` () = + formatSourceString false "let value = MyFunction()" ({ config with SpaceBeforeUnitArgumentInUppercaseInvocation = true }) |> should equal """let value = MyFunction () """ [] -let ``SpaceBeforeUnitArgumentInUppercaseFunctionCall should add space before unit in chained uppercase function call`` () = - formatSourceString false "let value = person.ToString()" ({ config with SpaceBeforeUnitArgumentInUppercaseFunctionCall = true }) +let ``SpaceBeforeUnitArgumentInUppercaseInvocation should add space before unit in chained uppercase function call`` () = + formatSourceString false "let value = person.ToString()" ({ config with SpaceBeforeUnitArgumentInUppercaseInvocation = true }) |> should equal """let value = person.ToString () """ @@ -33,20 +33,20 @@ let ``default config should not add space before unit in lowercase function call """ [] -let ``SpaceBeforeUnitArgumentInLowercaseFunctionCall should add space before unit in lowercase function call`` () = - formatSourceString false "let value = myFunction()" ({ config with SpaceBeforeUnitArgumentInLowercaseFunctionCall = true }) +let ``SpaceBeforeUnitArgumentInLowercaseInvocation should add space before unit in lowercase function call`` () = + formatSourceString false "let value = myFunction()" ({ config with SpaceBeforeUnitArgumentInLowercaseInvocation = true }) |> should equal """let value = myFunction () """ // Exception to the rule [] -let ``SpaceBeforeUnitArgumentInUppercaseFunctionCall and SpaceBeforeUnitArgumentInLowercaseFunctionCall should not have impact when member is called after unit`` () = +let ``SpaceBeforeUnitArgumentInUppercaseInvocation and SpaceBeforeUnitArgumentInLowercaseInvocation should not have impact when member is called after unit`` () = formatSourceString false """let v1 = myFunction().Member let v2 = OtherFunction().Member """ ({ config with - SpaceBeforeUnitArgumentInUppercaseFunctionCall = true - SpaceBeforeUnitArgumentInLowercaseFunctionCall = true }) + SpaceBeforeUnitArgumentInUppercaseInvocation = true + SpaceBeforeUnitArgumentInLowercaseInvocation = true }) |> prepend newline |> should equal """ let v1 = myFunction().Member @@ -63,7 +63,7 @@ let ``default config should not add space before parentheses in uppercase functi [] let ``SpaceBeforeParenthesisArgumentInUppercaseFunctionCall should add space before parentheses in uppercase function call`` () = - formatSourceString false "let value = MyFunction(a+b)" ({ config with SpaceBeforeParenthesesArgumentInUppercaseFunctionCall = true }) + formatSourceString false "let value = MyFunction(a+b)" ({ config with SpaceBeforeParenthesesInUppercaseInvocation = true }) |> should equal """let value = MyFunction (a + b) """ @@ -77,7 +77,7 @@ let ``default config should add space before parentheses in lowercase function c [] let ``SpaceBeforeParenthesisArgumentInUppercaseFunctionCall = false, should not add space before parentheses in lowercase function call`` () = - formatSourceString false "let value = myFunction(a+b)" ({ config with SpaceBeforeParenthesesArgumentInLowercaseFunctionCall = false }) + formatSourceString false "let value = myFunction(a+b)" ({ config with SpaceBeforeParenthesesInLowercaseInvocation = false }) |> should equal """let value = myFunction(a + b) """ @@ -166,7 +166,7 @@ let ``default config should not add space before unit in uppercase class definit let ``SpaceBeforeUnitParameterInUppercaseClassConstructor should add space after constructor of class`` () = formatSourceString false """type Person () = class end -""" ({ config with SpaceBeforeUnitParameterInUppercaseClassConstructor = true }) +""" ({ config with SpaceBeforeClassConstructor = true }) |> prepend newline |> should equal """ type Person () = @@ -194,7 +194,7 @@ let ``SpaceBeforeUnitParameterInLowercaseClassConstructor should add space befor formatSourceString false """type t() = class end -""" ({ config with SpaceBeforeUnitParameterInLowercaseClassConstructor = true }) +""" ({ config with SpaceBeforeClassConstructor = true }) |> prepend newline |> should equal """ type t () = @@ -222,7 +222,7 @@ let ``SpaceBeforeParenthesisParameterInUppercaseClassConstructor should add spac formatSourceString false """ type Animal(length:int) = class end -""" ({ config with SpaceBeforeParenthesesParameterInUppercaseClassConstructor = true }) +""" ({ config with SpaceBeforeClassConstructor = true }) |> prepend newline |> should equal """ type Animal (length: int) = @@ -250,7 +250,7 @@ let ``SpaceBeforeParenthesisParameterInUppercaseClassConstructor should add spac formatSourceString false """ type animal(length:int) = class end -""" ({ config with SpaceBeforeParenthesesParameterInLowercaseClassConstructor = true }) +""" ({ config with SpaceBeforeClassConstructor = true }) |> prepend newline |> should equal """ type animal (length: int) = diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index 0dd3a9e263..87d2c87baf 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -47,15 +47,15 @@ let rec addSpaceBeforeParensInFunCall functionOrMethod arg (ctx:Context) = | SynExpr.TypeApp(e, _, _, _, _, _, _), _ -> addSpaceBeforeParensInFunCall e arg ctx | UppercaseSynExpr, ConstExpr(Const "()", _) -> - ctx.Config.SpaceBeforeUnitArgumentInUppercaseFunctionCall + ctx.Config.SpaceBeforeUnitArgumentInUppercaseInvocation | LowercaseSynExpr, ConstExpr(Const "()", _) -> - ctx.Config.SpaceBeforeUnitArgumentInLowercaseFunctionCall + ctx.Config.SpaceBeforeUnitArgumentInLowercaseInvocation | SynExpr.Ident(_), SynExpr.Ident(_) -> true | UppercaseSynExpr, Paren(_) -> - ctx.Config.SpaceBeforeParenthesesArgumentInUppercaseFunctionCall + ctx.Config.SpaceBeforeParenthesesInUppercaseInvocation | LowercaseSynExpr, Paren(_) -> - ctx.Config.SpaceBeforeParenthesesArgumentInLowercaseFunctionCall + ctx.Config.SpaceBeforeParenthesesInLowercaseInvocation | _ -> true let addSpaceBeforeParensInFunDef (functionOrMethod:string) args (ctx:Context) = @@ -1676,8 +1676,6 @@ and genTypeDefn astContext (TypeDef(ats, px, ao, tds, tcs, tdr, ms, s, preferPos |> genTrivia tdr.Range | ObjectModel(TCSimple (TCInterface | TCClass) as tdk, MemberDefnList(impCtor, others), range) -> - let foo = s - let interfaceRange = match tdk with | TCSimple TCInterface -> Some range @@ -1685,24 +1683,7 @@ and genTypeDefn astContext (TypeDef(ats, px, ao, tds, tcs, tdr, ms, s, preferPos let astContext = { astContext with InterfaceRange = interfaceRange } - let addSpaceAfterTypeName ctx = - let (unitLower, unitUpper, parenLower, parenUpper) = - (ctx.Config.SpaceBeforeUnitParameterInLowercaseClassConstructor, - ctx.Config.SpaceBeforeUnitParameterInUppercaseClassConstructor, - ctx.Config.SpaceBeforeParenthesesParameterInLowercaseClassConstructor, - ctx.Config.SpaceBeforeParenthesesParameterInUppercaseClassConstructor) - let isUpper = Char.IsUpper foo.[0] - - match impCtor with - | Some(SynMemberDefn.ImplicitCtor(_,_, pats,_,_)) -> - let hasNoParameters = isEmptySynSimplePats pats - match hasNoParameters, isUpper with - | true, true -> unitUpper - | true, false -> unitLower - | false, true -> parenUpper - | false, false -> parenLower - | _ -> false - + let addSpaceAfterTypeName ctx = ctx.Config.SpaceBeforeClassConstructor typeName +> ifElseCtx addSpaceAfterTypeName sepSpace sepNone +> opt sepNone impCtor (genMemberDefn astContext) +> sepEq +> indent +> sepNln diff --git a/src/Fantomas/FormatConfig.fs b/src/Fantomas/FormatConfig.fs index f6574e12c8..86b75944b0 100644 --- a/src/Fantomas/FormatConfig.fs +++ b/src/Fantomas/FormatConfig.fs @@ -15,18 +15,15 @@ type FormatConfig = /// The column where we break to new lines PageWidth : Num SemicolonAtEndOfLine : bool - SpaceBeforeUnitArgumentInUppercaseFunctionCall : bool - SpaceBeforeUnitArgumentInLowercaseFunctionCall : bool - SpaceBeforeParenthesesArgumentInUppercaseFunctionCall : bool - SpaceBeforeParenthesesArgumentInLowercaseFunctionCall : bool + SpaceBeforeUnitArgumentInUppercaseInvocation : bool + SpaceBeforeUnitArgumentInLowercaseInvocation : bool + SpaceBeforeParenthesesInUppercaseInvocation : bool + SpaceBeforeParenthesesInLowercaseInvocation : bool SpaceBeforeUnitParameterInUppercaseFunctionDefinition : bool SpaceBeforeUnitParameterInLowercaseFunctionDefinition : bool SpaceBeforeParenthesesInUppercaseFunctionDefinition : bool SpaceBeforeParenthesesInLowercaseFunctionDefinition : bool - SpaceBeforeUnitParameterInUppercaseClassConstructor : bool - SpaceBeforeUnitParameterInLowercaseClassConstructor : bool - SpaceBeforeParenthesesParameterInUppercaseClassConstructor : bool - SpaceBeforeParenthesesParameterInLowercaseClassConstructor : bool + SpaceBeforeClassConstructor : bool SpaceBeforeColon : bool SpaceAfterComma : bool SpaceAfterSemicolon : bool @@ -43,18 +40,15 @@ type FormatConfig = { IndentSpaceNum = 4 PageWidth = 120 SemicolonAtEndOfLine = false - SpaceBeforeUnitArgumentInUppercaseFunctionCall = false - SpaceBeforeUnitArgumentInLowercaseFunctionCall = false - SpaceBeforeParenthesesArgumentInUppercaseFunctionCall = false - SpaceBeforeParenthesesArgumentInLowercaseFunctionCall = true + SpaceBeforeUnitArgumentInUppercaseInvocation = false + SpaceBeforeUnitArgumentInLowercaseInvocation = false + SpaceBeforeParenthesesInUppercaseInvocation = false + SpaceBeforeParenthesesInLowercaseInvocation = true SpaceBeforeUnitParameterInUppercaseFunctionDefinition = false SpaceBeforeUnitParameterInLowercaseFunctionDefinition = false SpaceBeforeParenthesesInUppercaseFunctionDefinition = false SpaceBeforeParenthesesInLowercaseFunctionDefinition = true - SpaceBeforeUnitParameterInUppercaseClassConstructor = false - SpaceBeforeUnitParameterInLowercaseClassConstructor = false - SpaceBeforeParenthesesParameterInUppercaseClassConstructor = false - SpaceBeforeParenthesesParameterInLowercaseClassConstructor = false + SpaceBeforeClassConstructor = false SpaceBeforeColon = false SpaceAfterComma = true SpaceAfterSemicolon = true diff --git a/src/Fantomas/schema.json b/src/Fantomas/schema.json index e8e2b6f4aa..687ce89975 100644 --- a/src/Fantomas/schema.json +++ b/src/Fantomas/schema.json @@ -11,16 +11,16 @@ "SemicolonAtEndOfLine": { "type": "boolean" }, - "SpaceBeforeUnitArgumentInUppercaseFunctionCall": { + "SpaceBeforeUnitArgumentInUppercaseInvocation": { "type": "boolean" }, - "SpaceBeforeUnitArgumentInLowercaseFunctionCall": { + "SpaceBeforeUnitArgumentInLowercaseInvocation": { "type": "boolean" }, - "SpaceBeforeParenthesesArgumentInUppercaseFunctionCall": { + "SpaceBeforeParenthesesInUppercaseInvocation": { "type": "boolean" }, - "SpaceBeforeParenthesesArgumentInLowercaseFunctionCall": { + "SpaceBeforeParenthesesInLowercaseInvocation": { "type": "boolean" }, "SpaceBeforeUnitParameterInUppercaseFunctionDefinition": { @@ -35,16 +35,7 @@ "SpaceBeforeParenthesesInLowercaseFunctionDefinition": { "type": "boolean" }, - "SpaceBeforeUnitParameterInUppercaseClassConstructor": { - "type": "boolean" - }, - "SpaceBeforeUnitParameterInLowercaseClassConstructor": { - "type": "boolean" - }, - "SpaceBeforeParenthesesParameterInUppercaseClassConstructor": { - "type": "boolean" - }, - "SpaceBeforeParenthesesParameterInLowercaseClassConstructor": { + "SpaceBeforeClassConstructor": { "type": "boolean" }, "SpaceBeforeColon": { From cac8d657c8c5f0a1c70cfd8ce2bbbfd4d419862a Mon Sep 17 00:00:00 2001 From: nojaf Date: Fri, 21 Feb 2020 10:05:16 +0100 Subject: [PATCH 09/12] Update sample default configuration --- docs/Documentation.md | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/docs/Documentation.md b/docs/Documentation.md index a40fff9977..5dcd51364f 100644 --- a/docs/Documentation.md +++ b/docs/Documentation.md @@ -425,10 +425,7 @@ A default configuration file would look like "SpaceBeforeUnitParameterInLowercaseFunctionDefinition":false, "SpaceBeforeParenthesesInUppercaseFunctionDefinition":false, "SpaceBeforeParenthesesInLowercaseFunctionDefinition":true , - "SpaceBeforeUnitParameterInUppercaseClassConstructor":false, - "SpaceBeforeUnitParameterInLowercaseClassConstructor":false, - "SpaceBeforeParenthesesParameterInUppercaseClassConstructor":false, - "SpaceBeforeParenthesesParameterInLowercaseClassConstructor":false, + "SpaceBeforeClassConstructor":false, "SpaceBeforeColon":false, "SpaceAfterComma":true , "SpaceAfterSemicolon":true , From cfb5e5147538782d6e18ca6298e885fe29b14e6c Mon Sep 17 00:00:00 2001 From: nojaf Date: Fri, 6 Mar 2020 12:07:37 +0100 Subject: [PATCH 10/12] Simply configuration options --- src/Fantomas.Tests/AttributeTests.fs | 8 +- src/Fantomas.Tests/CommentTests.fs | 14 +- src/Fantomas.Tests/CompilerDirectivesTests.fs | 10 +- .../ComputationExpressionTests.fs | 10 +- src/Fantomas.Tests/ControlStructureTests.fs | 12 +- src/Fantomas.Tests/DataStructureTests.fs | 2 +- src/Fantomas.Tests/Fantomas.Tests.fsproj | 6 +- src/Fantomas.Tests/FunctionDefinitionTests.fs | 10 +- src/Fantomas.Tests/InterfaceTests.fs | 4 +- src/Fantomas.Tests/LetBindingTests.fs | 16 +- src/Fantomas.Tests/LongIdentWithDotsTests.fs | 2 +- src/Fantomas.Tests/NoTrailingSpacesTests.fs | 2 +- src/Fantomas.Tests/PatternMatchingTests.fs | 20 +- src/Fantomas.Tests/PipingTests.fs | 2 +- src/Fantomas.Tests/RecordTests.fs | 2 +- src/Fantomas.Tests/SignatureTests.fs | 2 +- .../SpaceBeforeArgumentsAndParametersTests.fs | 259 ------------------ .../SpaceBeforeClassConstructorTests.fs | 113 ++++++++ .../SpaceBeforeLowercaseInvocationTests.fs | 43 +++ src/Fantomas.Tests/SpaceBeforeMemberTests.fs | 43 +++ .../SpaceBeforeParameterTests.fs | 78 ++++++ .../SpaceBeforeUppercaseInvocationTests.fs | 51 ++++ src/Fantomas.Tests/StructTests.fs | 2 +- src/Fantomas.Tests/TypeDeclarationTests.fs | 20 +- src/Fantomas.Tests/UnionTests.fs | 4 +- src/Fantomas/CodePrinter.fs | 31 +-- src/Fantomas/FormatConfig.fs | 24 +- 27 files changed, 427 insertions(+), 363 deletions(-) delete mode 100644 src/Fantomas.Tests/SpaceBeforeArgumentsAndParametersTests.fs create mode 100644 src/Fantomas.Tests/SpaceBeforeClassConstructorTests.fs create mode 100644 src/Fantomas.Tests/SpaceBeforeLowercaseInvocationTests.fs create mode 100644 src/Fantomas.Tests/SpaceBeforeMemberTests.fs create mode 100644 src/Fantomas.Tests/SpaceBeforeParameterTests.fs create mode 100644 src/Fantomas.Tests/SpaceBeforeUppercaseInvocationTests.fs diff --git a/src/Fantomas.Tests/AttributeTests.fs b/src/Fantomas.Tests/AttributeTests.fs index 9d39a18b04..3139da52c1 100644 --- a/src/Fantomas.Tests/AttributeTests.fs +++ b/src/Fantomas.Tests/AttributeTests.fs @@ -112,9 +112,9 @@ and [] b () = 10""" config |> prepend newline |> should equal """ [] -let rec a() = 10 +let rec a () = 10 -and [] b() = 10 +and [] b () = 10 """ [] @@ -190,7 +190,7 @@ let printInStyle (style: string) (msg): unit = jsNative let printModel model: unit = jsNative [] -let printStackTrace(): unit = jsNative +let printStackTrace (): unit = jsNative #endif let e2e value = Props.Data("e2e", value) @@ -292,7 +292,7 @@ let main argv = SpaceAfterComma = false SpaceAfterSemicolon = false SpaceAroundDelimiter = false - SpaceBeforeParenthesesInLowercaseInvocation = false }) + SpaceBeforeLowercaseInvocation = false }) |> prepend newline |> should equal """ open System diff --git a/src/Fantomas.Tests/CommentTests.fs b/src/Fantomas.Tests/CommentTests.fs index c63c37fc05..75ba0bcf3f 100644 --- a/src/Fantomas.Tests/CommentTests.fs +++ b/src/Fantomas.Tests/CommentTests.fs @@ -38,7 +38,7 @@ let print_30_permut() = """ config |> prepend newline |> should equal """ -let print_30_permut() = +let print_30_permut () = /// declare and initialize let permutation: int array = @@ -59,7 +59,7 @@ let print_30_permut() = """ config |> prepend newline |> should equal """ -let print_30_permut() = +let print_30_permut () = /// declare and initialize let permutation: int array = @@ -120,7 +120,7 @@ let f() = """ config |> prepend newline |> should equal """ -let f() = +let f () = // COMMENT x + x """ @@ -134,7 +134,7 @@ let f() = """ config |> prepend newline |> should equal """ -let f() = +let f () = let x = 1 // COMMENT x + x """ @@ -156,7 +156,7 @@ let f() = |> should equal """ /// XML COMMENT // Other comment -let f() = +let f () = // COMMENT A let y = 1 (* COMMENT B *) @@ -182,7 +182,7 @@ let f() = |> should equal """ /// XML COMMENT A // Other comment -let f() = +let f () = // COMMENT A let y = 1 /// XML COMMENT B @@ -380,7 +380,7 @@ let hello() = "hello world" """ config |> prepend newline |> should equal """ -let hello() = "hello world" +let hello () = "hello world" (* This is a comment. *) """ diff --git a/src/Fantomas.Tests/CompilerDirectivesTests.fs b/src/Fantomas.Tests/CompilerDirectivesTests.fs index f54e113150..3dad6229a1 100644 --- a/src/Fantomas.Tests/CompilerDirectivesTests.fs +++ b/src/Fantomas.Tests/CompilerDirectivesTests.fs @@ -89,12 +89,12 @@ let ``line, file and path identifiers``() = """ config |> prepend newline |> should equal """ -let printSourceLocation() = +let printSourceLocation () = printfn "Line: %s" __LINE__ printfn "Source Directory: %s" __SOURCE_DIRECTORY__ printfn "Source File: %s" __SOURCE_FILE__ -printSourceLocation() +printSourceLocation () """ [] @@ -170,7 +170,7 @@ let [] private assemblyConfig() = |> prepend newline |> should equal """ [] -let private assemblyConfig() = +let private assemblyConfig () = #if TRACE let x = "" #else @@ -1021,7 +1021,7 @@ let f () = |> prepend newline |> should equal """ #if TEST -let f() = +let f () = async { let x = 2 return x @@ -1042,7 +1042,7 @@ let f () = |> prepend newline |> should equal """ #if TEST -let f() = +let f () = async { let x = 2 return x diff --git a/src/Fantomas.Tests/ComputationExpressionTests.fs b/src/Fantomas.Tests/ComputationExpressionTests.fs index 8025bc6160..c955d48a75 100644 --- a/src/Fantomas.Tests/ComputationExpressionTests.fs +++ b/src/Fantomas.Tests/ComputationExpressionTests.fs @@ -78,7 +78,7 @@ let s2 = seq { 0 .. 10 .. 100 } let rec inorder tree = seq { match tree with - | Tree(x, left, right) -> + | Tree (x, left, right) -> yield! inorder left yield x yield! inorder right @@ -108,7 +108,7 @@ async { |> prepend newline |> should equal """ async { - match! myAsyncFunction() with + match! myAsyncFunction () with | Some x -> printfn "%A" x | None -> printfn "Function returned None!" } @@ -158,9 +158,9 @@ parallel { |> should equal """ // Reads the values of x, y and z concurrently, then applies f to them ``parallel`` { - let! x = slowRequestX() - and! y = slowRequestY() - and! z = slowRequestZ() + let! x = slowRequestX () + and! y = slowRequestY () + and! z = slowRequestZ () return f x y z } """ diff --git a/src/Fantomas.Tests/ControlStructureTests.fs b/src/Fantomas.Tests/ControlStructureTests.fs index 11e8348e2d..b5a1c5e74f 100644 --- a/src/Fantomas.Tests/ControlStructureTests.fs +++ b/src/Fantomas.Tests/ControlStructureTests.fs @@ -53,12 +53,12 @@ let ``for loops``() = """ config |> prepend newline |> should equal """ -let function1() = +let function1 () = for i = 1 to 10 do printf "%d " i printfn "" -let function2() = +let function2 () = for i = 10 downto 1 do printf "%d " i printfn "" @@ -140,7 +140,7 @@ let function1 x y = if x = y then raise (InnerError("inner")) else raise (OuterError("outer")) with | Failure _ -> () - | InnerError(str) -> printfn "Error1 %s" str + | InnerError (str) -> printfn "Error1 %s" str finally printfn "Always print this." """ @@ -155,12 +155,12 @@ let ``range expressions``() = function2()""" config |> prepend newline |> should equal """ -let function2() = +let function2 () = for i in 1 .. 2 .. 10 do printf "%d " i printfn "" -function2() +function2 () """ [] @@ -431,7 +431,7 @@ let a ex = |> should equal """ let a ex = if null = ex then - fooo() + fooo () None // this was None elif ex.GetType() = typeof then diff --git a/src/Fantomas.Tests/DataStructureTests.fs b/src/Fantomas.Tests/DataStructureTests.fs index db20f8e451..9bab1cba49 100644 --- a/src/Fantomas.Tests/DataStructureTests.fs +++ b/src/Fantomas.Tests/DataStructureTests.fs @@ -1393,7 +1393,7 @@ let DotNamedIndexedPropertySet () = """ config |> prepend newline |> should equal """ -(foo()).Item(key) <- value +(foo ()).Item(key) <- value """ [] diff --git a/src/Fantomas.Tests/Fantomas.Tests.fsproj b/src/Fantomas.Tests/Fantomas.Tests.fsproj index c45aa8736d..646276cf33 100644 --- a/src/Fantomas.Tests/Fantomas.Tests.fsproj +++ b/src/Fantomas.Tests/Fantomas.Tests.fsproj @@ -56,12 +56,16 @@ - + + + + + diff --git a/src/Fantomas.Tests/FunctionDefinitionTests.fs b/src/Fantomas.Tests/FunctionDefinitionTests.fs index 22b2156ce7..9149ba3002 100644 --- a/src/Fantomas.Tests/FunctionDefinitionTests.fs +++ b/src/Fantomas.Tests/FunctionDefinitionTests.fs @@ -65,21 +65,21 @@ let ``should keep mutually recursive functions in nested function``() = let ``should keep identifiers with whitespace in double backticks``() = formatSourceString false """let ``should keep identifiers in double backticks``() = x """ config - |> should equal """let ``should keep identifiers in double backticks``() = x + |> should equal """let ``should keep identifiers in double backticks`` () = x """ [] let ``should remove backticks from shouldn't identifier``() = - formatSourceString false """let ``shouldn't``() = x + formatSourceString false """let ``shouldn't`` () = x """ config - |> should equal """let shouldn't() = x + |> should equal """let shouldn't () = x """ [] let ``should keep identifiers with + in double backticks``() = formatSourceString false """let ``Foo+Bar``() = x """ config - |> should equal """let ``Foo+Bar``() = x + |> should equal """let ``Foo+Bar`` () = x """ [] @@ -281,7 +281,7 @@ type U = X of int let f = fun x -> match x with - | X(x) -> x + | X (x) -> x """ [] diff --git a/src/Fantomas.Tests/InterfaceTests.fs b/src/Fantomas.Tests/InterfaceTests.fs index 1bb92f10af..56259f4a45 100644 --- a/src/Fantomas.Tests/InterfaceTests.fs +++ b/src/Fantomas.Tests/InterfaceTests.fs @@ -71,7 +71,7 @@ let ``object expressions and interfaces``() = member this.G() = () }""" config |> prepend newline |> should equal """ -let implementer() = +let implementer () = { new ISecond with member this.H() = () member this.J() = () @@ -91,7 +91,7 @@ let f () = member x.GetEnumerator() = null }""" config |> prepend newline |> should equal """ -let f() = +let f () = { new obj() with member x.ToString() = "INotifyEnumerableInternal" interface INotifyEnumerableInternal<'T> diff --git a/src/Fantomas.Tests/LetBindingTests.fs b/src/Fantomas.Tests/LetBindingTests.fs index eaa8689d1a..8994990d67 100644 --- a/src/Fantomas.Tests/LetBindingTests.fs +++ b/src/Fantomas.Tests/LetBindingTests.fs @@ -20,7 +20,7 @@ let f () = """ formatSourceString false codeSnippet config - |> should equal """let f() = + |> should equal """let f () = let x = 1 // the "in" keyword is available in F# let y = 2 x + y @@ -36,7 +36,7 @@ let f () = """ formatSourceString false codeSnippet config - |> should equal """let f() = + |> should equal """let f () = let x = 1 (* the "in" keyword is available in F# *) let y = 2 x + y @@ -52,7 +52,7 @@ let f () = """ formatSourceString false codeSnippet config - |> should equal """let f() = + |> should equal """let f () = let x = 1 if true then x else x """ @@ -66,7 +66,7 @@ let f () = """ formatSourceString false codeSnippet config - |> should equal """let f() = + |> should equal """let f () = let x = 1 (while true do () @@ -238,7 +238,7 @@ let ``inner let binding should not add additional newline, #475`` () = |> prepend newline |> should equal " module Test = - let testFunc() = + let testFunc () = let someObject = someStaticObject.Create (((fun o -> @@ -287,7 +287,7 @@ let ``newline trivia before simple sequence doesn't force remaining to get offse q b """ config - |> should equal """let a() = + |> should equal """let a () = let q = 1 q @@ -302,7 +302,7 @@ let ``comment trivia before simple sequence doesn't force remaining to get offse q b """ config - |> should equal """let a() = + |> should equal """let a () = let q = 1 // comment q @@ -350,7 +350,7 @@ let ``has symbol in signature requires paren, 564`` () = SpaceAfterComma = false SpaceAfterSemicolon = false SpaceAroundDelimiter = false - SpaceBeforeParenthesesInLowercaseFunctionDefinition = false }) + SpaceBeforeParameter = false }) |> prepend newline |> should equal """ module Bar = diff --git a/src/Fantomas.Tests/LongIdentWithDotsTests.fs b/src/Fantomas.Tests/LongIdentWithDotsTests.fs index 87e320c319..5721e52428 100644 --- a/src/Fantomas.Tests/LongIdentWithDotsTests.fs +++ b/src/Fantomas.Tests/LongIdentWithDotsTests.fs @@ -103,7 +103,7 @@ module Program let main _ = try try - Config.Logger.configure() + Config.Logger.configure () let config = ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).Build() diff --git a/src/Fantomas.Tests/NoTrailingSpacesTests.fs b/src/Fantomas.Tests/NoTrailingSpacesTests.fs index 75c9c98c37..4458e620ab 100644 --- a/src/Fantomas.Tests/NoTrailingSpacesTests.fs +++ b/src/Fantomas.Tests/NoTrailingSpacesTests.fs @@ -13,7 +13,7 @@ let ``should not confuse me with an extra space at end of line v2``() = |> should throw typeof""" formatSourceString false codeSnippet config - |> should equal """let ``should not extrude without positive distance``() = + |> should equal """let ``should not extrude without positive distance`` () = let args = [| "-i"; "input.dxf"; "-o"; "output.pdf"; "--op"; "extrude" |] (fun () -> parseCmdLine args |> ignore) |> should throw typeof """ \ No newline at end of file diff --git a/src/Fantomas.Tests/PatternMatchingTests.fs b/src/Fantomas.Tests/PatternMatchingTests.fs index 042ab98751..3509093132 100644 --- a/src/Fantomas.Tests/PatternMatchingTests.fs +++ b/src/Fantomas.Tests/PatternMatchingTests.fs @@ -152,13 +152,13 @@ let detect1 x = | 1 -> printfn "Found a 1!" | (var1: int) -> printfn "%d" var1 -let RegisterControl(control: Control) = +let RegisterControl (control: Control) = match control with | :? Button as button -> button.Text <- "Registered." | :? CheckBox as checkbox -> checkbox.Text <- "Registered." | _ -> () -let ReadFromFile(reader: System.IO.StreamReader) = +let ReadFromFile (reader: System.IO.StreamReader) = match reader.ReadLine() with | null -> printfn "\n" @@ -270,7 +270,7 @@ let x = |> should equal """ let x = match y with - | Start(-1) -> true + | Start (-1) -> true | _ -> false """ @@ -373,11 +373,11 @@ let (|OneLinerBinding|MultilineBinding|) b = |> should equal """ let (|OneLinerBinding|MultilineBinding|) b = match b with - | LetBinding([], PreXmlDoc [||], _, _, _, _, OneLinerExpr _) - | DoBinding([], PreXmlDoc [||], OneLinerExpr _) - | MemberBinding([], PreXmlDoc [||], _, _, _, _, OneLinerExpr _) - | PropertyBinding([], PreXmlDoc [||], _, _, _, _, OneLinerExpr _) - | ExplicitCtor([], PreXmlDoc [||], _, _, OneLinerExpr _, _) -> OneLinerBinding b + | LetBinding ([], PreXmlDoc [||], _, _, _, _, OneLinerExpr _) + | DoBinding ([], PreXmlDoc [||], OneLinerExpr _) + | MemberBinding ([], PreXmlDoc [||], _, _, _, _, OneLinerExpr _) + | PropertyBinding ([], PreXmlDoc [||], _, _, _, _, OneLinerExpr _) + | ExplicitCtor ([], PreXmlDoc [||], _, _, OneLinerExpr _, _) -> OneLinerBinding b | _ -> MultilineBinding b """ @@ -441,11 +441,11 @@ let internal ImageLoadResilient (f: unit -> 'a) (tidy: unit -> 'a) = |> should equal """ let internal ImageLoadResilient (f: unit -> 'a) (tidy: unit -> 'a) = try - f() + f () with | :? BadImageFormatException | :? ArgumentException - | :? IOException -> tidy() + | :? IOException -> tidy () """ [] diff --git a/src/Fantomas.Tests/PipingTests.fs b/src/Fantomas.Tests/PipingTests.fs index 70c8d40ea6..b448c77fae 100644 --- a/src/Fantomas.Tests/PipingTests.fs +++ b/src/Fantomas.Tests/PipingTests.fs @@ -56,7 +56,7 @@ let runAll() = |> Async.RunSynchronously |> ignore""" config |> prepend newline |> should equal """ -let runAll() = +let runAll () = urlList |> Seq.map fetchAsync |> Async.Parallel diff --git a/src/Fantomas.Tests/RecordTests.fs b/src/Fantomas.Tests/RecordTests.fs index 8aa40e94e7..9e0bc93402 100644 --- a/src/Fantomas.Tests/RecordTests.fs +++ b/src/Fantomas.Tests/RecordTests.fs @@ -148,7 +148,7 @@ let ``should not break inside of if statements in records``() = } """ { config with SemicolonAtEndOfLine = true } - |> should equal """let XpkgDefaults() = + |> should equal """let XpkgDefaults () = { ToolPath = "./tools/xpkg/xpkg.exe"; WorkingDir = "./"; TimeOut = TimeSpan.FromMinutes 5.; diff --git a/src/Fantomas.Tests/SignatureTests.fs b/src/Fantomas.Tests/SignatureTests.fs index dd2a2dfce3..d43e5febec 100644 --- a/src/Fantomas.Tests/SignatureTests.fs +++ b/src/Fantomas.Tests/SignatureTests.fs @@ -95,7 +95,7 @@ type A () = type A() = interface IA with - member x.F(f: unit -> _) = f() + member x.F(f: unit -> _) = f () """ [] diff --git a/src/Fantomas.Tests/SpaceBeforeArgumentsAndParametersTests.fs b/src/Fantomas.Tests/SpaceBeforeArgumentsAndParametersTests.fs deleted file mode 100644 index 758efddf38..0000000000 --- a/src/Fantomas.Tests/SpaceBeforeArgumentsAndParametersTests.fs +++ /dev/null @@ -1,259 +0,0 @@ -module Fantomas.Tests.SpaceBeforeArgumentsAndParametersTests - -open NUnit.Framework -open FsUnit -open Fantomas.Tests.TestHelper - -/// Space before () in Uppercase function call - -[] -let ``default config should not add space before unit in uppercase function call`` () = - formatSourceString false "let value = MyFunction()" config - |> should equal """let value = MyFunction() -""" - -[] -let ``SpaceBeforeUnitArgumentInUppercaseInvocation should add space before unit in uppercase function call`` () = - formatSourceString false "let value = MyFunction()" ({ config with SpaceBeforeUnitArgumentInUppercaseInvocation = true }) - |> should equal """let value = MyFunction () -""" - -[] -let ``SpaceBeforeUnitArgumentInUppercaseInvocation should add space before unit in chained uppercase function call`` () = - formatSourceString false "let value = person.ToString()" ({ config with SpaceBeforeUnitArgumentInUppercaseInvocation = true }) - |> should equal """let value = person.ToString () -""" - -/// Space before () in lowercase function call - -[] -let ``default config should not add space before unit in lowercase function call`` () = - formatSourceString false "let value = myFunction()" config - |> should equal """let value = myFunction() -""" - -[] -let ``SpaceBeforeUnitArgumentInLowercaseInvocation should add space before unit in lowercase function call`` () = - formatSourceString false "let value = myFunction()" ({ config with SpaceBeforeUnitArgumentInLowercaseInvocation = true }) - |> should equal """let value = myFunction () -""" - -// Exception to the rule - -[] -let ``SpaceBeforeUnitArgumentInUppercaseInvocation and SpaceBeforeUnitArgumentInLowercaseInvocation should not have impact when member is called after unit`` () = - formatSourceString false """let v1 = myFunction().Member -let v2 = OtherFunction().Member -""" ({ config with - SpaceBeforeUnitArgumentInUppercaseInvocation = true - SpaceBeforeUnitArgumentInLowercaseInvocation = true }) - |> prepend newline - |> should equal """ -let v1 = myFunction().Member -let v2 = OtherFunction().Member -""" - -// Space before parentheses (a+b) in Uppercase function call - -[] -let ``default config should not add space before parentheses in uppercase function call`` () = - formatSourceString false "let value = MyFunction(a+b)" config - |> should equal """let value = MyFunction(a + b) -""" - -[] -let ``SpaceBeforeParenthesisArgumentInUppercaseFunctionCall should add space before parentheses in uppercase function call`` () = - formatSourceString false "let value = MyFunction(a+b)" ({ config with SpaceBeforeParenthesesInUppercaseInvocation = true }) - |> should equal """let value = MyFunction (a + b) -""" - -// Space before parentheses (a+b) in lowercase function call - -[] -let ``default config should add space before parentheses in lowercase function call`` () = - formatSourceString false "let value = myFunction(a+b)" config - |> should equal """let value = myFunction (a + b) -""" - -[] -let ``SpaceBeforeParenthesisArgumentInUppercaseFunctionCall = false, should not add space before parentheses in lowercase function call`` () = - formatSourceString false "let value = myFunction(a+b)" ({ config with SpaceBeforeParenthesesInLowercaseInvocation = false }) - |> should equal """let value = myFunction(a + b) -""" - -// Space before unit in Uppercase function signature - -[] -let ``default config should not add space before unit in uppercase function definition`` () = - formatSourceString false "let Value () = x" config - |> should equal """let Value() = x -""" - -[] -let ``SpaceBeforeUnitParameterInUppercaseFunctionDefinition config should not add space before unit in uppercase function definition`` () = - formatSourceString false "let Value() = x" ({ config with SpaceBeforeUnitParameterInUppercaseFunctionDefinition = true }) - |> should equal """let Value () = x -""" - -// Space before unit in lowercase function definition - -[] -let ``default config should not add space before unit in lowercase function definition`` () = - formatSourceString false "let value () = x" config - |> should equal """let value() = x -""" - -[] -let ``SpaceBeforeUnitParameterInLowercaseFunctionDefinition config should add space before unit in lowercase function definition`` () = - formatSourceString false "let value() = x" ({ config with SpaceBeforeUnitParameterInLowercaseFunctionDefinition = true }) - |> should equal """let value () = x -""" - -// Space before parentheses (a+b) in Uppercase function definition - -[] -let ``default config should not add space before parentheses in uppercase function definition`` () = - formatSourceString false "let Value (a:int) = x" config - |> should equal """let Value(a: int) = x -""" - -[] -let ``SpaceBeforeParenthesisInUppercaseFunctionDefinition config should add space before parentheses in uppercase function definition`` () = - formatSourceString false "let Value(a:int) = x" ({ config with SpaceBeforeParenthesesInUppercaseFunctionDefinition = true }) - |> should equal """let Value (a: int) = x -""" - -[] -let ``SpaceBeforeParenthesisInUppercaseFunctionDefinition should add space after discrimintation union member`` () = - formatSourceString false """match x with -| Zero() -> () -| One (o) -> () -| Two(o,t) -> () -""" ({ config with SpaceBeforeParenthesesInUppercaseFunctionDefinition = true }) - |> prepend newline - |> should equal """ -match x with -| Zero() -> () -| One (o) -> () -| Two (o, t) -> () -""" - -// Space before parentheses (a+b) in lowercase function definition - -[] -let ``default config should not add space before parentheses in lowercase function definition`` () = - formatSourceString false "let value (a:int) = x" config - |> should equal """let value (a: int) = x -""" - -[] -let ``SpaceBeforeParenthesisInLowercaseFunctionDefinition = false, should not add space before parentheses in lowercase function definition`` () = - formatSourceString false "let value(a:int) = x" ({ config with SpaceBeforeParenthesesInLowercaseFunctionDefinition = false }) - |> should equal """let value(a: int) = x -""" - -// Space before unit in Uppercase class definition - -[] -let ``default config should not add space before unit in uppercase class definition`` () = - formatSourceString false "type Person () = class end" config - |> should equal """type Person() = - class - end -""" - -[] -let ``SpaceBeforeUnitParameterInUppercaseClassConstructor should add space after constructor of class`` () = - formatSourceString false """type Person () = - class end -""" ({ config with SpaceBeforeClassConstructor = true }) - |> prepend newline - |> should equal """ -type Person () = - class - end -""" - -// Space before unit in lowercase class definition - -[] -let ``default config should not add space before unit in lowercase class definition`` () = - formatSourceString false """type t () = - class - end -""" config - |> prepend newline - |> should equal """ -type t() = - class - end -""" - -[] -let ``SpaceBeforeUnitParameterInLowercaseClassConstructor should add space before unit in lowercase class definition`` () = - formatSourceString false """type t() = - class - end -""" ({ config with SpaceBeforeClassConstructor = true }) - |> prepend newline - |> should equal """ -type t () = - class - end -""" - -// Space before parentheses in Uppercase class definition - -[] -let ``default config should not add space before uppercase constructor of class`` () = - formatSourceString false """ -type Animal(length:int) = - class end -""" config - |> prepend newline - |> should equal """ -type Animal(length: int) = - class - end -""" - -[] -let ``SpaceBeforeParenthesisParameterInUppercaseClassConstructor should add space before uppercase constructor of class`` () = - formatSourceString false """ -type Animal(length:int) = - class end -""" ({ config with SpaceBeforeClassConstructor = true }) - |> prepend newline - |> should equal """ -type Animal (length: int) = - class - end -""" - -// Space before parentheses in lowercase class definition - -[] -let ``default config should not add space before lowercase constructor of class`` () = - formatSourceString false """ -type animal(length:int) = - class end -""" config - |> prepend newline - |> should equal """ -type animal(length: int) = - class - end -""" - -[] -let ``SpaceBeforeParenthesisParameterInUppercaseClassConstructor should add space before lowercase constructor of class`` () = - formatSourceString false """ -type animal(length:int) = - class end -""" ({ config with SpaceBeforeClassConstructor = true }) - |> prepend newline - |> should equal """ -type animal (length: int) = - class - end -""" diff --git a/src/Fantomas.Tests/SpaceBeforeClassConstructorTests.fs b/src/Fantomas.Tests/SpaceBeforeClassConstructorTests.fs new file mode 100644 index 0000000000..b30d2e814a --- /dev/null +++ b/src/Fantomas.Tests/SpaceBeforeClassConstructorTests.fs @@ -0,0 +1,113 @@ +module Fantomas.Tests.SpaceBeforeClassConstructorTests + +open NUnit.Framework +open FsUnit +open Fantomas.Tests.TestHelper + +let spaceBeforeConfig = { config with SpaceBeforeClassConstructor = true } + +// Space before unit in Uppercase class definition + +[] +let ``default config should not add space before unit in uppercase class definition`` () = + formatSourceString false "type Person () = class end" config + |> should equal """type Person() = + class + end +""" + +[] +let ``SpaceBeforeUnitParameterInUppercaseClassConstructor should add space after constructor of class`` () = + formatSourceString false """type Person () = + class end +""" spaceBeforeConfig + |> prepend newline + |> should equal """ +type Person () = + class + end +""" + +// Space before unit in lowercase class definition + +[] +let ``default config should not add space before unit in lowercase class definition`` () = + formatSourceString false """type t () = + class + end +""" config + |> prepend newline + |> should equal """ +type t() = + class + end +""" + +[] +let ``SpaceBeforeUnitParameterInLowercaseClassConstructor should add space before unit in lowercase class definition`` () = + formatSourceString false """type t() = + class + end +""" spaceBeforeConfig + |> prepend newline + |> should equal """ +type t () = + class + end +""" + +// Space before parentheses in Uppercase class definition + +[] +let ``default config should not add space before uppercase constructor of class`` () = + formatSourceString false """ +type Animal(length:int) = + class end +""" config + |> prepend newline + |> should equal """ +type Animal(length: int) = + class + end +""" + +[] +let ``SpaceBeforeParenthesisParameterInUppercaseClassConstructor should add space before uppercase constructor of class`` () = + formatSourceString false """ +type Animal(length:int) = + class end +""" spaceBeforeConfig + |> prepend newline + |> should equal """ +type Animal (length: int) = + class + end +""" + +// Space before parentheses in lowercase class definition + +[] +let ``default config should not add space before lowercase constructor of class`` () = + formatSourceString false """ +type animal(length:int) = + class end +""" config + |> prepend newline + |> should equal """ +type animal(length: int) = + class + end +""" + +[] +let ``SpaceBeforeParenthesisParameterInUppercaseClassConstructor should add space before lowercase constructor of class`` () = + formatSourceString false """ +type animal(length:int) = + class end +""" spaceBeforeConfig + |> prepend newline + |> should equal """ +type animal (length: int) = + class + end +""" diff --git a/src/Fantomas.Tests/SpaceBeforeLowercaseInvocationTests.fs b/src/Fantomas.Tests/SpaceBeforeLowercaseInvocationTests.fs new file mode 100644 index 0000000000..e44e446dc0 --- /dev/null +++ b/src/Fantomas.Tests/SpaceBeforeLowercaseInvocationTests.fs @@ -0,0 +1,43 @@ +module Fantomas.Tests.SpaceBeforeLowercaseInvocationTests + +open NUnit.Framework +open FsUnit +open Fantomas.Tests.TestHelper + +let noSpaceBefore = { config with SpaceBeforeLowercaseInvocation = false } + +/// Space before () in lowercase function call + +[] +let ``default config should add space before unit in lowercase function call`` () = + formatSourceString false "let value = myFunction()" config + |> should equal """let value = myFunction () +""" + +[] +let ``spaceBeforeLowercaseInvocation = false, should not add space before unit in lowercase function call`` () = + formatSourceString false "let value = myFunction()" noSpaceBefore + |> should equal """let value = myFunction() +""" + +// Space before parentheses (a+b) in lowercase function call + +[] +let ``default config should add space before parentheses in lowercase function call`` () = + formatSourceString false "let value = myFunction(a+b)" config + |> should equal """let value = myFunction (a + b) +""" + +[] +let ``spaceBeforeLowercaseInvocation = false, should not add space before parentheses in lowercase function call`` () = + formatSourceString false "let value = myFunction(a+b)" noSpaceBefore + |> should equal """let value = myFunction(a + b) +""" + +[] +let ``spaceBeforeLowercaseInvocation should not have impact when member is called after unit`` () = + formatSourceString false "let v1 = myFunction().Member" noSpaceBefore + |> prepend newline + |> should equal """ +let v1 = myFunction().Member +""" \ No newline at end of file diff --git a/src/Fantomas.Tests/SpaceBeforeMemberTests.fs b/src/Fantomas.Tests/SpaceBeforeMemberTests.fs new file mode 100644 index 0000000000..3bd8bfbb2f --- /dev/null +++ b/src/Fantomas.Tests/SpaceBeforeMemberTests.fs @@ -0,0 +1,43 @@ +module Fantomas.Tests.SpaceBeforeMemberTests + +open NUnit.Framework +open FsUnit +open Fantomas.Tests.TestHelper + +let spaceBeforeConfig = { config with SpaceBeforeMember = true } + +[] +let ``default config should not add a space before a type member`` () = + formatSourceString false """ +type Person() = + member this.Walk (distance:int) = () + member this.Sleep() = ignore + member __.singAlong () = () + member __.swim (duration:TimeSpan) = () +""" config + |> prepend newline + |> should equal """ +type Person() = + member this.Walk(distance: int) = () + member this.Sleep() = ignore + member __.singAlong() = () + member __.swim(duration: TimeSpan) = () +""" + +[] +let ``spaceBeforeMember should add a space before a type member`` () = + formatSourceString false """ +type Person() = + member this.Walk (distance:int) = () + member this.Sleep() = ignore + member __.singAlong () = () + member __.swim (duration:TimeSpan) = () +""" spaceBeforeConfig + |> prepend newline + |> should equal """ +type Person() = + member this.Walk (distance: int) = () + member this.Sleep () = ignore + member __.singAlong () = () + member __.swim (duration: TimeSpan) = () +""" \ No newline at end of file diff --git a/src/Fantomas.Tests/SpaceBeforeParameterTests.fs b/src/Fantomas.Tests/SpaceBeforeParameterTests.fs new file mode 100644 index 0000000000..a9c569eac8 --- /dev/null +++ b/src/Fantomas.Tests/SpaceBeforeParameterTests.fs @@ -0,0 +1,78 @@ +module Fantomas.Tests.SpaceBeforeParameterTests + +open NUnit.Framework +open FsUnit +open Fantomas.Tests.TestHelper + +let noSpaceBefore = { config with SpaceBeforeParameter = false } + +// Space before unit in Uppercase function signature + +[] +let ``default config should add space before unit in uppercase function definition`` () = + formatSourceString false "let Value () = x" config + |> should equal """let Value () = x +""" + +[] +let ``noSpaceBefore = false, should not add space before unit in uppercase function definition`` () = + formatSourceString false "let Value() = x" noSpaceBefore + |> should equal """let Value() = x +""" + +// Space before unit in lowercase function definition + +[] +let ``default config should add space before unit in lowercase function definition`` () = + formatSourceString false "let value () = x" config + |> should equal """let value () = x +""" + +[] +let ``spaceBeforeParameter = false, should not add space before unit in lowercase function definition`` () = + formatSourceString false "let value() = x" noSpaceBefore + |> should equal """let value() = x +""" + +// Space before parentheses (a+b) in Uppercase function definition + +[] +let ``default config should add space before parentheses in uppercase function definition`` () = + formatSourceString false "let Value (a:int) = x" config + |> should equal """let Value (a: int) = x +""" + +[] +let ``spaceBeforeParameter = false, should not add space before parentheses in uppercase function definition`` () = + formatSourceString false "let Value(a:int) = x" noSpaceBefore + |> should equal """let Value(a: int) = x +""" + +[] +let ``default config should add space after discrimintation union member`` () = + formatSourceString false """match x with +| Zero() -> () +| One (o) -> () +| Two(o,t) -> () +""" config + |> prepend newline + |> should equal """ +match x with +| Zero () -> () +| One (o) -> () +| Two (o, t) -> () +""" + +// Space before parentheses (a+b) in lowercase function definition + +[] +let ``default config should add space before parentheses in lowercase function definition`` () = + formatSourceString false "let value(a:int) = x" config + |> should equal """let value (a: int) = x +""" + +[] +let ``spaceBeforeParameter = false, should not add space before parentheses in lowercase function definition`` () = + formatSourceString false "let value (a:int) = x" noSpaceBefore + |> should equal """let value(a: int) = x +""" diff --git a/src/Fantomas.Tests/SpaceBeforeUppercaseInvocationTests.fs b/src/Fantomas.Tests/SpaceBeforeUppercaseInvocationTests.fs new file mode 100644 index 0000000000..dc2cdc3ffb --- /dev/null +++ b/src/Fantomas.Tests/SpaceBeforeUppercaseInvocationTests.fs @@ -0,0 +1,51 @@ +module Fantomas.Tests.SpaceBeforeUppercaseInvocationTests + +open NUnit.Framework +open FsUnit +open Fantomas.Tests.TestHelper + +let spaceBeforeConfig = { config with SpaceBeforeUppercaseInvocation = true } + +/// Space before () in Uppercase function call + +[] +let ``default config should not add space before unit in uppercase function call`` () = + formatSourceString false "let value = MyFunction()" config + |> should equal """let value = MyFunction() +""" + +[] +let ``spaceBeforeUppercaseInvocation should add space before unit in uppercase function call`` () = + formatSourceString false "let value = MyFunction()" spaceBeforeConfig + |> should equal """let value = MyFunction () +""" + +[] +let ``spaceBeforeUppercaseInvocation should add space before unit in chained uppercase function call`` () = + formatSourceString false "let value = person.ToString()" spaceBeforeConfig + |> should equal """let value = person.ToString () +""" + +// Exception to the rule + +[] +let ``spaceBeforeUppercaseInvocation should not have impact when member is called after unit`` () = + formatSourceString false "let v2 = OtherFunction().Member" spaceBeforeConfig + |> prepend newline + |> should equal """ +let v2 = OtherFunction().Member +""" + +// Space before parentheses (a+b) in Uppercase function call + +[] +let ``default config should not add space before parentheses in uppercase function call`` () = + formatSourceString false "let value = MyFunction(a+b)" config + |> should equal """let value = MyFunction(a + b) +""" + +[] +let ``spaceBeforeUppercaseInvocation should add space before parentheses in uppercase function call`` () = + formatSourceString false "let value = MyFunction(a+b)" spaceBeforeConfig + |> should equal """let value = MyFunction (a + b) +""" \ No newline at end of file diff --git a/src/Fantomas.Tests/StructTests.fs b/src/Fantomas.Tests/StructTests.fs index 1ca6363119..38ba3882ea 100644 --- a/src/Fantomas.Tests/StructTests.fs +++ b/src/Fantomas.Tests/StructTests.fs @@ -80,7 +80,7 @@ match t with type S = S of struct (int * int) let g: struct (int * int) = struct (1, 1) -let z = fun ((S(struct (u, v))): S) -> u + v +let z = fun ((S (struct (u, v))): S) -> u + v let t = struct (1, 2) match t with diff --git a/src/Fantomas.Tests/TypeDeclarationTests.fs b/src/Fantomas.Tests/TypeDeclarationTests.fs index 624ae2ea3d..efa3495333 100644 --- a/src/Fantomas.Tests/TypeDeclarationTests.fs +++ b/src/Fantomas.Tests/TypeDeclarationTests.fs @@ -32,11 +32,11 @@ let ``type annotations``() = |> prepend newline |> should equal """ let iterate1 (f: unit -> seq) = - for e in f() do + for e in f () do printfn "%d" e let iterate2 (f: unit -> #seq) = - for e in f() do + for e in f () do printfn "%d" e """ @@ -333,7 +333,7 @@ let CalculateFine (ticket : SpeedingTicket) = type SpeedingTicket() = member this.GetMPHOver(speed: int, limit: int) = speed - limit -let CalculateFine(ticket: SpeedingTicket) = +let CalculateFine (ticket: SpeedingTicket) = let delta = ticket.GetMPHOver(limit = 55, speed = 70) if delta < 20 then 50.0 else 100.0 """ @@ -717,7 +717,7 @@ type A() = with set v = let x = match _kbytes.GetAddress(8) with - | Some(x) -> x + | Some (x) -> x | None -> null ignore x """ @@ -750,7 +750,7 @@ type Bar = member this.Item with get(i : string) = match mo with - | Some(m) when m.Groups.[i].Success -> m.Groups.[i].Value + | Some (m) when m.Groups.[i].Success -> m.Groups.[i].Value | _ -> null""" config |> prepend newline |> should equal """ @@ -759,13 +759,13 @@ type Bar = member this.Item with get (i: int) = match mo with - | Some(m) when m.Groups.[i].Success -> m.Groups.[i].Value + | Some (m) when m.Groups.[i].Success -> m.Groups.[i].Value | _ -> null member this.Item with get (i: string) = match mo with - | Some(m) when m.Groups.[i].Success -> m.Groups.[i].Value + | Some (m) when m.Groups.[i].Success -> m.Groups.[i].Value | _ -> null """ @@ -851,7 +851,7 @@ let ``type abbreviation augmentation``() = [] let ``operator in words should not print to symbol, 409`` () = formatSourceString false """type T() = - static member op_LessThan(a, b) = a < b""" config + static member op_LessThan(a, b) = a < b""" ({ config with SpaceBeforeMember = true }) |> should equal """type T() = static member op_LessThan (a, b) = a < b """ @@ -1026,8 +1026,8 @@ open Swensen.Unquote type FormattingSpecs() = [] - let ``true is true``() = test <@ true = true @> + let ``true is true`` () = test <@ true = true @> [] - let ``false is false``() = test <@ false = false @> + let ``false is false`` () = test <@ false = false @> """ \ No newline at end of file diff --git a/src/Fantomas.Tests/UnionTests.fs b/src/Fantomas.Tests/UnionTests.fs index 8cf6a0d80e..2405eb4a3d 100644 --- a/src/Fantomas.Tests/UnionTests.fs +++ b/src/Fantomas.Tests/UnionTests.fs @@ -57,9 +57,9 @@ type Type = | TyCon of string * Type list override this.ToString() = match this with - | TyLam(t1, t2) -> sprintf "(%s -> %s)" (t1.ToString()) (t2.ToString()) + | TyLam (t1, t2) -> sprintf "(%s -> %s)" (t1.ToString()) (t2.ToString()) | TyVar a -> a - | TyCon(s, ts) -> s + | TyCon (s, ts) -> s """ [] diff --git a/src/Fantomas/CodePrinter.fs b/src/Fantomas/CodePrinter.fs index 17f05d2af0..a23567b660 100644 --- a/src/Fantomas/CodePrinter.fs +++ b/src/Fantomas/CodePrinter.fs @@ -34,46 +34,45 @@ type ASTContext = IsFirstTypeParam: bool /// Check whether the context is inside DotGet to suppress whitespaces IsInsideDotGet: bool + /// Check whether the context is inside a SynMemberDefn.Member(memberDefn,range) + /// This is required to correctly detect the setting SpaceBeforeMember + IsMemberDefinition: bool } static member Default = { TopLevelModuleName = "" IsFirstChild = false; InterfaceRange = None IsCStylePattern = false; IsNakedRange = false HasVerticalBar = false; IsUnionField = false - IsFirstTypeParam = false; IsInsideDotGet = false } + IsFirstTypeParam = false; IsInsideDotGet = false + IsMemberDefinition = false } let rec addSpaceBeforeParensInFunCall functionOrMethod arg (ctx:Context) = match functionOrMethod, arg with | SynExpr.TypeApp(e, _, _, _, _, _, _), _ -> addSpaceBeforeParensInFunCall e arg ctx | UppercaseSynExpr, ConstExpr(Const "()", _) -> - ctx.Config.SpaceBeforeUnitArgumentInUppercaseInvocation + ctx.Config.SpaceBeforeUppercaseInvocation | LowercaseSynExpr, ConstExpr(Const "()", _) -> - ctx.Config.SpaceBeforeUnitArgumentInLowercaseInvocation + ctx.Config.SpaceBeforeLowercaseInvocation | SynExpr.Ident(_), SynExpr.Ident(_) -> true | UppercaseSynExpr, Paren(_) -> - ctx.Config.SpaceBeforeParenthesesInUppercaseInvocation + ctx.Config.SpaceBeforeUppercaseInvocation | LowercaseSynExpr, Paren(_) -> - ctx.Config.SpaceBeforeParenthesesInLowercaseInvocation + ctx.Config.SpaceBeforeLowercaseInvocation | _ -> true -let addSpaceBeforeParensInFunDef (functionOrMethod:string) args (ctx:Context) = +let addSpaceBeforeParensInFunDef (astContext: ASTContext) (functionOrMethod:string) args (ctx:Context) = let isLastPartUppercase = let parts = functionOrMethod.Split '.' Char.IsUpper parts.[parts.Length - 1].[0] match functionOrMethod, args with | "new", _ -> false - | _, PatParen (PatConst(Const "()", _)) -> - if isLastPartUppercase - then ctx.Config.SpaceBeforeUnitParameterInUppercaseFunctionDefinition - else ctx.Config.SpaceBeforeUnitParameterInLowercaseFunctionDefinition - | _, PatParen(_) -> - if isLastPartUppercase - then ctx.Config.SpaceBeforeParenthesesInUppercaseFunctionDefinition - else ctx.Config.SpaceBeforeParenthesesInLowercaseFunctionDefinition + if astContext.IsMemberDefinition + then ctx.Config.SpaceBeforeMember + else ctx.Config.SpaceBeforeParameter | (_:string), _ -> not isLastPartUppercase | _ -> true @@ -676,7 +675,7 @@ and genMemberBinding astContext b = let prefix = genPreXmlDoc px +> genAttributes astContext ats +> genMemberFlagsForMemberBinding astContext mf b.RangeOfBindingAndRhs - +> ifElse isInline (!- "inline ") sepNone +> opt sepSpace ao genAccess +> genPat astContext p + +> ifElse isInline (!- "inline ") sepNone +> opt sepSpace ao genAccess +> genPat ({ astContext with IsMemberDefinition = true }) p match e with | TypedExpr(Typed, e, t) -> prefix +> sepColon +> genType astContext false t +> sepEq +> preserveBreakNlnOrAddSpace astContext e @@ -2281,7 +2280,7 @@ and genPat astContext pat = aoc +> genPat astContext p1 -- " :: " +> genPat astContext p2 | [(ido, p) as ip] -> aoc +> infixOperatorFromTrivia pat.Range s +> tpsoc +> - ifElse (hasParenInPat p || Option.isSome ido) (ifElseCtx (addSpaceBeforeParensInFunDef s p) sepSpace sepNone) sepSpace + ifElse (hasParenInPat p || Option.isSome ido) (ifElseCtx (addSpaceBeforeParensInFunDef astContext s p) sepSpace sepNone) sepSpace +> ifElse (Option.isSome ido) (sepOpenT +> genPatWithIdent astContext ip +> sepCloseT) (genPatWithIdent astContext ip) // This pattern is potentially long | ps -> diff --git a/src/Fantomas/FormatConfig.fs b/src/Fantomas/FormatConfig.fs index 5baf99aa3a..4dadcc47f1 100644 --- a/src/Fantomas/FormatConfig.fs +++ b/src/Fantomas/FormatConfig.fs @@ -15,15 +15,11 @@ type FormatConfig = /// The column where we break to new lines PageWidth : Num SemicolonAtEndOfLine : bool - SpaceBeforeUnitArgumentInUppercaseInvocation : bool - SpaceBeforeUnitArgumentInLowercaseInvocation : bool - SpaceBeforeParenthesesInUppercaseInvocation : bool - SpaceBeforeParenthesesInLowercaseInvocation : bool - SpaceBeforeUnitParameterInUppercaseFunctionDefinition : bool - SpaceBeforeUnitParameterInLowercaseFunctionDefinition : bool - SpaceBeforeParenthesesInUppercaseFunctionDefinition : bool - SpaceBeforeParenthesesInLowercaseFunctionDefinition : bool + SpaceBeforeParameter: bool + SpaceBeforeLowercaseInvocation: bool + SpaceBeforeUppercaseInvocation: bool SpaceBeforeClassConstructor : bool + SpaceBeforeMember : bool SpaceBeforeColon : bool SpaceAfterComma : bool SpaceBeforeSemicolon : bool @@ -41,15 +37,11 @@ type FormatConfig = { IndentSpaceNum = 4 PageWidth = 120 SemicolonAtEndOfLine = false - SpaceBeforeUnitArgumentInUppercaseInvocation = false - SpaceBeforeUnitArgumentInLowercaseInvocation = false - SpaceBeforeParenthesesInUppercaseInvocation = false - SpaceBeforeParenthesesInLowercaseInvocation = true - SpaceBeforeUnitParameterInUppercaseFunctionDefinition = false - SpaceBeforeUnitParameterInLowercaseFunctionDefinition = false - SpaceBeforeParenthesesInUppercaseFunctionDefinition = false - SpaceBeforeParenthesesInLowercaseFunctionDefinition = true + SpaceBeforeParameter = true + SpaceBeforeLowercaseInvocation = true + SpaceBeforeUppercaseInvocation = false SpaceBeforeClassConstructor = false + SpaceBeforeMember = false SpaceBeforeColon = false SpaceAfterComma = true SpaceBeforeSemicolon = false From 56452ecf58ab9fd56190f58ae87df6b12853cf7e Mon Sep 17 00:00:00 2001 From: nojaf Date: Tue, 17 Mar 2020 21:03:55 +0100 Subject: [PATCH 11/12] Update schema.json --- src/Fantomas/schema.json | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/src/Fantomas/schema.json b/src/Fantomas/schema.json index 7fc42823ba..a3e52daa78 100644 --- a/src/Fantomas/schema.json +++ b/src/Fantomas/schema.json @@ -11,31 +11,19 @@ "SemicolonAtEndOfLine": { "type": "boolean" }, - "SpaceBeforeUnitArgumentInUppercaseInvocation": { + "SpaceBeforeParameter": { "type": "boolean" }, - "SpaceBeforeUnitArgumentInLowercaseInvocation": { + "SpaceBeforeLowercaseInvocation": { "type": "boolean" }, - "SpaceBeforeParenthesesInUppercaseInvocation": { + "SpaceBeforeUppercaseInvocation": { "type": "boolean" }, - "SpaceBeforeParenthesesInLowercaseInvocation": { + "SpaceBeforeClassConstructor": { "type": "boolean" }, - "SpaceBeforeUnitParameterInUppercaseFunctionDefinition": { - "type": "boolean" - }, - "SpaceBeforeUnitParameterInLowercaseFunctionDefinition": { - "type": "boolean" - }, - "SpaceBeforeParenthesesInUppercaseFunctionDefinition": { - "type": "boolean" - }, - "SpaceBeforeParenthesesInLowercaseFunctionDefinition": { - "type": "boolean" - }, - "SpaceBeforeClassConstructor": { + "SpaceBeforeMember": { "type": "boolean" }, "SpaceBeforeColon": { From b792c1ddd5570e926be67227a9d8efbf2a464cb8 Mon Sep 17 00:00:00 2001 From: nojaf Date: Tue, 17 Mar 2020 21:09:45 +0100 Subject: [PATCH 12/12] Remove documentation for space before parameters. --- docs/Documentation.md | 158 ------------------------------------------ 1 file changed, 158 deletions(-) diff --git a/docs/Documentation.md b/docs/Documentation.md index 09fefed048..8c9f5b7926 100644 --- a/docs/Documentation.md +++ b/docs/Documentation.md @@ -132,164 +132,6 @@ add semicolons at the end of lines e.g. Mass = 0.0002858859807 * solarMass } ``` -##### `--noSpaceBeforeArgument` - -This option has been split into multiple settings. -These can be configured by passing a `fantomas-config.json` path to the `--config` flag. - -###### `"SpaceBeforeUnitArgumentInUppercaseInvocation"` - -> default false - -Will add a whitespace before `()` argument when the function name starts with an uppercase character. e.g. - -```fsharp -let value = MyFunction() -let value = person.ToString() -``` - -becomes - -```fsharp -let value = MyFunction () -let value = person.ToString () -``` - -###### `"SpaceBeforeUnitArgumentInLowercaseInvocation"` - -Will add a whitespace before `()` argument when the function name starts with a lowercase character. e.g. - -> default false - -```fsharp -let value = myFunction() -``` - -becomes - -```fsharp -let value = myFunction () -``` - -###### `"SpaceBeforeParenthesesInUppercaseInvocation"` - -> default false - -Will add a whitespace before an argument wrapped with parentheses when the function name starts with an uppercase character. e.g. - -```fsharp -let value = MyFunction(a+b) -``` - -becomes - -```fsharp -let value = MyFunction (a + b) -``` - -###### `"SpaceBeforeParenthesesInLowercaseInvocation"` - -> default true - -Will add a whitespace before an argument wrapped with parentheses when the function name starts with an lowercase character. e.g. - -```fsharp -let value = myFunction(a+b) -``` - -becomes - -```fsharp -let value = myFunction (a+b) -``` - -To remove the space, set `"SpaceBeforeParenthesesInLowercaseInvocation"` to `false`. - -###### `"SpaceBeforeUnitParameterInUppercaseFunctionDefinition"` - -> default false - -Will add a whitespace before a `()` parameter in a function definition that starts with an uppercase letter. e.g. - -```fsharp -let Value() = x -``` - -becomes - -```fsharp -let Value () = x -``` - -###### `"SpaceBeforeUnitParameterInLowercaseFunctionDefinition"` - -> default false - -Will add a whitespace before a `()` parameter in a function definition that starts with an lowercase letter. e.g. - -```fsharp -let value() = x -``` - -becomes - -```fsharp -let value () = x -``` - -###### `"SpaceBeforeParenthesesInUppercaseFunctionDefinition"` - -> default false - -Will add a whitespace before a parameters wrapped with parentheses that starts with an uppercase letter. e.g. - -```fsharp -let Value(a:int) = x -``` - -becomes - -```fsharp -let Value (a:int) = x -``` - -###### `SpaceBeforeParenthesesInLowercaseFunctionDefinition"` - -> default true - -Will add a whitespace before a parameters wrapped with parentheses that starts with an uppercase letter. e.g. - -```fsharp -let value(a:int) = x -``` - -becomes - -```fsharp -let value (a:int) = x -``` - -To remove the space, set `"SpaceBeforeParenthesesInLowercaseFunctionDefinition"` to `false`. - -###### `"SpaceBeforeClassConstructor"` - -> default false - -Will add a whitespace before a constructor of class definition. - -```fsharp -type Person() = - class end -``` - -becomes - -```fsharp -type Person () = - class - end -``` - ##### `--spaceBeforeColon` if being set, there is a space before `:` e.g.