fsprojects / fantomas

FSharp source code formatter
https://fsprojects.github.io/fantomas
Other
773 stars 194 forks source link

Many Ifdefs seem to cause parse error #3013

Open TheAngryByrd opened 9 months ago

TheAngryByrd commented 9 months ago

Issue created from fantomas-online

Code

module FsToolkit.ErrorHandling.Tests

#if FABLE_COMPILER_PYTHON
open Fable.Pyxpecto
#endif
#if FABLE_COMPILER_JAVASCRIPT
open Fable.Mocha
#endif
#if !FABLE_COMPILER
open Expecto
//needed for `dotnet test` to work
[<Tests>]
#endif
let allTests =
    testList "All Tests" [
        ResultTests.allTests

    ]

[<EntryPoint>]
let main argv =
#if FABLE_COMPILER_PYTHON
    Pyxpecto.runTests allTests
#endif
#if FABLE_COMPILER_JAVASCRIPT
    Mocha.runTests allTests
#endif
#if !FABLE_COMPILER
    Tests.runTestsWithCLIArgs [] Array.empty allTests
#endif

Error

Fantomas.Core.ParseException: ParseException
  [{ Severity = Error
     SubCategory = "parse"
     Range = Some (30,0--30,6)
     ErrorNumber = Some 58
     Message =
      "Unexpected syntax or possible incorrect indentation: this token is offside of context started at position (21:1). Try indenting this further.
To continue using non-conforming indentation, pass the '--strict-indentation-' flag to the compiler, or set the language version to F# 7." };
   { Severity = Error
     SubCategory = "parse"
     Range = Some (30,0--30,6)
     ErrorNumber = Some 10
     Message =
      "Incomplete structured construct at or before this point in binding" }]
   at Fantomas.Core.CodeFormatterImpl.parse@39-6.Invoke(Unit unitVar) in /_//src/Fantomas.Core/CodeFormatterImpl.fs:line 47
   at Microsoft.FSharp.Control.AsyncPrimitives.CallThenInvoke[T,TResult](AsyncActivation`1 ctxt, TResult result1, FSharpFunc`2 part2) in D:\a\_work\1\s\src\FSharp.Core\async.fs:line 508
   at Microsoft.FSharp.Control.Trampoline.Execute(FSharpFunc`2 firstAction) in D:\a\_work\1\s\src\FSharp.Core\async.fs:line 112

Further Examples:

Problem description

Please describe here the Fantomas problem you encountered. Check out our Contribution Guidelines.

Extra information

Options

Fantomas main branch at 2023-12-06T16:47:58Z - 0f8ee237a452f3772afe7989e031b5f65ba389f5

Default Fantomas configuration

Did you know that you can ignore files when formatting by using a .fantomasignore file? PS: It's unlikely that someone else will solve your specific issue, as it's something that you have a personal stake in.

nojaf commented 9 months ago

Hi Jimmy,

That main function leads to 5 define combinations: image

I believe the FABLE_COMPILER case, is leading to invalid code and cannot be parsed.

Try

[<EntryPoint>]
let main argv =
#if FABLE_COMPILER_PYTHON
    Pyxpecto.runTests allTests
#endif
#if FABLE_COMPILER_JAVASCRIPT
    Mocha.runTests allTests
#else
    Tests.runTestsWithCLIArgs [] Array.empty allTests
#endif

as a workaround.