@@ -13,6 +13,8 @@ type Reporter() =
13
13
14
14
member val Errors = errors
15
15
16
+ member val HasRegEpx = false with get, set
17
+
16
18
// Not really proud of this implementation, but I was not able to make it in a
17
19
// pure functional way, using a Tree structure or something similar
18
20
// It seems like for now this implementation does the job which is the most important
@@ -43,6 +45,15 @@ type TransformContext
43
45
// You should not use this directly, but instead use the AddWarning and AddError methods
44
46
member val _Reporter = reporter
45
47
48
+ member _.ExposeRegExp () =
49
+ // TODO: Rework how we memorize if need to expose RegExp alias
50
+ // Perhaps, before the printer phase we should traverse the whole AST to find information
51
+ // like aliases that we need to expose
52
+ // We could propagate the IsStandardLibrary flag to the F# AST to check such information
53
+ // Example: If we find an F# TypeReference with the name "RegExp" and the IsStandardLibrary flag is true
54
+ // then we need to expose the RegExp alias
55
+ reporter.HasRegEpx <- true
56
+
46
57
member _.ExposeType ( typ : FSharpType ) =
47
58
match parent with
48
59
| None -> types.Add( typ)
@@ -99,6 +110,26 @@ type TransformContext
99
110
100
111
member _.AddError ( error : string ) = reporter.Errors.Add error
101
112
113
+ let private mapTypeNameToFableCoreAwareName
114
+ ( context : TransformContext )
115
+ ( typeReference : GlueTypeReference )
116
+ =
117
+
118
+ if typeReference.IsStandardLibrary then
119
+ match typeReference.Name with
120
+ | " Date" -> " JS.Date"
121
+ | " Promise" -> " JS.Promise"
122
+ | " Uint8Array" -> " JS.Uint8Array"
123
+ | " ReadonlyArray" -> " ResizeArray"
124
+ | " Array" -> " ResizeArray"
125
+ | " Boolean" -> " bool"
126
+ | " RegExp" ->
127
+ context.ExposeRegExp()
128
+ " RegExp"
129
+ | name -> name
130
+ else
131
+ typeReference.Name
132
+
102
133
let private unwrapOptionIfAlreadyOptional
103
134
( context : TransformContext )
104
135
( typ : GlueType )
@@ -373,11 +404,15 @@ let rec private transformType
373
404
374
405
| GlueType.TypeReference typeReference ->
375
406
({
376
- Name = Naming. mapTypeNameToFableCoreAwareName typeReference.Name
407
+ Name = mapTypeNameToFableCoreAwareName context typeReference
377
408
FullName = typeReference.FullName
378
409
TypeArguments =
379
410
typeReference.TypeArguments |> List.map ( transformType context)
380
411
Type = //transformType context typeReference.TypeRef
412
+ // TODO: This code looks suspicious
413
+ // Why would a typeReference always be a string? I think I added that here to make
414
+ // the compiler happy because we don't have a concrete type for the TypeReference
415
+ // this is because of the recursive types which creates infinite loops in the reader
381
416
FSharpType.Primitive FSharpPrimitive.String
382
417
}
383
418
: FSharpTypeReference)
@@ -1827,7 +1862,8 @@ let private transformTypeAliasDeclaration
1827
1862
|> FSharpType.TypeAlias
1828
1863
1829
1864
| GlueType.TypeReference typeReference ->
1830
- let context = context.PushScope typeReference.Name
1865
+ let mappedName = mapTypeNameToFableCoreAwareName context typeReference
1866
+ let context = context.PushScope mappedName
1831
1867
1832
1868
let handleDefaultCase () =
1833
1869
({
@@ -1884,7 +1920,7 @@ let private transformTypeAliasDeclaration
1884
1920
Name = typeAliasName
1885
1921
Type =
1886
1922
{
1887
- Name = typeReference.Name
1923
+ Name = mappedName
1888
1924
FullName = typeReference.FullName
1889
1925
TypeArguments =
1890
1926
[ FSharpType.Interface typeArgument ]
@@ -2197,11 +2233,20 @@ let private transform
2197
2233
yield ! rootTransformContext.ToList()
2198
2234
]
2199
2235
2236
+ type TransformResult =
2237
+ {
2238
+ FSharpAST: FSharpType list
2239
+ Warnings: ResizeArray < string >
2240
+ Errors: ResizeArray < string >
2241
+ IncludeRegExpAlias: bool
2242
+ }
2243
+
2200
2244
let apply ( glueAst : GlueType list ) =
2201
2245
let reporter = Reporter()
2202
2246
2203
- {|
2247
+ {
2204
2248
FSharpAST = transform reporter true glueAst
2205
2249
Warnings = reporter.Warnings
2206
2250
Errors = reporter.Errors
2207
- |}
2251
+ IncludeRegExpAlias = reporter.HasRegEpx
2252
+ }
0 commit comments