diff --git a/paket.dependencies b/paket.dependencies index c2138cc4..6f9c0b61 100644 --- a/paket.dependencies +++ b/paket.dependencies @@ -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 @@ -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 \ No newline at end of file diff --git a/paket.lock b/paket.lock index 3bea9f6b..082cd72b 100644 --- a/paket.lock +++ b/paket.lock @@ -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 diff --git a/src/bridge.fs b/src/bridge.fs index 7379f058..42571e47 100644 --- a/src/bridge.fs +++ b/src/bridge.fs @@ -132,6 +132,7 @@ module internal Bridge = |> fixNodeArray |> fixReadonlyArray |> fixDateTime + |> mapErrorToException |> fixStatic |> createIExports |> fixOverloadingOnStringParameters // fixEscapeWords must be after @@ -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 } diff --git a/src/transform.fs b/src/transform.fs index 967f55ac..d4e85556 100644 --- a/src/transform.fs +++ b/src/transform.fs @@ -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 @@ -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 -> @@ -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() @@ -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 diff --git a/test-compile/paket.references b/test-compile/paket.references index 6b9c845a..888931d8 100644 --- a/test-compile/paket.references +++ b/test-compile/paket.references @@ -1,2 +1,3 @@ -Fable.Core -Fable.Import.Browser \ No newline at end of file +group TestCompile + Fable.Core + Fable.Browser.Dom \ No newline at end of file diff --git a/test/fragments/babylonjs/SceneLoader.ImportMeshAsync.expected.fs b/test/fragments/babylonjs/SceneLoader.ImportMeshAsync.expected.fs index 5f256da5..1446871f 100644 --- a/test/fragments/babylonjs/SceneLoader.ImportMeshAsync.expected.fs +++ b/test/fragments/babylonjs/SceneLoader.ImportMeshAsync.expected.fs @@ -2,7 +2,7 @@ module rec SceneLoader.ImportMeshAsync open System open Fable.Core -open Fable.Import.JS +open Fable.Core.JS let [] babylon: BABYLON.IExports = jsNative diff --git a/test/fragments/babylonjs/Stage.PrivateCtor.expected.fs b/test/fragments/babylonjs/Stage.PrivateCtor.expected.fs index dd59edcf..08e97bc3 100644 --- a/test/fragments/babylonjs/Stage.PrivateCtor.expected.fs +++ b/test/fragments/babylonjs/Stage.PrivateCtor.expected.fs @@ -2,7 +2,7 @@ module rec Stage.PrivateCtor open System open Fable.Core -open Fable.Import.JS +open Fable.Core.JS let [] babylon: BABYLON.IExports = jsNative diff --git a/test/fragments/breezejs/globalClass.fs b/test/fragments/breezejs/globalClass.fs index b7787a33..dadc89cb 100644 --- a/test/fragments/breezejs/globalClass.fs +++ b/test/fragments/breezejs/globalClass.fs @@ -2,7 +2,7 @@ module rec globalClass open System open Fable.Core -open Fable.Import.JS +open Fable.Core.JS type [] IExports = diff --git a/test/fragments/breezejs/globalVariable.fs b/test/fragments/breezejs/globalVariable.fs index c3719707..dd2c2bac 100644 --- a/test/fragments/breezejs/globalVariable.fs +++ b/test/fragments/breezejs/globalVariable.fs @@ -2,7 +2,7 @@ module rec globalVariable open System open Fable.Core -open Fable.Import.JS +open Fable.Core.JS let [] FilterQueryOp: FilterQueryOp = jsNative diff --git a/test/fragments/monaco/variableInModule.fs b/test/fragments/monaco/variableInModule.fs index e01af063..0e8c7421 100644 --- a/test/fragments/monaco/variableInModule.fs +++ b/test/fragments/monaco/variableInModule.fs @@ -2,7 +2,7 @@ module rec variableInModule open System open Fable.Core -open Fable.Import.JS +open Fable.Core.JS let [] monaco: Monaco.IExports = jsNative diff --git a/test/fragments/node/typeImport.fs b/test/fragments/node/typeImport.fs index 77e41e37..b4bf38b6 100644 --- a/test/fragments/node/typeImport.fs +++ b/test/fragments/node/typeImport.fs @@ -2,7 +2,7 @@ module rec typeImport open System open Fable.Core -open Fable.Import.JS +open Fable.Core.JS module Url = diff --git a/test/fragments/react-native-fbsdk/stringEnumWithPeriod.fs b/test/fragments/react-native-fbsdk/stringEnumWithPeriod.fs index f325fbf0..41db5c3a 100644 --- a/test/fragments/react-native-fbsdk/stringEnumWithPeriod.fs +++ b/test/fragments/react-native-fbsdk/stringEnumWithPeriod.fs @@ -2,7 +2,7 @@ module rec stringEnumWithPeriod open System open Fable.Core -open Fable.Import.JS +open Fable.Core.JS type [] [] Permissions = diff --git a/test/fragments/react/f1.fs b/test/fragments/react/f1.fs index f3118ecb..42d98ce1 100644 --- a/test/fragments/react/f1.fs +++ b/test/fragments/react/f1.fs @@ -2,7 +2,7 @@ module rec f1 open System open Fable.Core -open Fable.Import.JS +open Fable.Core.JS module React = diff --git a/test/fragments/react/f2.fs b/test/fragments/react/f2.fs index 3ec8f559..ad65cde9 100644 --- a/test/fragments/react/f2.fs +++ b/test/fragments/react/f2.fs @@ -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 diff --git a/test/fragments/react/f3.fs b/test/fragments/react/f3.fs index 9a2ac9ee..c320d8db 100644 --- a/test/fragments/react/f3.fs +++ b/test/fragments/react/f3.fs @@ -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 diff --git a/test/fragments/react/f4.fs b/test/fragments/react/f4.fs index c0ed1caf..6907ff68 100644 --- a/test/fragments/react/f4.fs +++ b/test/fragments/react/f4.fs @@ -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 diff --git a/test/fragments/react/f5.fs b/test/fragments/react/f5.fs index c2343d9e..48308d00 100644 --- a/test/fragments/react/f5.fs +++ b/test/fragments/react/f5.fs @@ -2,7 +2,7 @@ module rec f5 open System open Fable.Core -open Fable.Import.JS +open Fable.Core.JS let [] react: React.IExports = jsNative diff --git a/test/fragments/react/f6.fs b/test/fragments/react/f6.fs index a10f3ab6..ea22cc4d 100644 --- a/test/fragments/react/f6.fs +++ b/test/fragments/react/f6.fs @@ -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 diff --git a/test/fragments/react/f7.fs b/test/fragments/react/f7.fs index 015bf03a..bdaa466d 100644 --- a/test/fragments/react/f7.fs +++ b/test/fragments/react/f7.fs @@ -2,7 +2,7 @@ module rec f7 open System open Fable.Core -open Fable.Import.JS +open Fable.Core.JS type SyntheticEvent = React.SyntheticEvent diff --git a/test/fragments/react/f8.fs b/test/fragments/react/f8.fs index 6fc5a547..5faeb795 100644 --- a/test/fragments/react/f8.fs +++ b/test/fragments/react/f8.fs @@ -2,7 +2,7 @@ module rec f8 open System open Fable.Core -open Fable.Import.JS +open Fable.Core.JS type Validator = React.Validator diff --git a/test/fragments/reactxp/duplicatedVariableExports.fs b/test/fragments/reactxp/duplicatedVariableExports.fs index 52532a01..7f599fb5 100644 --- a/test/fragments/reactxp/duplicatedVariableExports.fs +++ b/test/fragments/reactxp/duplicatedVariableExports.fs @@ -2,7 +2,7 @@ module rec duplicatedVariableExports open System open Fable.Core -open Fable.Import.JS +open Fable.Core.JS let [] reactXP: ReactXP.IExports = jsNative diff --git a/test/fragments/reactxp/multiple/ReactXP.fs b/test/fragments/reactxp/multiple/ReactXP.fs index 3a025b88..9089407b 100644 --- a/test/fragments/reactxp/multiple/ReactXP.fs +++ b/test/fragments/reactxp/multiple/ReactXP.fs @@ -2,7 +2,7 @@ module rec ReactXP open System open Fable.Core -open Fable.Import.JS +open Fable.Core.JS module ReactXP = __web_ReactXP diff --git a/test/fragments/regressions/#275-private-members.expected.fs b/test/fragments/regressions/#275-private-members.expected.fs index 42b26dde..c74e4c92 100644 --- a/test/fragments/regressions/#275-private-members.expected.fs +++ b/test/fragments/regressions/#275-private-members.expected.fs @@ -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 [] privateMembersTests: PrivateMembersTests.IExports = jsNative diff --git a/test/fragments/regressions/#277-unwrap-options.expected.fs b/test/fragments/regressions/#277-unwrap-options.expected.fs index d9d48909..a8d162f0 100644 --- a/test/fragments/regressions/#277-unwrap-options.expected.fs +++ b/test/fragments/regressions/#277-unwrap-options.expected.fs @@ -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 [] unwrapOptions: UnwrapOptions.IExports = jsNative let [] unwrapOptionsAlias: UnwrapOptionsAlias.IExports = jsNative diff --git a/test/fragments/regressions/#278-typeliterals-return.expected.fs b/test/fragments/regressions/#278-typeliterals-return.expected.fs index a7b0aae5..74f876e2 100644 --- a/test/fragments/regressions/#278-typeliterals-return.expected.fs +++ b/test/fragments/regressions/#278-typeliterals-return.expected.fs @@ -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 [] typeLiteralsAsReturnValue: TypeLiteralsAsReturnValue.IExports = jsNative diff --git a/test/fragments/regressions/#288-type-alias-float-number.expected.fs b/test/fragments/regressions/#288-type-alias-float-number.expected.fs index c6637939..3204f0f5 100644 --- a/test/fragments/regressions/#288-type-alias-float-number.expected.fs +++ b/test/fragments/regressions/#288-type-alias-float-number.expected.fs @@ -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 [] typeAloasFloatNumber: TypeAloasFloatNumber.IExports = jsNative diff --git a/test/fragments/regressions/#289-recursive-merge-modules.expected.fs b/test/fragments/regressions/#289-recursive-merge-modules.expected.fs index f71dc2d1..d79fbb4f 100644 --- a/test/fragments/regressions/#289-recursive-merge-modules.expected.fs +++ b/test/fragments/regressions/#289-recursive-merge-modules.expected.fs @@ -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 = diff --git a/test/fragments/regressions/#292-static-props.expected.fs b/test/fragments/regressions/#292-static-props.expected.fs index d8098bea..c8ac5e7b 100644 --- a/test/fragments/regressions/#292-static-props.expected.fs +++ b/test/fragments/regressions/#292-static-props.expected.fs @@ -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 [] staticTests: StaticTests.IExports = jsNative diff --git a/test/fragments/regressions/#311-error-to-exception.d.ts b/test/fragments/regressions/#311-error-to-exception.d.ts new file mode 100644 index 00000000..525e18a0 --- /dev/null +++ b/test/fragments/regressions/#311-error-to-exception.d.ts @@ -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; +} + diff --git a/test/fragments/regressions/#311-error-to-exception.expected.fs b/test/fragments/regressions/#311-error-to-exception.expected.fs new file mode 100644 index 00000000..9b41b539 --- /dev/null +++ b/test/fragments/regressions/#311-error-to-exception.expected.fs @@ -0,0 +1,16 @@ +// ts2fable 0.0.0 +module rec ``#311-error-to-exception`` +open System +open Fable.Core +open Fable.Core.JS + +let [] errorExceptionTest: ErrorExceptionTest.IExports = jsNative + +module ErrorExceptionTest = + + type [] IExports = + abstract instanceErrorProperty: System.Exception + abstract instanceInheritFromErrorProperty: InheritFromError + + type InheritFromError = + System.Exception diff --git a/test/fragments/yargs/duplicateOption.fs b/test/fragments/yargs/duplicateOption.fs index a9d9a587..1c4d173d 100644 --- a/test/fragments/yargs/duplicateOption.fs +++ b/test/fragments/yargs/duplicateOption.fs @@ -2,7 +2,7 @@ module rec duplicateOption open System open Fable.Core -open Fable.Import.JS +open Fable.Core.JS type [] Options = diff --git a/test/fsFileTests.fs b/test/fsFileTests.fs index c94d51f6..02299e67 100644 --- a/test/fsFileTests.fs +++ b/test/fsFileTests.fs @@ -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/311 + it "regression #311 error to exception" <| fun _ -> + runRegressionTest "#311-error-to-exception"