Skip to content

Commit

Permalink
Cli v5 (#2347)
Browse files Browse the repository at this point in the history
* Remove Fsi, Stdin and Stdout.

* Update the meaning of --force.

* Don't processFolder asynchronously.

* Mention invalid result when force is active.
Don't print exception stacktrace.

* Add test to cover --force functionality.

* Update documentation.

* Add changelog entry.
  • Loading branch information
nojaf authored Jul 8, 2022
1 parent 7e63a41 commit 2ca49d0
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 202 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
# Changelog

## [Unreleased]
## [5.0.0-alpha-011] - 2022-07-08

### Changed
* Restore the CodeFormatter.MakeRange public API. [#2306](https://github.com/fsprojects/fantomas/pull/2306)
* Update FCS to 'Add arrow to SynType.Fun trivia.', commit 5a5a5f6cd07aa4a8326baa07d4f7af1305ced6f4
* Update FCS to 'Fix setter first', commit 267d0a57f217df756d9ac33c6aa4ffbfe3b53097
* Update style of long if/match expressions (See [fslang-design#646](https://github.com/fsharp/fslang-design/issues/646)). [#2334](https://github.com/fsprojects/fantomas/pull/2334)
* `--force` will now write a file even when the result is invalid. [#2346](https://github.com/fsprojects/fantomas/issues/2346)

### Added
* Add setting `fsharp_max_if_then_short_width`. [#2299](https://github.com/fsprojects/fantomas/issues/2299)

### Fixed
* KeepIndentInBranch not respected inside a let and match. [1825](https://github.com/fsprojects/fantomas/issues/1825)

### Removed
* `--stdin`, `--stdout` and `--fsi` flags. [#2346](https://github.com/fsprojects/fantomas/issues/2346)

## [5.0.0-alpha-010] - 2022-06-27

### Changed
Expand Down
9 changes: 3 additions & 6 deletions docs-old/Documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ For the overview how to use the tool, you can type the command
dotnet fantomas --help

```
USAGE: dotnet fantomas [--help] [--recurse] [--force] [--profile] [--fsi <string>] [--stdin] [--stdout] [--out <string>] [--check] [--daemon] [--version] [<string>...]
USAGE: dotnet fantomas [--help] [--recurse] [--force] [--profile] [--out <string>] [--check] [--daemon] [--version] [<string>...]
INPUT:
Expand All @@ -27,12 +27,9 @@ INPUT:
OPTIONS:
--recurse, -r Process the input folder recursively.
--force Print the source unchanged if it cannot be parsed correctly.
--force Print the output even if it is not valid F# code. For debugging purposes only.
--profile Print performance profiling information.
--fsi <string> Read F# source from stdin as F# signatures.
--stdin Read F# source from standard input.
--stdout Write the formatted source code to standard output.
--out <string> Give a valid path for files/folders. Files should have .fs, .fsx, .fsi, .ml or .mli extension only.
--out <string> Give a valid path for files/folders. Files should have .fs, .fsx, .fsi, .ml or .mli extension only. Multiple files/folders are not supported.
--check Don't format files, just check if they have changed. Exits with 0 if it's formatted correctly, with 1 if some files need formatting and 99 if there was an internal error
--daemon Daemon mode, launches an LSP-like server to can be used by editor tooling.
--version, -v Displays the version of Fantomas
Expand Down
1 change: 1 addition & 0 deletions paket.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ nuget Ionide.KeepAChangelog.Tasks copy_local: true
nuget Dotnet.ReproducibleBuilds copy_local: true

github: fsprojects/fantomas:829faa6ba834f99afed9b4434b3a1680536474b2 src/Fantomas/CodePrinter.fs
github: dotnet/fsharp:267d0a57f217df756d9ac33c6aa4ffbfe3b53097 src/Compiler/Checking/CheckDeclarations.fs

# F# compiler source
github: dotnet/fsharp:267d0a57f217df756d9ac33c6aa4ffbfe3b53097 src/Compiler/FSComp.txt
Expand Down
1 change: 1 addition & 0 deletions paket.lock
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ GITHUB
src/Compiler/AbstractIL/ilpars.fsy (267d0a57f217df756d9ac33c6aa4ffbfe3b53097)
src/Compiler/AbstractIL/ilx.fs (267d0a57f217df756d9ac33c6aa4ffbfe3b53097)
src/Compiler/AbstractIL/ilx.fsi (267d0a57f217df756d9ac33c6aa4ffbfe3b53097)
src/Compiler/Checking/CheckDeclarations.fs (267d0a57f217df756d9ac33c6aa4ffbfe3b53097)
src/Compiler/Facilities/DiagnosticOptions.fs (267d0a57f217df756d9ac33c6aa4ffbfe3b53097)
src/Compiler/Facilities/DiagnosticOptions.fsi (267d0a57f217df756d9ac33c6aa4ffbfe3b53097)
src/Compiler/Facilities/DiagnosticsLogger.fs (267d0a57f217df756d9ac33c6aa4ffbfe3b53097)
Expand Down
1 change: 1 addition & 0 deletions src/Fantomas.Tests/Fantomas.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<Compile Include="Integration\MultiplePathsTests.fs" />
<Compile Include="Integration\WriteTests.fs" />
<Compile Include="Integration\DaemonTests.fs" />
<Compile Include="Integration\ForceTests.fs" />
</ItemGroup>
<Import Project="..\..\.paket\Paket.Restore.targets" />
</Project>
44 changes: 44 additions & 0 deletions src/Fantomas.Tests/Integration/ForceTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module Fantomas.Tests.Integration.ForceTests

open System.IO
open NUnit.Framework
open FsUnit
open Fantomas.Tests.TestHelpers

// The day this test fails because Fantomas can format the file, is the day you can remove this file.

[<Test>]
let ``code that was invalid should be still be written`` () =
let pwd = Path.GetDirectoryName(typeof<TemporaryFileCodeSample>.Assembly.Location)

let sourceFile =
Path.Combine(
pwd,
"..",
"..",
"..",
"..",
"..",
"paket-files",
"dotnet",
"fsharp",
"src",
"Compiler",
"Checking",
"CheckDeclarations.fs"
)

use outputFixture = new OutputFile()

let { ExitCode = exitCode; Output = output } =
runFantomasTool $"--force --out {outputFixture.Filename} {sourceFile}"

exitCode |> should equal 0

output
|> should contain "was not valid after formatting"

output |> should contain "has been written"

File.Exists outputFixture.Filename
|> should equal true
36 changes: 0 additions & 36 deletions src/Fantomas.Tests/Integration/MultiplePathsTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -36,42 +36,6 @@ let ``format multiple paths`` () =
fileContentMatches FormattedCode fileFixtureOne.Filename
fileContentMatches FormattedCode fileFixtureTwo.Filename

[<Test>]
let ``format multiple paths cannot be combined with --out`` () =
use config = new ConfigurationFile("[*]\nend_of_line = lf")

use fileFixtureOne = new TemporaryFileCodeSample(UserCode)

use fileFixtureTwo = new TemporaryFileCodeSample(UserCode)

let arguments =
sprintf "\"%s\" \"%s\" --out \"some Folder\"" fileFixtureOne.Filename fileFixtureTwo.Filename

let { ExitCode = exitCode; Error = error } = runFantomasTool arguments

exitCode |> should equal 1

error
|> should contain "--stdout and --out cannot be combined with multiple files."

[<Test>]
let ``format multiple paths cannot be combined with --stdout`` () =
use config = new ConfigurationFile("[*]\nend_of_line = lf")

use fileFixtureOne = new TemporaryFileCodeSample(UserCode)

use fileFixtureTwo = new TemporaryFileCodeSample(UserCode)

let arguments =
sprintf "\"%s\" \"%s\" --stdout" fileFixtureOne.Filename fileFixtureTwo.Filename

let { ExitCode = exitCode; Error = error } = runFantomasTool arguments

exitCode |> should equal 1

error
|> should contain "--stdout and --out cannot be combined with multiple files."

[<Test>]
let ``format multiple paths with recursive flag`` () =
use config = new ConfigurationFile("[*]\nend_of_line = lf")
Expand Down
42 changes: 6 additions & 36 deletions src/Fantomas/Format.fs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ exception CodeFormatException of (string * Option<Exception>) array with
type FormatResult =
| Formatted of filename: string * formattedContent: string
| Unchanged of filename: string
| InvalidCode of filename: string * formattedContent: string
| Error of filename: string * formattingError: Exception
| IgnoredFile of filename: string

Expand Down Expand Up @@ -64,10 +65,9 @@ let private formatContentInternalAsync
let! isValid = CodeFormatter.IsValidFSharpCodeAsync(isSignatureFile, formattedContent)

if not isValid then
raise
<| FormatException "Formatted content is not valid F# code"

return Formatted(filename = file, formattedContent = formattedContent)
return InvalidCode(filename = file, formattedContent = formattedContent)
else
return Formatted(filename = file, formattedContent = formattedContent)
else
return Unchanged(filename = file)
with ex ->
Expand All @@ -94,37 +94,6 @@ let private formatFileInternalAsync (compareWithoutLineEndings: bool) (file: str

let formatFileAsync = formatFileInternalAsync false

let formatFilesAsync files =
files |> Seq.map formatFileAsync |> Async.Parallel

let formatCode files =
async {
let! results = formatFilesAsync files

// Check for formatting errors:
let errors =
results
|> Array.choose (fun x ->
match x with
| Error (file, ex) -> Some(file, Some(ex))
| _ -> None)

if not <| Array.isEmpty errors then
raise <| CodeFormatException errors

// Overwrite source files with formatted content
let result =
results
|> Array.choose (fun x ->
match x with
| Formatted (source, formatted) ->
File.WriteAllText(source, formatted)
Some source
| _ -> None)

return result
}

type CheckResult =
{ Errors: (string * exn) list
Formatted: string list }
Expand Down Expand Up @@ -159,7 +128,8 @@ let checkCode (filenames: seq<string>) =
| FormatResult.Unchanged _
| FormatResult.IgnoredFile _ -> None
| FormatResult.Formatted (f, _)
| FormatResult.Error (f, _) -> Some f
| FormatResult.Error (f, _)
| FormatResult.InvalidCode (f, _) -> Some f

let changes =
formatted
Expand Down
Loading

0 comments on commit 2ca49d0

Please sign in to comment.