Skip to content

Commit 2ca49d0

Browse files
authored
Cli v5 (#2347)
* 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.
1 parent 7e63a41 commit 2ca49d0

File tree

9 files changed

+112
-202
lines changed

9 files changed

+112
-202
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
# Changelog
22

3-
## [Unreleased]
3+
## [5.0.0-alpha-011] - 2022-07-08
44

55
### Changed
66
* Restore the CodeFormatter.MakeRange public API. [#2306](https://github.com/fsprojects/fantomas/pull/2306)
77
* Update FCS to 'Add arrow to SynType.Fun trivia.', commit 5a5a5f6cd07aa4a8326baa07d4f7af1305ced6f4
88
* Update FCS to 'Fix setter first', commit 267d0a57f217df756d9ac33c6aa4ffbfe3b53097
99
* 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)
10+
* `--force` will now write a file even when the result is invalid. [#2346](https://github.com/fsprojects/fantomas/issues/2346)
1011

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

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

18+
### Removed
19+
* `--stdin`, `--stdout` and `--fsi` flags. [#2346](https://github.com/fsprojects/fantomas/issues/2346)
20+
1721
## [5.0.0-alpha-010] - 2022-06-27
1822

1923
### Changed

docs-old/Documentation.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ For the overview how to use the tool, you can type the command
1818
dotnet fantomas --help
1919

2020
```
21-
USAGE: dotnet fantomas [--help] [--recurse] [--force] [--profile] [--fsi <string>] [--stdin] [--stdout] [--out <string>] [--check] [--daemon] [--version] [<string>...]
21+
USAGE: dotnet fantomas [--help] [--recurse] [--force] [--profile] [--out <string>] [--check] [--daemon] [--version] [<string>...]
2222
2323
INPUT:
2424
@@ -27,12 +27,9 @@ INPUT:
2727
OPTIONS:
2828
2929
--recurse, -r Process the input folder recursively.
30-
--force Print the source unchanged if it cannot be parsed correctly.
30+
--force Print the output even if it is not valid F# code. For debugging purposes only.
3131
--profile Print performance profiling information.
32-
--fsi <string> Read F# source from stdin as F# signatures.
33-
--stdin Read F# source from standard input.
34-
--stdout Write the formatted source code to standard output.
35-
--out <string> Give a valid path for files/folders. Files should have .fs, .fsx, .fsi, .ml or .mli extension only.
32+
--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.
3633
--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
3734
--daemon Daemon mode, launches an LSP-like server to can be used by editor tooling.
3835
--version, -v Displays the version of Fantomas

paket.dependencies

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ nuget Ionide.KeepAChangelog.Tasks copy_local: true
1616
nuget Dotnet.ReproducibleBuilds copy_local: true
1717

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

2021
# F# compiler source
2122
github: dotnet/fsharp:267d0a57f217df756d9ac33c6aa4ffbfe3b53097 src/Compiler/FSComp.txt

paket.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ GITHUB
182182
src/Compiler/AbstractIL/ilpars.fsy (267d0a57f217df756d9ac33c6aa4ffbfe3b53097)
183183
src/Compiler/AbstractIL/ilx.fs (267d0a57f217df756d9ac33c6aa4ffbfe3b53097)
184184
src/Compiler/AbstractIL/ilx.fsi (267d0a57f217df756d9ac33c6aa4ffbfe3b53097)
185+
src/Compiler/Checking/CheckDeclarations.fs (267d0a57f217df756d9ac33c6aa4ffbfe3b53097)
185186
src/Compiler/Facilities/DiagnosticOptions.fs (267d0a57f217df756d9ac33c6aa4ffbfe3b53097)
186187
src/Compiler/Facilities/DiagnosticOptions.fsi (267d0a57f217df756d9ac33c6aa4ffbfe3b53097)
187188
src/Compiler/Facilities/DiagnosticsLogger.fs (267d0a57f217df756d9ac33c6aa4ffbfe3b53097)

src/Fantomas.Tests/Fantomas.Tests.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<Compile Include="Integration\MultiplePathsTests.fs" />
2828
<Compile Include="Integration\WriteTests.fs" />
2929
<Compile Include="Integration\DaemonTests.fs" />
30+
<Compile Include="Integration\ForceTests.fs" />
3031
</ItemGroup>
3132
<Import Project="..\..\.paket\Paket.Restore.targets" />
3233
</Project>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
module Fantomas.Tests.Integration.ForceTests
2+
3+
open System.IO
4+
open NUnit.Framework
5+
open FsUnit
6+
open Fantomas.Tests.TestHelpers
7+
8+
// The day this test fails because Fantomas can format the file, is the day you can remove this file.
9+
10+
[<Test>]
11+
let ``code that was invalid should be still be written`` () =
12+
let pwd = Path.GetDirectoryName(typeof<TemporaryFileCodeSample>.Assembly.Location)
13+
14+
let sourceFile =
15+
Path.Combine(
16+
pwd,
17+
"..",
18+
"..",
19+
"..",
20+
"..",
21+
"..",
22+
"paket-files",
23+
"dotnet",
24+
"fsharp",
25+
"src",
26+
"Compiler",
27+
"Checking",
28+
"CheckDeclarations.fs"
29+
)
30+
31+
use outputFixture = new OutputFile()
32+
33+
let { ExitCode = exitCode; Output = output } =
34+
runFantomasTool $"--force --out {outputFixture.Filename} {sourceFile}"
35+
36+
exitCode |> should equal 0
37+
38+
output
39+
|> should contain "was not valid after formatting"
40+
41+
output |> should contain "has been written"
42+
43+
File.Exists outputFixture.Filename
44+
|> should equal true

src/Fantomas.Tests/Integration/MultiplePathsTests.fs

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -36,42 +36,6 @@ let ``format multiple paths`` () =
3636
fileContentMatches FormattedCode fileFixtureOne.Filename
3737
fileContentMatches FormattedCode fileFixtureTwo.Filename
3838

39-
[<Test>]
40-
let ``format multiple paths cannot be combined with --out`` () =
41-
use config = new ConfigurationFile("[*]\nend_of_line = lf")
42-
43-
use fileFixtureOne = new TemporaryFileCodeSample(UserCode)
44-
45-
use fileFixtureTwo = new TemporaryFileCodeSample(UserCode)
46-
47-
let arguments =
48-
sprintf "\"%s\" \"%s\" --out \"some Folder\"" fileFixtureOne.Filename fileFixtureTwo.Filename
49-
50-
let { ExitCode = exitCode; Error = error } = runFantomasTool arguments
51-
52-
exitCode |> should equal 1
53-
54-
error
55-
|> should contain "--stdout and --out cannot be combined with multiple files."
56-
57-
[<Test>]
58-
let ``format multiple paths cannot be combined with --stdout`` () =
59-
use config = new ConfigurationFile("[*]\nend_of_line = lf")
60-
61-
use fileFixtureOne = new TemporaryFileCodeSample(UserCode)
62-
63-
use fileFixtureTwo = new TemporaryFileCodeSample(UserCode)
64-
65-
let arguments =
66-
sprintf "\"%s\" \"%s\" --stdout" fileFixtureOne.Filename fileFixtureTwo.Filename
67-
68-
let { ExitCode = exitCode; Error = error } = runFantomasTool arguments
69-
70-
exitCode |> should equal 1
71-
72-
error
73-
|> should contain "--stdout and --out cannot be combined with multiple files."
74-
7539
[<Test>]
7640
let ``format multiple paths with recursive flag`` () =
7741
use config = new ConfigurationFile("[*]\nend_of_line = lf")

src/Fantomas/Format.fs

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ exception CodeFormatException of (string * Option<Exception>) array with
3232
type FormatResult =
3333
| Formatted of filename: string * formattedContent: string
3434
| Unchanged of filename: string
35+
| InvalidCode of filename: string * formattedContent: string
3536
| Error of filename: string * formattingError: Exception
3637
| IgnoredFile of filename: string
3738

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

6667
if not isValid then
67-
raise
68-
<| FormatException "Formatted content is not valid F# code"
69-
70-
return Formatted(filename = file, formattedContent = formattedContent)
68+
return InvalidCode(filename = file, formattedContent = formattedContent)
69+
else
70+
return Formatted(filename = file, formattedContent = formattedContent)
7171
else
7272
return Unchanged(filename = file)
7373
with ex ->
@@ -94,37 +94,6 @@ let private formatFileInternalAsync (compareWithoutLineEndings: bool) (file: str
9494

9595
let formatFileAsync = formatFileInternalAsync false
9696

97-
let formatFilesAsync files =
98-
files |> Seq.map formatFileAsync |> Async.Parallel
99-
100-
let formatCode files =
101-
async {
102-
let! results = formatFilesAsync files
103-
104-
// Check for formatting errors:
105-
let errors =
106-
results
107-
|> Array.choose (fun x ->
108-
match x with
109-
| Error (file, ex) -> Some(file, Some(ex))
110-
| _ -> None)
111-
112-
if not <| Array.isEmpty errors then
113-
raise <| CodeFormatException errors
114-
115-
// Overwrite source files with formatted content
116-
let result =
117-
results
118-
|> Array.choose (fun x ->
119-
match x with
120-
| Formatted (source, formatted) ->
121-
File.WriteAllText(source, formatted)
122-
Some source
123-
| _ -> None)
124-
125-
return result
126-
}
127-
12897
type CheckResult =
12998
{ Errors: (string * exn) list
13099
Formatted: string list }
@@ -159,7 +128,8 @@ let checkCode (filenames: seq<string>) =
159128
| FormatResult.Unchanged _
160129
| FormatResult.IgnoredFile _ -> None
161130
| FormatResult.Formatted (f, _)
162-
| FormatResult.Error (f, _) -> Some f
131+
| FormatResult.Error (f, _)
132+
| FormatResult.InvalidCode (f, _) -> Some f
163133

164134
let changes =
165135
formatted

0 commit comments

Comments
 (0)