From aeda84073ffaf63abfe1f1192ea6558b5974253d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jind=C5=99ich=20Iv=C3=A1nek?= Date: Thu, 13 Jan 2022 08:13:52 +0100 Subject: [PATCH] Fix #2013: Consider true, false as compiler define identifiers. (#2019) * Consider true, false as compiler define identifiers. Fix #2013 --- src/Fantomas.Tests/CompilerDirectivesTests.fs | 23 +++++++++++++ .../TokenParserBoolExprTests.fs | 32 +++++++++++++++++-- src/Fantomas/TokenParser.fs | 4 ++- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/Fantomas.Tests/CompilerDirectivesTests.fs b/src/Fantomas.Tests/CompilerDirectivesTests.fs index a6e7566a61..b53fd06cea 100644 --- a/src/Fantomas.Tests/CompilerDirectivesTests.fs +++ b/src/Fantomas.Tests/CompilerDirectivesTests.fs @@ -2692,3 +2692,26 @@ try with | _ -> () """ + +[] +let ``should handle #if with boolean constant`` () = + formatSourceString + false + """ +#if false +let x = 1 +#endif +#if true +let x = 1 +#endif +""" + config + |> should + equal + """#if false +let x = 1 +#endif +#if true +let x = 1 +#endif +""" diff --git a/src/Fantomas.Tests/TokenParserBoolExprTests.fs b/src/Fantomas.Tests/TokenParserBoolExprTests.fs index a8d4f0357f..1862b28ebd 100644 --- a/src/Fantomas.Tests/TokenParserBoolExprTests.fs +++ b/src/Fantomas.Tests/TokenParserBoolExprTests.fs @@ -38,6 +38,28 @@ let x = 1 ) )) +let ``Get define exprs from boolean constant`` booleanConstString = + let source = + (sprintf + """ +#if %s +let x = 1 +#endif +""" + booleanConstString) + + getDefineExprs source + |> List.head + |> should equal (BoolExpr.Ident booleanConstString) + +[] +let ``Get define exprs from boolean constant (true)`` () = + ``Get define exprs from boolean constant`` "true" + +[] +let ``Get define exprs from boolean constant (false)`` () = + ``Get define exprs from boolean constant`` "false" + [] let ``simple compiler directive - else expr`` () = let source = @@ -212,8 +234,14 @@ type BoolExprGenerator = static member SimpleIdent() = { new Arbitrary() with member x.Generator = - Gen.choose (int 'A', int 'Z') - |> Gen.map (char >> string) + let idents = + Gen.choose (int 'A', int 'Z') + |> Gen.map (char >> string) + + let constants = Gen.elements [ "true"; "false" ] + + Gen.frequency [ 10, idents + 1, constants ] member x.Shrinker t = Seq.empty } diff --git a/src/Fantomas/TokenParser.fs b/src/Fantomas/TokenParser.fs index 660c42688e..bc76ac1d94 100644 --- a/src/Fantomas/TokenParser.fs +++ b/src/Fantomas/TokenParser.fs @@ -373,7 +373,7 @@ and tokenize defines (hashTokens: Token list) (content: string) : Token list = let getDefinesWords (tokens: Token list) = tokens - |> List.filter (fun { TokenInfo = { TokenName = tn } } -> tn = "IDENT") + |> List.filter (fun { TokenInfo = { TokenName = tn } } -> tn = "IDENT" || tn = "FALSE" || tn = "TRUE") |> List.map (fun t -> t.Content) |> List.distinct @@ -385,6 +385,8 @@ let getDefineExprs (hashTokens: Token list) = |> Seq.filter (fun t -> t.TokenInfo.TokenName = "IDENT" + || t.TokenInfo.TokenName = "TRUE" + || t.TokenInfo.TokenName = "FALSE" || Set.contains t.Content allowedContent) |> Seq.map (fun t -> t.Content) |> Seq.toList