Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Error to Exception and other fixes #311

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion paket.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ group netcorebuild

nuget Fake.DotNet.Cli
nuget Fake.IO.FileSystem
nuget Fake.Core.Environment
nuget Fake.Core.Environment 5.2
nuget Fake.Core.BuildServer
nuget Fake.Api.GitHub
nuget Fake.Tools.Git
nuget Fake.Windows.Chocolatey
nuget Fake.Core.Target
nuget Fake.JavaScript.Yarn
nuget FSharp.Core 4.5

group WebApp
source https://api.nuget.org/v3/index.json
Expand All @@ -39,3 +40,10 @@ group WebApp
github fable-compiler/ts2fable-exports:dev Monaco.fs

clitool dotnet-fable

group TestCompile
source https://api.nuget.org/v3/index.json
storage:none

nuget Fable.Core
nuget Fable.Browser.Dom
24 changes: 24 additions & 0 deletions paket.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,30 @@ NUGET
System.Xml.XmlDocument (>= 4.3)
System.Xml.XPath (>= 4.3)

GROUP TestCompile
STORAGE: NONE
NUGET
remote: https://api.nuget.org/v3/index.json
Fable.Browser.Blob (1.1) - restriction: >= netstandard2.0
Fable.Core (>= 3.0) - restriction: >= netstandard2.0
FSharp.Core (>= 4.6.2) - restriction: >= netstandard2.0
Fable.Browser.Dom (1.1)
Fable.Browser.Blob (>= 1.1) - restriction: >= netstandard2.0
Fable.Browser.Event (>= 1.0) - restriction: >= netstandard2.0
Fable.Browser.WebStorage (>= 1.0) - restriction: >= netstandard2.0
Fable.Core (>= 3.0) - restriction: >= netstandard2.0
FSharp.Core (>= 4.6.2) - restriction: >= netstandard2.0
Fable.Browser.Event (1.0) - restriction: >= netstandard2.0
Fable.Core (>= 3.0) - restriction: >= netstandard2.0
FSharp.Core (>= 4.5.2) - restriction: >= netstandard2.0
Fable.Browser.WebStorage (1.0) - restriction: >= netstandard2.0
Fable.Browser.Event (>= 1.0) - restriction: >= netstandard2.0
Fable.Core (>= 3.0) - restriction: >= netstandard2.0
FSharp.Core (>= 4.5.2) - restriction: >= netstandard2.0
Fable.Core (3.1.1)
FSharp.Core (>= 4.5.2) - restriction: >= netstandard2.0
FSharp.Core (4.7) - restriction: >= netstandard2.0

GROUP WebApp
STORAGE: NONE
NUGET
Expand Down
8 changes: 4 additions & 4 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 Expand Up @@ -178,14 +179,13 @@ module internal Bridge =
)

{
// use the F# file name as the module namespace
// TODO ensure valid name
Namespace = nameSpace
// use the escaped F# file name as the module namespace
Namespace = Naming.escapeWord nameSpace
Opens =
[
"System"
"Fable.Core"
"Fable.Import.JS"
"Fable.Core.JS"
]
Files = fsFiles
}
Expand Down
37 changes: 30 additions & 7 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 @@ -485,12 +494,7 @@ let fixReadonlyArray(f: FsFile): FsFile =
| _ -> tp
| _ -> tp

// only replace in functions
f |> fixFile (fun tp ->
match tp with
| FsType.Function _ -> fixType fix tp
| _ -> tp
)
f |> fixFile (fun tp -> fixType fix tp)

