Skip to content

Commit

Permalink
Merge pull request #293 from 0x53A/static-props
Browse files Browse the repository at this point in the history
handle static properties
  • Loading branch information
humhei authored Feb 25, 2019
2 parents 322b24c + 551ae68 commit 3ed8e49
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/read.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 4 additions & 0 deletions src/syntax.fs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ type FsProperty =
Option: bool
Type: FsType
IsReadonly: bool
IsStatic: bool
Accessibility : FsAccessibility option
}

Expand Down Expand Up @@ -218,6 +219,7 @@ type FsVariable =
Name: string
Type: FsType
IsConst: bool
IsStatic: bool
Accessibility : FsAccessibility option
}

Expand Down Expand Up @@ -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) =
Expand Down
4 changes: 4 additions & 0 deletions src/transform.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions test/fragments/regressions/#292-static-props.d.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}

23 changes: 23 additions & 0 deletions test/fragments/regressions/#292-static-props.expected.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// ts2fable 0.0.0
module rec #292-static-props
open System
open Fable.Core
open Fable.Import.JS

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

module StaticTests =

type [<AllowNullLiteral>] IExports =
abstract PropertiesClass: PropertiesClassStatic

/// this class should contain 4 properties - 2 static ones, 2 instance ones.
type [<AllowNullLiteral>] 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 [<AllowNullLiteral>] PropertiesClassStatic =
[<Emit "new $0($1...)">] abstract Create: unit -> PropertiesClass
abstract staticProperty: float with get, set
abstract publicStaticProperty: float with get, set
6 changes: 5 additions & 1 deletion test/fsFileTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
runRegressionTest "#289-recursive-merge-modules"

// https://github.com/fable-compiler/ts2fable/issues/292
it "regression #292 static props" <| fun _ ->
runRegressionTest "#292-static-props"

0 comments on commit 3ed8e49

Please sign in to comment.