Skip to content

Commit

Permalink
- improve Option handling
Browse files Browse the repository at this point in the history
- improve string calls
  • Loading branch information
dawedawe committed Dec 5, 2023
1 parent d1be55c commit dc415ef
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 17 deletions.
33 changes: 22 additions & 11 deletions src/Fantomas.Core/ASTTransformer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,16 @@ let mkSynMatchClause creationAide (SynMatchClause(p, eo, e, range, _, trivia)) :
| None -> range
| Some barRange -> unionRanges barRange range

let arrowRange =
match trivia.ArrowRange with
| Some r -> r
| None -> failwith $"unable to get the arrow range from trivia in {nameof mkSynMatchClause}"

MatchClauseNode(
Option.map (stn "|") trivia.BarRange,
mkPat creationAide p,
Option.map (mkExpr creationAide) eo,
stn "->" trivia.ArrowRange.Value,
stn "->" arrowRange,
mkExpr creationAide e,
fullRange
)
Expand Down Expand Up @@ -555,12 +560,12 @@ let rec (|ElIf|_|) =

let (|ConstNumberExpr|_|) =
function
| SynExpr.Const(SynConst.Double v, m) -> Some(string v, m)
| SynExpr.Const(SynConst.Decimal v, m) -> Some(string v, m)
| SynExpr.Const(SynConst.Single v, m) -> Some(string v, m)
| SynExpr.Const(SynConst.Int16 v, m) -> Some(string v, m)
| SynExpr.Const(SynConst.Int32 v, m) -> Some(string v, m)
| SynExpr.Const(SynConst.Int64 v, m) -> Some(string v, m)
| SynExpr.Const(SynConst.Double v, m) -> Some(string<double> v, m)
| SynExpr.Const(SynConst.Decimal v, m) -> Some(string<decimal> v, m)
| SynExpr.Const(SynConst.Single v, m) -> Some(string<single> v, m)
| SynExpr.Const(SynConst.Int16 v, m) -> Some(string<int16> v, m)
| SynExpr.Const(SynConst.Int32 v, m) -> Some(string<int> v, m)
| SynExpr.Const(SynConst.Int64 v, m) -> Some(string<int64> v, m)
| _ -> None

let (|App|_|) e =
Expand Down Expand Up @@ -1792,7 +1797,13 @@ let mkBinding
ao, Choice1Of2(IdentListNode([ name ], m)), None, []
| _ -> None, Choice2Of2(mkPat creationAide pat), None, []

let equals = stn "=" trivia.EqualsRange.Value
let equals =
let equalsRange =
match trivia.EqualsRange with
| Some r -> r
| None -> failwith $"failed to get equals range in {nameof mkBinding}"

stn "=" equalsRange

let e = parseExpressionInSynBinding returnInfo expr

Expand Down Expand Up @@ -2040,7 +2051,7 @@ let mkSynRationalConst (creationAide: CreationAide) rc =
let rec visit rc =
match rc with
| SynRationalConst.Integer(i, range) ->
stn (creationAide.TextFromSource (fun () -> string i) range) range
stn (creationAide.TextFromSource (fun () -> string<int> i) range) range
|> RationalConstNode.Integer

| SynRationalConst.Paren(SynRationalConst.Rational(numerator,
Expand All @@ -2057,12 +2068,12 @@ let mkSynRationalConst (creationAide: CreationAide) rc =
stn "(" r

let n =
stn (creationAide.TextFromSource (fun () -> string numerator) numeratorRange) numeratorRange
stn (creationAide.TextFromSource (fun () -> string<int> numerator) numeratorRange) numeratorRange

let div = stn "/" divRange

let d =
stn (creationAide.TextFromSource (fun () -> string denominator) denominatorRange) denominatorRange
stn (creationAide.TextFromSource (fun () -> string<int> denominator) denominatorRange) denominatorRange

let closingParen =
let r =
Expand Down
5 changes: 3 additions & 2 deletions src/Fantomas.Core/SyntaxOak.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2817,8 +2817,9 @@ type MeasureDivideNode(lhs: Measure option, operator: SingleTextNode, rhs: Measu
inherit NodeBase(range)

override val Children: Node array =
[| if Option.isSome lhs then
yield Measure.Node lhs.Value
[| match lhs with
| Some n -> yield Measure.Node n
| None -> ()
yield operator
yield Measure.Node rhs |]

Expand Down
20 changes: 17 additions & 3 deletions src/Fantomas.Tests/IgnoreFileTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,12 @@ let ``IgnoreFile.find preferentially finds the fantomasignore next to the source
|> makeFileHierarchy fs

let loadIgnoreList, getLoads = oneShotLoader (fun _ -> failwith "never called")
let ignoreFile = IgnoreFile.find fs loadIgnoreList source |> Option.get

let ignoreFile =
match IgnoreFile.find fs loadIgnoreList source with
| Some f -> f
| None -> failwith $"calling {nameof IgnoreFile.find} failed"

ignoreFile.Location.FullName |> shouldEqual target
getLoads () |> shouldEqual (Set.ofList [ target ])

Expand All @@ -117,7 +122,12 @@ let ``IgnoreFile.find can find the fantomasignore one layer up from the source f
|> makeFileHierarchy fs

let loadIgnoreList, getLoads = oneShotLoader (fun _ -> failwith "never called")
let ignoreFile = IgnoreFile.find fs loadIgnoreList source |> Option.get

let ignoreFile =
match IgnoreFile.find fs loadIgnoreList source with
| Some f -> f
| None -> failwith $"calling {nameof IgnoreFile.find} failed"

ignoreFile.Location.FullName |> shouldEqual target
getLoads () |> shouldEqual (Set.ofList [ target ])

Expand All @@ -139,7 +149,11 @@ let ``IgnoreFile.current' does not load more than once`` () =
getLoads () |> shouldBeEmpty

for _ in 1..2 do
let forced = ignoreFile.Force() |> Option.get
let forced =
match ignoreFile.Force() with
| Some f -> f
| None -> failwith $"calling {nameof ignoreFile.Force} failed"

forced.Location.FullName |> shouldEqual target
// The second invocation would throw if we were somehow getting the
// singleton wrong and re-invoking the find-and-load.
Expand Down
5 changes: 4 additions & 1 deletion src/Fantomas.Tests/Integration/ConfigTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ let valid_eol_settings = [ "lf"; "crlf" ]

[<TestCaseSource("valid_eol_settings")>]
let ``uses end_of_line setting to write user newlines`` setting =
let newline = (EndOfLineStyle.OfConfigString setting).Value.NewLineString
let newline =
match EndOfLineStyle.OfConfigString setting with
| Some nl -> nl.NewLineString
| None -> failwith $"unable to get {nameof EndOfLineStyle.OfConfigString}"

let sampleCode nln =
sprintf "let a = 9%s%slet b = 7%s" nln nln nln
Expand Down

0 comments on commit dc415ef

Please sign in to comment.