Skip to content

Commit 5d14836

Browse files
Zhengbo LiZhengbo Li
Zhengbo Li
authored and
Zhengbo Li
committed
Detect added json items for static interfaces
1 parent 1e76d8b commit 5d14836

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

Shared.fsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,19 @@ module JsonItems =
8888
t.Kind.ToLower() = kind.ToString() &&
8989
(t.Flavor.IsNone || t.Flavor.Value = flavor.ToString() || flavor = All))
9090

91-
let getOverriddenItems kind flavor = getItems overriddenItems kind flavor
92-
let getAddedItems kind flavor = getItems addedItems kind flavor
93-
let getRemovedItems kind flavor = getItems removedItems kind flavor
91+
let getOverriddenItems kind flavor =
92+
getItems overriddenItems kind flavor
93+
let getAddedItems kind flavor =
94+
getItems addedItems kind flavor
95+
let getRemovedItems kind flavor =
96+
getItems removedItems kind flavor
97+
98+
let getAddedItemsByInterfaceName kind flavor iName =
99+
getItems addedItems kind flavor |> Array.filter (matchInterface iName)
100+
let getOverriddenItemsByInterfaceName kind flavor iName =
101+
getItems overriddenItems kind flavor |> Array.filter (matchInterface iName)
102+
let getRemovedItemsByInterfaceName kind flavor iName =
103+
getItems removedItems kind flavor |> Array.filter (matchInterface iName)
94104

95105
module Comments =
96106
type CommentType = JsonProvider<"inputfiles/comments.json">

TS.fsx

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -465,17 +465,33 @@ let EmitStaticInterface flavor (i:Browser.Interface) =
465465
// Some types are static types with non-static members. For example,
466466
// NodeFilter is a static method itself, however it has an "acceptNode" method
467467
// that expects the user to implement.
468-
let hasNonStaticMember =
469-
match i.Methods with
470-
| Some ms -> ms.Methods |> Array.exists (fun m -> m.Static.IsNone)
471-
| _ -> false
468+
let hasNonStaticMember =
469+
let hasNonStaticMethod =
470+
match JsonItems.getAddedItemsByInterfaceName ItemKind.Method flavor i.Name with
471+
| [||] ->
472+
match i.Methods with
473+
| Some ms -> ms.Methods |> Array.exists (fun m -> m.Static.IsNone)
474+
| _ -> false
475+
| addedMs -> addedMs |> Array.exists (fun m -> m.Static.IsNone || m.Static.Value = false)
476+
let hasProperty =
477+
if i.Properties.IsSome then true
478+
else
479+
JsonItems.getAddedItemsByInterfaceName ItemKind.Property flavor i.Name |> Array.isEmpty |> not
480+
hasNonStaticMethod || hasProperty
481+
482+
let emitAddedConstructor () =
483+
match JsonItems.getAddedItemsByInterfaceName ItemKind.Constructor flavor i.Name with
484+
| [||] -> ()
485+
| ctors ->
486+
Pt.printl "prototype: %s;" i.Name
487+
ctors |> Array.iter (fun ctor -> ctor.Signatures |> Array.iter (Pt.printl "%s;"))
472488

473489
// For static types with non-static members, we put the non-static members into an
474490
// interface, and put the static members into the object literal type of 'declare var'
475491
// For static types with only static members, we put everything in the interface.
476492
// Because in the two cases the interface contains different things, it might be easier to
477493
// read to seperate them into two functions.
478-
let emitStaticInterfaceWithNonStaticMembers () =
494+
let emitStaticInterfaceWithNonStaticMembers () =
479495
Pt.resetIndent()
480496
EmitInterfaceDeclaration i
481497
Pt.increaseIndent()
@@ -492,6 +508,7 @@ let EmitStaticInterface flavor (i:Browser.Interface) =
492508
Pt.increaseIndent()
493509
EmitConstants i
494510
EmitMembers flavor prefix EmitScope.StaticOnly i
511+
emitAddedConstructor ()
495512
Pt.decreaseIndent()
496513
Pt.printl "}"
497514
Pt.printl ""
@@ -506,7 +523,7 @@ let EmitStaticInterface flavor (i:Browser.Interface) =
506523
EmitConstants i
507524
EmitEventHandlers prefix i
508525
EmitIndexers EmitScope.StaticOnly i
509-
526+
emitAddedConstructor ()
510527
Pt.decreaseIndent()
511528
Pt.printl "}"
512529
Pt.printl "declare var %s: %s;" i.Name i.Name
@@ -616,4 +633,4 @@ let EmitDomWeb () =
616633

617634
let EmitDomWorker () =
618635
ignoreDOMTypes <- true
619-
EmitTheWholeThing Flavor.Worker GlobalVars.tsWorkerOutput
636+
EmitTheWholeThing Flavor.Worker GlobalVars.tsWorkerOutput

0 commit comments

Comments
 (0)