@@ -3,9 +3,9 @@ module Exercism.TestRunner.FSharp.Rewrite
3
3
open System.IO
4
4
open Exercism.TestRunner .FSharp .Core
5
5
open Exercism.TestRunner .FSharp .Visitor
6
- open FSharp.Compiler .Syntax
7
- open FSharp.Compiler .Text
8
6
open Fantomas.Core
7
+ open Fantomas.FCS .Syntax
8
+ open Fantomas.FCS .Text
9
9
open Fantomas.FCS .Parse
10
10
11
11
type ParseResult =
@@ -18,13 +18,6 @@ type RewriteResult =
18
18
19
19
type EnableAllTests () =
20
20
inherit SyntaxVisitor()
21
-
22
- default this.VisitSynAttributeList ( attrs : SynAttributeList ) : SynAttributeList =
23
- base .VisitSynAttributeList(
24
- { attrs with
25
- Attributes =
26
- attrs.Attributes
27
- |> List.filter ( fun attr -> attr.TypeName.LongIdent.Head.idText <> " Ignore" ) })
28
21
29
22
override _.VisitSynAttribute ( attr : SynAttribute ) : SynAttribute =
30
23
let isSkipExpr expr =
@@ -33,32 +26,42 @@ type EnableAllTests() =
33
26
| _ -> false
34
27
35
28
match attr.ArgExpr with
36
- | SynExpr.Paren( expr, leftParenRange, rightParenRange, range) ->
29
+ | SynExpr.Paren( expr, leftParenRange, rightParenRange, range) ->
37
30
match expr with
38
- | SynExpr.App _ when isSkipExpr expr ->
39
- let newExpr = SynExpr.Const( SynConst.Unit, attr.ArgExpr.Range)
40
- base .VisitSynAttribute({ attr with ArgExpr = newExpr })
31
+ | SynExpr.App _ when isSkipExpr ( expr) ->
32
+ let noAttributesArgExpr = SynExpr.Const( SynConst.Unit, attr.ArgExpr.Range)
33
+ base .VisitSynAttribute({ attr with ArgExpr = noAttributesArgExpr })
41
34
| SynExpr.Tuple( iStruct, exprs, commaRanges, tplRange) ->
42
- let newExpr =
43
- SynExpr.Paren(
44
- SynExpr.Tuple( iStruct, exprs |> List.filter ( isSkipExpr >> not ), commaRanges, tplRange), leftParenRange, rightParenRange, range)
45
- base .VisitSynAttribute({ attr with ArgExpr = newExpr })
35
+ match List.tryFindIndex isSkipExpr exprs with
36
+ | Some index ->
37
+ let newExpr =
38
+ SynExpr.Paren(
39
+ SynExpr.Tuple(
40
+ iStruct,
41
+ exprs |> List.removeAt index,
42
+ commaRanges |> List.removeAt ( index - 1 ), tplRange
43
+ ),
44
+ leftParenRange,
45
+ rightParenRange,
46
+ range)
47
+ base .VisitSynAttribute({ attr with ArgExpr = newExpr })
48
+ | None -> base .VisitSynAttribute( attr)
46
49
| _ -> base .VisitSynAttribute( attr)
47
50
| _ -> base .VisitSynAttribute( attr)
48
51
49
52
let private parseFile ( filePath : string ) =
50
53
if File.Exists( filePath) then
51
54
let source = File.ReadAllText( filePath)
52
- let sourceText = source |> SourceText.ofString
53
- let tree , diagnostics = parseFile false sourceText []
54
- Some tree // TODO: use diagnostics to determine success
55
+ let sourceText = source |> SourceText.ofString
56
+ let tree , _ = CodeFormatter.ParseAsync ( false , source ) |> Async.RunSynchronously |> Array.head
57
+ Some tree
55
58
|> Option.map ( fun tree -> ParseSuccess( source, sourceText, tree))
56
59
|> Option.defaultValue ParseError
57
60
else
58
61
ParseError
59
62
60
- let private toCode code tree =
61
- CodeFormatter.FormatASTAsync( tree, code , FormatConfig.FormatConfig.Default )
63
+ let private toCode tree =
64
+ CodeFormatter.FormatASTAsync( tree)
62
65
|> Async.RunSynchronously
63
66
|> SourceText.ofString
64
67
@@ -67,12 +70,17 @@ let private enableAllTests parsedInput =
67
70
68
71
let private rewriteProjectFile ( context : TestRunContext ) =
69
72
let originalProjectFile = File.ReadAllText( context.ProjectFile)
70
- originalProjectFile, originalProjectFile.Replace( " net5.0" , " net6.0" ) .Replace( " net6.0" , " net7.0" )
73
+ let rewrittenProjectFile =
74
+ originalProjectFile
75
+ .Replace( " net5.0" , " net8.0" )
76
+ .Replace( " net6.0" , " net8.0" )
77
+ .Replace( " net7.0" , " net8.0" )
78
+ originalProjectFile, rewrittenProjectFile
71
79
72
80
let rewriteTests ( context : TestRunContext ) =
73
81
match parseFile context.TestsFile with
74
- | ParseSuccess ( originalSource, originalSourceText, originalTestTree) ->
75
- let rewrittenTestCode = originalTestTree |> enableAllTests |> toCode originalSource
76
- let ( originalProjectFile , rewrittenProjectFile ) = rewriteProjectFile context
82
+ | ParseSuccess ( originalSource, originalSourceText, originalTestTree) ->
83
+ let rewrittenTestCode = originalTestTree |> enableAllTests |> toCode
84
+ let originalProjectFile , rewrittenProjectFile = rewriteProjectFile context
77
85
RewriteSuccess( originalSourceText, originalTestTree, rewrittenTestCode, originalProjectFile, rewrittenProjectFile)
78
86
| ParseError -> RewriteError
0 commit comments