From 551ae68c9ffc9b427c49e91e348a74122f80e9f6 Mon Sep 17 00:00:00 2001 From: Lukas Rieger Date: Sun, 24 Feb 2019 22:36:20 +0100 Subject: [PATCH] handle static properties --- src/read.fs | 4 ++++ src/syntax.fs | 4 ++++ src/transform.fs | 4 ++++ .../regressions/#292-static-props.d.ts | 18 +++++++++++++++ .../regressions/#292-static-props.expected.fs | 23 +++++++++++++++++++ test/fsFileTests.fs | 6 ++++- 6 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 test/fragments/regressions/#292-static-props.d.ts create mode 100644 test/fragments/regressions/#292-static-props.expected.fs diff --git a/src/read.fs b/src/read.fs index 928f0ef7..50af7a06 100644 --- a/src/read.fs +++ b/src/read.fs @@ -217,6 +217,7 @@ let readVariable (checker: TypeChecker) (vb: VariableStatement) = Name = vd.name |> getBindingName Type = vd.``type`` |> Option.map (readTypeNode checker) |> Option.defaultValue (simpleType "obj") IsConst = isConst vd + IsStatic = hasModifier SyntaxKind.StaticKeyword vd.modifiers Accessibility = getAccessibility vb.modifiers } |> FsType.Variable @@ -447,6 +448,7 @@ let readPropertySignature (checker: TypeChecker) (ps: PropertySignature): FsProp | None -> FsType.None | Some tp -> readTypeNode checker tp IsReadonly = isReadOnly ps.modifiers + IsStatic = hasModifier SyntaxKind.StaticKeyword ps.modifiers Accessibility = getAccessibility ps.modifiers } @@ -469,6 +471,7 @@ let readPropertyDeclaration (checker: TypeChecker) (pd: PropertyDeclaration): Fs | None -> FsType.None | Some tp -> readTypeNode checker tp IsReadonly = isReadOnly pd.modifiers + IsStatic = hasModifier SyntaxKind.StaticKeyword pd.modifiers Accessibility = getAccessibility pd.modifiers } @@ -500,6 +503,7 @@ let readIndexSignature (checker: TypeChecker) (ps: IndexSignatureDeclaration): F | None -> FsType.None | Some tp -> readTypeNode checker tp IsReadonly = isReadOnly ps.modifiers + IsStatic = hasModifier SyntaxKind.StaticKeyword ps.modifiers Accessibility = getAccessibility ps.modifiers } diff --git a/src/syntax.fs b/src/syntax.fs index 2a1baf57..9ee76234 100644 --- a/src/syntax.fs +++ b/src/syntax.fs @@ -146,6 +146,7 @@ type FsProperty = Option: bool Type: FsType IsReadonly: bool + IsStatic: bool Accessibility : FsAccessibility option } @@ -218,6 +219,7 @@ type FsVariable = Name: string Type: FsType IsConst: bool + IsStatic: bool Accessibility : FsAccessibility option } @@ -334,6 +336,8 @@ let isStatic (tp: FsType) = match tp with | FsType.Function fn -> fn.IsStatic | FsType.Interface it -> it.IsStatic + | FsType.Property p -> p.IsStatic + | FsType.Variable v -> v.IsStatic | _ -> false let isConstructor (tp: FsType) = diff --git a/src/transform.fs b/src/transform.fs index 31f061ca..331532c9 100644 --- a/src/transform.fs +++ b/src/transform.fs @@ -287,6 +287,7 @@ let rec createIExportsModule (ns: string list) (md: FsModule): FsModule * FsVari Option = false Type = it.Name |> simpleType IsReadonly = true + IsStatic = false Accessibility = None } |> FsType.Property @@ -313,6 +314,7 @@ let rec createIExportsModule (ns: string list) (md: FsModule): FsModule * FsVari Name = md.Name |> lowerFirst Type = sprintf "%s.IExports" (fixModuleName md.Name) |> simpleType IsConst = true + IsStatic = false Accessibility = None } |> variablesForParent.Add @@ -323,6 +325,7 @@ let rec createIExportsModule (ns: string list) (md: FsModule): FsModule * FsVari Name = md.Name |> lowerFirst Type = sprintf "%s.IExports" (fixModuleName md.Name) |> simpleType IsConst = true + IsStatic = false Accessibility = None } |> variablesForParent.Add @@ -360,6 +363,7 @@ let rec createIExportsModule (ns: string list) (md: FsModule): FsModule * FsVari Name = smd.Name |> lowerFirst Type = sprintf "%s.IExports" (fixModuleName smd.Name) |> simpleType IsConst = true + IsStatic = false Accessibility = None } |> variables.Add |> ignore diff --git a/test/fragments/regressions/#292-static-props.d.ts b/test/fragments/regressions/#292-static-props.d.ts new file mode 100644 index 00000000..51093071 --- /dev/null +++ b/test/fragments/regressions/#292-static-props.d.ts @@ -0,0 +1,18 @@ + +export module StaticTests { + + /** this class should contain 4 properties - 2 static ones, 2 instance ones. */ + class PropertiesClass { + static staticProperty: number; + instanceProperty: number; + + /** these should NOT be emitted */ + private static privateStaticProperty: number; + /** these should NOT be emitted */ + private privateInstanceProperty: number; + + public static publicStaticProperty: number; + public publicInstanceProperty: number; + } +} + diff --git a/test/fragments/regressions/#292-static-props.expected.fs b/test/fragments/regressions/#292-static-props.expected.fs new file mode 100644 index 00000000..d8098bea --- /dev/null +++ b/test/fragments/regressions/#292-static-props.expected.fs @@ -0,0 +1,23 @@ +// ts2fable 0.0.0 +module rec #292-static-props +open System +open Fable.Core +open Fable.Import.JS + +let [] staticTests: StaticTests.IExports = jsNative + +module StaticTests = + + type [] IExports = + abstract PropertiesClass: PropertiesClassStatic + + /// this class should contain 4 properties - 2 static ones, 2 instance ones. + type [] PropertiesClass = + abstract instanceProperty: float with get, set + abstract publicInstanceProperty: float with get, set + + /// this class should contain 4 properties - 2 static ones, 2 instance ones. + type [] PropertiesClassStatic = + [] abstract Create: unit -> PropertiesClass + abstract staticProperty: float with get, set + abstract publicStaticProperty: float with get, set diff --git a/test/fsFileTests.fs b/test/fsFileTests.fs index 6ab22ef1..c94d51f6 100644 --- a/test/fsFileTests.fs +++ b/test/fsFileTests.fs @@ -376,4 +376,8 @@ describe "transform tests" <| fun _ -> // https://github.com/fable-compiler/ts2fable/issues/280 // https://github.com/fable-compiler/ts2fable/pull/289 it "regression #289 merging outer modules" <| fun _ -> - runRegressionTest "#289-recursive-merge-modules" \ No newline at end of file + runRegressionTest "#289-recursive-merge-modules" + + // https://github.com/fable-compiler/ts2fable/issues/292 + it "regression #292 static props" <| fun _ -> + runRegressionTest "#292-static-props"