let fixEscapeWords(f: FsFile): FsFile =
f |> fixFile (fun tp ->
Expand Down Expand Up @@ -525,6 +529,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 Expand Up @@ -1253,7 +1276,7 @@ let fixFsFileOut fo =

if isBrowser then
{ fo with
Opens = fo.Opens @ ["Fable.Import.Browser"]
Opens = fo.Opens @ ["Browser.Types"]
Files = fo.Files |> List.map fixHelperLines }
else fo

Expand Down
5 changes: 3 additions & 2 deletions test-compile/paket.references
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Fable.Core
Fable.Import.Browser
group TestCompile
Fable.Core
Fable.Browser.Dom
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module rec SceneLoader.ImportMeshAsync
open System
open Fable.Core
open Fable.Import.JS
open Fable.Core.JS

let [<Import("*","test")>] babylon: BABYLON.IExports = jsNative

Expand Down
2 changes: 1 addition & 1 deletion test/fragments/babylonjs/Stage.PrivateCtor.expected.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module rec Stage.PrivateCtor
open System
open Fable.Core
open Fable.Import.JS
open Fable.Core.JS

let [<Import("*","test")>] babylon: BABYLON.IExports = jsNative

Expand Down
2 changes: 1 addition & 1 deletion test/fragments/breezejs/globalClass.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module rec globalClass
open System
open Fable.Core
open Fable.Import.JS
open Fable.Core.JS


type [<AllowNullLiteral>] IExports =
Expand Down
2 changes: 1 addition & 1 deletion test/fragments/breezejs/globalVariable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module rec globalVariable
open System
open Fable.Core
open Fable.Import.JS
open Fable.Core.JS

let [<Import("FilterQueryOp","test")>] FilterQueryOp: FilterQueryOp = jsNative

Expand Down
2 changes: 1 addition & 1 deletion test/fragments/monaco/variableInModule.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module rec variableInModule
open System
open Fable.Core
open Fable.Import.JS
open Fable.Core.JS

let [<Import("*","test")>] monaco: Monaco.IExports = jsNative

Expand Down
2 changes: 1 addition & 1 deletion test/fragments/node/typeImport.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module rec typeImport
open System
open Fable.Core
open Fable.Import.JS
open Fable.Core.JS


module Url =
Expand Down
2 changes: 1 addition & 1 deletion test/fragments/react-native-fbsdk/stringEnumWithPeriod.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module rec stringEnumWithPeriod
open System
open Fable.Core
open Fable.Import.JS
open Fable.Core.JS


type [<StringEnum>] [<RequireQualifiedAccess>] Permissions =
Expand Down
2 changes: 1 addition & 1 deletion test/fragments/react/f1.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module rec f1
open System
open Fable.Core
open Fable.Import.JS
open Fable.Core.JS


module React =
Expand Down
2 changes: 1 addition & 1 deletion test/fragments/react/f2.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module rec f2
open System
open Fable.Core
open Fable.Import.JS
open Fable.Core.JS

type ClassAttributes = React.ClassAttributes
type ReactNode = React.ReactNode
Expand Down
2 changes: 1 addition & 1 deletion test/fragments/react/f3.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module rec f3
open System
open Fable.Core
open Fable.Import.JS
open Fable.Core.JS

type ComponentLifecycle = React.ComponentLifecycle
type ComponentClass = React.ComponentClass
Expand Down
2 changes: 1 addition & 1 deletion test/fragments/react/f4.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module rec f4
open System
open Fable.Core
open Fable.Import.JS
open Fable.Core.JS

type DetailedHTMLFactory = React.DetailedHTMLFactory
type DialogHTMLAttributes = React.DialogHTMLAttributes
Expand Down
2 changes: 1 addition & 1 deletion test/fragments/react/f5.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module rec f5
open System
open Fable.Core
open Fable.Import.JS
open Fable.Core.JS

let [<Import("*","test")>] react: React.IExports = jsNative

Expand Down
2 changes: 1 addition & 1 deletion test/fragments/react/f6.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module rec f6
open System
open Fable.Core
open Fable.Import.JS
open Fable.Core.JS

type Component = React.Component
type ComponentState = React.ComponentState
Expand Down
2 changes: 1 addition & 1 deletion test/fragments/react/f7.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module rec f7
open System
open Fable.Core
open Fable.Import.JS
open Fable.Core.JS

type SyntheticEvent = React.SyntheticEvent

Expand Down
2 changes: 1 addition & 1 deletion test/fragments/react/f8.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module rec f8
open System
open Fable.Core
open Fable.Import.JS
open Fable.Core.JS

type Validator = React.Validator

Expand Down
2 changes: 1 addition & 1 deletion test/fragments/reactxp/duplicatedVariableExports.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module rec duplicatedVariableExports
open System
open Fable.Core
open Fable.Import.JS
open Fable.Core.JS

let [<Import("*","reactxp")>] reactXP: ReactXP.IExports = jsNative

Expand Down
2 changes: 1 addition & 1 deletion test/fragments/reactxp/multiple/ReactXP.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module rec ReactXP
open System
open Fable.Core
open Fable.Import.JS
open Fable.Core.JS

module ReactXP = __web_ReactXP

Expand Down
4 changes: 2 additions & 2 deletions test/fragments/regressions/#275-private-members.expected.fs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// ts2fable 0.0.0
module rec #275-private-members
module rec ``#275-private-members``
open System
open Fable.Core
open Fable.Import.JS
open Fable.Core.JS

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

Expand Down
4 changes: 2 additions & 2 deletions test/fragments/regressions/#277-unwrap-options.expected.fs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// ts2fable 0.0.0
module rec #277-unwrap-options
module rec ``#277-unwrap-options``
open System
open Fable.Core
open Fable.Import.JS
open Fable.Core.JS

let [<Import("UnwrapOptions","test")>] unwrapOptions: UnwrapOptions.IExports = jsNative
let [<Import("UnwrapOptionsAlias","test")>] unwrapOptionsAlias: UnwrapOptionsAlias.IExports = jsNative
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// ts2fable 0.0.0
module rec #278-typeliterals-return
module rec ``#278-typeliterals-return``
open System
open Fable.Core
open Fable.Import.JS
open Fable.Core.JS

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// ts2fable 0.0.0
module rec #288-type-alias-float-number
module rec ``#288-type-alias-float-number``
open System
open Fable.Core
open Fable.Import.JS
open Fable.Core.JS

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// ts2fable 0.0.0
module rec #289-recursive-merge-modules
module rec ``#289-recursive-merge-modules``
open System
open Fable.Core
open Fable.Import.JS
open Fable.Core.JS


module Outer =
Expand Down
4 changes: 2 additions & 2 deletions test/fragments/regressions/#292-static-props.expected.fs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// ts2fable 0.0.0
module rec #292-static-props
module rec ``#292-static-props``
open System
open Fable.Core
open Fable.Import.JS
open Fable.Core.JS

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

Expand Down
13 changes: 13 additions & 0 deletions test/fragments/regressions/#311-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;
}

Loading