Skip to content

Commit

Permalink
Don't fail if compiler warns about reserved words (fsprojects#3001)
Browse files Browse the repository at this point in the history
* Don't fail if compiler warns about reserved words

* Use error codes instead of plain strings

* Update changelog entry

* Update CHANGELOG.md
  • Loading branch information
josh-degraw committed Nov 30, 2023
1 parent 10ea3a3 commit cee9a9e
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 46 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

### Fixed
* Process is reserved keyword. [#2996](https://github.com/fsprojects/fantomas/issues/2996)
* Always yield list items on separate lines if a conditional is present. [#2972](https://github.com/fsprojects/fantomas/issues/2972)

## 6.3.0-alpha-003 - 2023-11-15
Expand Down
50 changes: 19 additions & 31 deletions src/Fantomas.Core.Tests/FunctionDefinitionTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -111,43 +111,31 @@ let ``should keep identifiers with + in double backticks`` () =
"""

[<Test>]
let ``double backticks with non-alphanum character, 776`` () =
[<TestCase "let ``!foo hoo`` () = ()">]
[<TestCase "let ``@foo hoo`` () = ()">]
[<TestCase "let ``$foo hoo`` () = ()">]
[<TestCase "let ``%foo hoo`` () = ()">]
[<TestCase "let ``^foo hoo`` () = ()">]
[<TestCase "let ``&foo hoo`` () = ()">]
[<TestCase "let ``*foo hoo`` () = ()">]
[<TestCase "let ``<foo hoo`` () = ()">]
[<TestCase "let ``>foo hoo`` () = ()">]
[<TestCase "let ``=foo hoo`` () = ()">]
[<TestCase "let ``-foo hoo`` () = ()">]
[<TestCase "let ``!foo hoo`` () : unit = ()">]
[<TestCase "let ``@foo hoo`` = ()">]
[<TestCase "let ``$foo hoo``: unit = ()">]
let ``double backticks with non-alphanum character, 776`` (input) =
formatSourceString
"""
let ``!foo hoo`` () = ()
let ``@foo hoo`` () = ()
let ``$foo hoo`` () = ()
let ``%foo hoo`` () = ()
let ``^foo hoo`` () = ()
let ``&foo hoo`` () = ()
let ``*foo hoo`` () = ()
let ``<foo hoo`` () = ()
let ``>foo hoo`` () = ()
let ``=foo hoo`` () = ()
let ``-foo hoo`` () = ()
let ``!foo hoo`` () : unit = ()
let ``@foo hoo`` = ()
let ``$foo hoo`` : unit = ()
$"""
%s{input}
"""
config
|> prepend newline
|> should
equal
"""
let ``!foo hoo`` () = ()
let ``@foo hoo`` () = ()
let ``$foo hoo`` () = ()
let ``%foo hoo`` () = ()
let ``^foo hoo`` () = ()
let ``&foo hoo`` () = ()
let ``*foo hoo`` () = ()
let ``<foo hoo`` () = ()
let ``>foo hoo`` () = ()
let ``=foo hoo`` () = ()
let ``-foo hoo`` () = ()
let ``!foo hoo`` () : unit = ()
let ``@foo hoo`` = ()
let ``$foo hoo``: unit = ()
$"""
%s{input}
"""

[<Test>]
Expand Down
14 changes: 14 additions & 0 deletions src/Fantomas.Core.Tests/IdentTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,17 @@ let ``process`` = 1
"""
let ``process`` = 1
"""

[<Test>]
let ``allows processing when encountering reserved words`` () =
formatSourceString
"""
let process: IProcess = importAll "process"
"""
config
|> prepend newline
|> should
equal
"""
let process: IProcess = importAll "process"
"""
1 change: 1 addition & 0 deletions src/Fantomas.Core/Utils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ open Microsoft.FSharp.Core.CompilerServices

[<RequireQualifiedAccess>]
module String =

let startsWithOrdinal (prefix: string) (str: string) =
str.StartsWith(prefix, StringComparison.Ordinal)

Expand Down
34 changes: 19 additions & 15 deletions src/Fantomas.Core/Validation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,28 @@ open Fantomas.FCS.Text
open Fantomas.FCS.Syntax
open Fantomas.FCS.Parse

let private safeToIgnoreWarnings =
// See https://github.com/dotnet/fsharp/blob/2a25184293e39a635217670652b00680de04472a/src/Compiler/Driver/CompilerDiagnostics.fs#L214
// and https://github.com/dotnet/fsharp/blob/b7e747921515ae7939c7cb6885513eb80ec7ca2f/src/Compiler/FSComp.txt
// for error codes
let safeToIgnoreWarnings =
set
[ "This construct is deprecated: it is only for use in the F# library"
"Identifiers containing '@' are reserved for use in F# code generation" ]

// Exception of type 'Fantomas.FCS.DiagnosticsLogger+LibraryUseOnly' was thrown.
[ 35 // Deprecated
42 // LibraryUseOnly
46 // ReservedKeyword
1104 ] // lexhlpIdentifiersContainingAtSymbolReserved

let noWarningOrErrorDiagnostics diagnostics =
let errors =
diagnostics
|> List.filter (fun e ->
match e.Severity with
| FSharpDiagnosticSeverity.Error -> true
| FSharpDiagnosticSeverity.Hidden
| FSharpDiagnosticSeverity.Info -> false
| FSharpDiagnosticSeverity.Warning -> not (safeToIgnoreWarnings.Contains(e.Message)))

List.isEmpty errors
diagnostics
|> List.filter (fun e ->
match e.Severity with
| FSharpDiagnosticSeverity.Error -> true
| FSharpDiagnosticSeverity.Hidden
| FSharpDiagnosticSeverity.Info -> false
| FSharpDiagnosticSeverity.Warning ->
match e.ErrorNumber with
| None -> true
| Some errorNumber -> not (safeToIgnoreWarnings.Contains(errorNumber)))
|> List.isEmpty

/// Check whether an input string is invalid in F# by looking for errors and warnings in the diagnostics.
let isValidFSharpCode (isSignature: bool) (source: string) : Async<bool> =
Expand Down
3 changes: 3 additions & 0 deletions src/Fantomas.FCS/Parse.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1049,12 +1049,15 @@ let parseFile
let diagnostics =
List.map
(fun (p, severity) ->
// See https://github.com/dotnet/fsharp/blob/2a25184293e39a635217670652b00680de04472a/src/Compiler/Driver/CompilerDiagnostics.fs#L214
// for the error codes
let range, message, errorNumber =
match p.Exception with
| :? IndentationProblem as ip -> Some ip.Data1, ip.Data0, Some 58
| :? SyntaxError as se -> Some se.range, (getSyntaxErrorMessage se.Data0), Some 10
| :? LibraryUseOnly as luo -> Some luo.range, LibraryUseOnlyE().Format, Some 42
| :? DiagnosticWithText as dwt -> Some dwt.range, dwt.message, Some dwt.number
| :? ReservedKeyword as rkw -> Some rkw.Data1, rkw.Data0, Some 46
| _ -> None, p.Exception.Message, None

{ Severity = severity
Expand Down

0 comments on commit cee9a9e

Please sign in to comment.