Skip to content

Commit

Permalink
convert Error to System.Exception
Browse files Browse the repository at this point in the history
  • Loading branch information
0x53A committed Oct 1, 2019
1 parent 3cd4bfb commit 154f540
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/bridge.fs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ module internal Bridge =
|> fixNodeArray
|> fixReadonlyArray
|> fixDateTime
|> mapErrorToException
|> fixStatic
|> createIExports
|> fixOverloadingOnStringParameters // fixEscapeWords must be after
Expand Down
28 changes: 28 additions & 0 deletions src/transform.fs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,15 @@ let createIExports (f: FsFile): FsFile =
)
}

let flattenInherits (i:FsInterface) : FsType seq = seq {
for b in i.Inherits do
yield b
match b with
| FsType.Interface i2 ->
yield! flattenInherits i2
| _ -> ()
}

let fixTic (typeParameters: FsType list) (tp: FsType) =
if typeParameters.Length = 0 then
tp
Expand Down Expand Up @@ -525,6 +534,25 @@ let fixDateTime(f: FsFile): FsFile =
| _ -> tp
)

/// https://github.com/fable-compiler/ts2fable/issues/298#issuecomment-478328229
let mapErrorToException (f: FsFile): FsFile =
let simpleExceptionType = simpleType "System.Exception"
f |> fixFile (fun tp ->
match tp with

// if the type IS error, map it to System.Exception
| FsType.Mapped m when (m.Name = "Error") ->
simpleExceptionType

// if the type INHERITS from error, replace it with System.Exception.
// This loses information, but at least compiles.
// see https://github.com/fable-compiler/ts2fable/issues/298#issuecomment-504104618
| FsType.Interface i when (flattenInherits i |> Seq.contains simpleExceptionType) ->
FsType.Alias { Name = i.Name; Type = simpleExceptionType; TypeParameters = [] }

| _ -> tp
)

let fixEnumReferences (f: FsFile): FsFile =
// get a list of enum names
let list = List<string>()
Expand Down
13 changes: 13 additions & 0 deletions test/fragments/regressions/#xxx-error-to-exception.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

export module ErrorExceptionTest {
// should be System.Exception
var instanceErrorProperty: Error;

// should be replaced with an alias to System.Exception, "i" will not be mapped.
interface InheritFromError extends Error {
i : number
}

var instanceInheritFromErrorProperty: InheritFromError;
}

16 changes: 16 additions & 0 deletions test/fragments/regressions/#xxx-error-to-exception.expected.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// ts2fable 0.0.0
module rec #xxx-error-to-exception
open System
open Fable.Core
open Fable.Import.JS

let [<Import("ErrorExceptionTest","test")>] errorExceptionTest: ErrorExceptionTest.IExports = jsNative

module ErrorExceptionTest =

type [<AllowNullLiteral>] IExports =
abstract instanceErrorProperty: System.Exception
abstract instanceInheritFromErrorProperty: InheritFromError

type InheritFromError =
System.Exception
4 changes: 4 additions & 0 deletions test/fsFileTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,7 @@ describe "transform tests" <| fun _ ->
// https://github.com/fable-compiler/ts2fable/issues/292
it "regression #292 static props" <| fun _ ->
runRegressionTest "#292-static-props"

// https://github.com/fable-compiler/ts2fable/issues/xxx
it "regression #xxx error to exception" <| fun _ ->
runRegressionTest "#xxx-error-to-exception"

0 comments on commit 154f540

Please sign in to comment.