Skip to content

Commit 67bcdd6

Browse files
committed
support namespaces
1 parent 2f67161 commit 67bcdd6

File tree

3 files changed

+13135
-13606
lines changed

3 files changed

+13135
-13606
lines changed

TS.fsx

+76-30
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ module Types =
118118
| Ctor of Browser.Constructor
119119
| CallBackFun of Browser.CallbackFunction
120120

121+
type InterfaceOrNamespace =
122+
| Interface of Browser.Interface
123+
| Namespace of Browser.Namespace
124+
121125
// Note:
122126
// Eventhandler's name and the eventName are not just off by "on".
123127
// For example, handlers named "onabort" may handle "SVGAbort" event in the XML file
@@ -159,6 +163,7 @@ module InputJson =
159163
| SignatureOverload
160164
| TypeDef
161165
| Extends
166+
| Namespace
162167
override x.ToString() =
163168
match x with
164169
| Property _ -> "property"
@@ -171,6 +176,7 @@ module InputJson =
171176
| SignatureOverload _ -> "signatureoverload"
172177
| TypeDef _ -> "typedef"
173178
| Extends _ -> "extends"
179+
| Namespace _ -> "namespace"
174180

175181
let getItemByName (allItems: InputJsonType.Root []) (itemName: string) (kind: ItemKind) otherFilter =
176182
let filter (item: InputJsonType.Root) =
@@ -367,6 +373,11 @@ module Data =
367373
let isFromBrowserXml = browser.Enums |> Array.filter (fun i -> knownWorkerEnums.Contains i.Name)
368374
Array.append isFromBrowserXml worker.Enums
369375

376+
let GetNamespacesByFlavor flavor =
377+
match flavor with
378+
| Flavor.Web | Flavor.All -> browser.Namespaces
379+
| Flavor.Worker -> worker.Namespaces
380+
370381
/// Event name to event type map
371382
let eNameToEType =
372383
[ for i in allWebNonCallbackInterfaces do
@@ -902,7 +913,16 @@ module Emit =
902913
| Some pollutor -> "this: " + pollutor.Name + ", "
903914
| _ -> ""
904915

905-
let EmitProperties flavor prefix (emitScope: EmitScope) (i: Browser.Interface) (conflictedMembers: Set<string>) =
916+
let EmitProperties flavor prefix (emitScope: EmitScope) (i: InterfaceOrNamespace) (conflictedMembers: Set<string>) =
917+
let name =
918+
match i with
919+
| InterfaceOrNamespace.Interface it -> it.Name
920+
| InterfaceOrNamespace.Namespace n -> n.Name
921+
let properties =
922+
match i with
923+
| InterfaceOrNamespace.Interface it -> it.Properties
924+
| InterfaceOrNamespace.Namespace n -> n.Properties
925+
906926
let emitPropertyFromJson (p: InputJsonType.Root) =
907927
let readOnlyModifier =
908928
match p.Readonly with
@@ -911,7 +931,7 @@ module Emit =
911931
Pt.Printl "%s%s%s: %s;" prefix readOnlyModifier p.Name.Value p.Type.Value
912932

913933
let emitCommentForProperty (printLine: Printf.StringFormat<_, unit> -> _) pName =
914-
match CommentJson.GetCommentForProperty i.Name pName with
934+
match CommentJson.GetCommentForProperty name pName with
915935
| Some comment -> printLine "%s" comment
916936
| _ -> ()
917937

@@ -921,24 +941,24 @@ module Emit =
921941
emitCommentForProperty printLine p.Name
922942

923943
// Treat window.name specially because of https://github.com/Microsoft/TypeScript/issues/9850
924-
if p.Name = "name" && i.Name = "Window" && emitScope = EmitScope.All then
944+
if p.Name = "name" && name = "Window" && emitScope = EmitScope.All then
925945
printLine "declare const name: never;"
926-
elif Option.isNone (getRemovedItemByName p.Name ItemKind.Property i.Name) then
927-
match getOverriddenItemByName p.Name ItemKind.Property i.Name with
946+
elif Option.isNone (getRemovedItemByName p.Name ItemKind.Property name) then
947+
match getOverriddenItemByName p.Name ItemKind.Property name with
928948
| Some p' -> emitPropertyFromJson p'
929949
| None ->
930950
let pType =
931-
match p.Type with
932-
| "EventHandler" ->
951+
match (i, p.Type) with
952+
| (InterfaceOrNamespace.Interface it, "EventHandler") ->
933953
// Sometimes event handlers with the same name may actually handle different
934954
// events in different interfaces. For example, "onerror" handles "ErrorEvent"
935955
// normally, but in "SVGSVGElement" it handles "SVGError" event instead.
936956
let eType =
937957
if p.EventHandler.IsSome then
938-
getEventTypeInInterface p.EventHandler.Value i
958+
getEventTypeInInterface p.EventHandler.Value it
939959
else
940960
"Event"
941-
String.Format("({0}ev: {1}) => any", EmitEventHandlerThis flavor prefix i, eType)
961+
String.Format("({0}ev: {1}) => any", EmitEventHandlerThis flavor prefix it, eType)
942962
| _ -> DomTypeToTsType p.Type
943963
let pTypeAndNull = if p.Nullable.IsSome then makeNullable pType else pType
944964
let readOnlyModifier = if p.ReadOnly.IsSome && prefix = "" then "readonly " else ""
@@ -947,19 +967,29 @@ module Emit =
947967
// Note: the schema file shows the property doesn't have "static" attribute,
948968
// therefore all properties are emited for the instance type.
949969
if emitScope <> StaticOnly then
950-
match i.Properties with
970+
match properties with
951971
| Some ps ->
952972
ps.Properties
953973
|> Array.filter (ShouldKeep flavor)
954974
|> Array.iter emitProperty
955975
| None -> ()
956976

957977
for addedItem in getAddedItems ItemKind.Property flavor do
958-
if (matchInterface i.Name addedItem) && (prefix <> "declare var " || addedItem.ExposeGlobally.IsNone || addedItem.ExposeGlobally.Value) then
978+
if (matchInterface name addedItem) && (prefix <> "declare var " || addedItem.ExposeGlobally.IsNone || addedItem.ExposeGlobally.Value) then
959979
emitCommentForProperty Pt.Printl addedItem.Name.Value
960980
emitPropertyFromJson addedItem
961981

962-
let EmitMethods flavor prefix (emitScope: EmitScope) (i: Browser.Interface) (conflictedMembers: Set<string>) =
982+
let EmitMethods flavor prefix (emitScope: EmitScope) (i: InterfaceOrNamespace) (conflictedMembers: Set<string>) =
983+
let name =
984+
match i with
985+
| InterfaceOrNamespace.Interface it -> it.Name
986+
| InterfaceOrNamespace.Namespace n -> n.Name
987+
988+
let methods =
989+
match i with
990+
| InterfaceOrNamespace.Interface it -> it.Methods
991+
| InterfaceOrNamespace.Namespace n -> n.Methods
992+
963993
// Note: two cases:
964994
// 1. emit the members inside a interface -> no need to add prefix
965995
// 2. emit the members outside to expose them (for "Window") -> need to add "declare"
@@ -968,7 +998,7 @@ module Emit =
968998

969999
let emitCommentForMethod (printLine: Printf.StringFormat<_, unit> -> _) (mName: string option) =
9701000
if mName.IsSome then
971-
match CommentJson.GetCommentForMethod i.Name mName.Value with
1001+
match CommentJson.GetCommentForMethod name mName.Value with
9721002
| Some comment -> printLine "%s" comment
9731003
| _ -> ()
9741004

@@ -978,7 +1008,7 @@ module Emit =
9781008
matchScope emitScope m &&
9791009
not (prefix <> "" && OptionCheckValue "addEventListener" m.Name)
9801010

981-
let emitMethod flavor prefix (i:Browser.Interface) (m:Browser.Method) =
1011+
let emitMethod flavor prefix (i: InterfaceOrNamespace) (m:Browser.Method) =
9821012
let printLine content =
9831013
if m.Name.IsSome && conflictedMembers.Contains m.Name.Value then Pt.PrintlToStack content else Pt.Printl content
9841014
// print comment
@@ -989,8 +1019,8 @@ module Emit =
9891019
// - removedType: meaning the type is marked as removed in the json file
9901020
// if there is any conflicts between the two, the "removedType" has a higher priority over
9911021
// the "overridenType".
992-
let removedType = Option.bind (fun name -> InputJson.getRemovedItemByName name InputJson.ItemKind.Method i.Name) m.Name
993-
let overridenType = Option.bind (fun mName -> InputJson.getOverriddenItemByName mName InputJson.ItemKind.Method i.Name) m.Name
1022+
let removedType = Option.bind (fun name -> InputJson.getRemovedItemByName name InputJson.ItemKind.Method name) m.Name
1023+
let overridenType = Option.bind (fun mName -> InputJson.getOverriddenItemByName mName InputJson.ItemKind.Method name) m.Name
9941024

9951025
if removedType.IsNone then
9961026
match overridenType with
@@ -1000,7 +1030,7 @@ module Emit =
10001030
| _ -> ()
10011031
t.Signatures |> Array.iter (printLine "%s%s;" prefix)
10021032
| None ->
1003-
match i.Name, m.Name with
1033+
match name, m.Name with
10041034
| _, Some "createElement" -> EmitCreateElementOverloads m
10051035
| _, Some "createEvent" -> EmitCreateEventOverloads m
10061036
| _, Some "getElementsByTagName" -> EmitGetElementsByTagNameOverloads m
@@ -1009,7 +1039,7 @@ module Emit =
10091039
| _ ->
10101040
if m.Name.IsSome then
10111041
// If there are added overloads from the json files, print them first
1012-
match getAddedItemByName m.Name.Value ItemKind.SignatureOverload i.Name with
1042+
match getAddedItemByName m.Name.Value ItemKind.SignatureOverload name with
10131043
| Some ol -> ol.Signatures |> Array.iter (printLine "%s")
10141044
| _ -> ()
10151045

@@ -1021,25 +1051,29 @@ module Emit =
10211051
if isNullable then makeNullable returnType else returnType
10221052
printLine "%s%s(%s): %s;" prefix (if m.Name.IsSome then m.Name.Value else "") paramsString returnString
10231053

1024-
if i.Methods.IsSome then
1025-
i.Methods.Value.Methods
1054+
if methods.IsSome then
1055+
methods.Value.Methods
10261056
|> Array.filter mFilter
10271057
|> Array.iter (emitMethod flavor prefix i)
10281058

10291059
for addedItem in getAddedItems ItemKind.Method flavor do
1030-
if (matchInterface i.Name addedItem && matchScope emitScope addedItem) then
1060+
if (matchInterface name addedItem && matchScope emitScope addedItem) then
10311061
emitCommentForMethod Pt.Printl addedItem.Name
10321062
emitMethodFromJson addedItem
10331063

10341064
// The window interface inherited some methods from "Object",
10351065
// which need to explicitly exposed
1036-
if i.Name = "Window" && prefix = "declare function " then
1066+
if name = "Window" && prefix = "declare function " then
10371067
Pt.Printl "declare function toString(): string;"
10381068

10391069
/// Emit the properties and methods of a given interface
1040-
let EmitMembers flavor (prefix: string) (emitScope: EmitScope) (i:Browser.Interface) =
1070+
let EmitMembers flavor (prefix: string) (emitScope: EmitScope) (i: InterfaceOrNamespace) =
1071+
let name =
1072+
match i with
1073+
| InterfaceOrNamespace.Interface it -> it.Name
1074+
| InterfaceOrNamespace.Namespace n -> n.Name
10411075
let conflictedMembers =
1042-
match Map.tryFind i.Name extendConflictsBaseTypes with
1076+
match Map.tryFind name extendConflictsBaseTypes with
10431077
| Some conflict -> conflict.MemberNames
10441078
| _ -> []
10451079
|> Set.ofList
@@ -1051,7 +1085,7 @@ module Emit =
10511085
/// Called only once on the global polluter object
10521086
let rec EmitAllMembers flavor (i:Browser.Interface) =
10531087
let prefix = "declare var "
1054-
EmitMembers flavor prefix EmitScope.All i
1088+
EmitMembers flavor prefix EmitScope.All (InterfaceOrNamespace.Interface i)
10551089

10561090
for relatedIName in iNameToIDependList.[i.Name] do
10571091
match GetInterfaceByName relatedIName with
@@ -1117,7 +1151,7 @@ module Emit =
11171151
EmitConstructorSignature i
11181152
EmitConstants i
11191153
let prefix = ""
1120-
EmitMembers flavor prefix EmitScope.StaticOnly i
1154+
EmitMembers flavor prefix EmitScope.StaticOnly (InterfaceOrNamespace.Interface i)
11211155

11221156
Pt.DecreaseIndent()
11231157
Pt.Printl "}"
@@ -1265,7 +1299,7 @@ module Emit =
12651299
Pt.IncreaseIndent()
12661300

12671301
let prefix = ""
1268-
EmitMembers flavor prefix EmitScope.InstanceOnly i
1302+
EmitMembers flavor prefix EmitScope.InstanceOnly (InterfaceOrNamespace.Interface i)
12691303
EmitConstants i
12701304
EmitEventHandlers flavor prefix i
12711305
EmitIndexers EmitScope.InstanceOnly i
@@ -1324,7 +1358,7 @@ module Emit =
13241358
Pt.IncreaseIndent()
13251359

13261360
let prefix = ""
1327-
EmitMembers flavor prefix EmitScope.InstanceOnly i
1361+
EmitMembers flavor prefix EmitScope.InstanceOnly (InterfaceOrNamespace.Interface i)
13281362
EmitEventHandlers flavor prefix i
13291363
EmitIndexers EmitScope.InstanceOnly i
13301364

@@ -1334,7 +1368,7 @@ module Emit =
13341368
Pt.Printl "declare var %s: {" i.Name
13351369
Pt.IncreaseIndent()
13361370
EmitConstants i
1337-
EmitMembers flavor prefix EmitScope.StaticOnly i
1371+
EmitMembers flavor prefix EmitScope.StaticOnly (InterfaceOrNamespace.Interface i)
13381372
emitAddedConstructor ()
13391373
Pt.DecreaseIndent()
13401374
Pt.Printl "}"
@@ -1346,7 +1380,7 @@ module Emit =
13461380
Pt.IncreaseIndent()
13471381

13481382
let prefix = ""
1349-
EmitMembers flavor prefix EmitScope.StaticOnly i
1383+
EmitMembers flavor prefix EmitScope.StaticOnly (InterfaceOrNamespace.Interface i)
13501384
EmitConstants i
13511385
EmitEventHandlers flavor prefix i
13521386
EmitIndexers EmitScope.StaticOnly i
@@ -1462,6 +1496,17 @@ module Emit =
14621496

14631497
InputJson.getAddedItems ItemKind.TypeDef flavor
14641498
|> Array.iter emitTypeDefFromJson
1499+
1500+
let EmitNamespaces flavor =
1501+
let EmitNamespace (n: Browser.Namespace) =
1502+
Pt.Printl "declare namespace %s {" n.Name
1503+
Pt.IncreaseIndent()
1504+
EmitMembers flavor "" EmitScope.All (InterfaceOrNamespace.Namespace n)
1505+
Pt.DecreaseIndent()
1506+
Pt.Printl "}"
1507+
Pt.Printl ""
1508+
1509+
GetNamespacesByFlavor flavor |> Array.iter EmitNamespace
14651510

14661511
let EmitTheWholeThing flavor (target:TextWriter) =
14671512
Pt.Reset()
@@ -1475,6 +1520,7 @@ module Emit =
14751520
EmitDictionaries flavor
14761521
browser.CallbackInterfaces.Interfaces |> Array.iter EmitCallBackInterface
14771522
EmitNonCallbackInterfaces flavor
1523+
EmitNamespaces flavor
14781524

14791525
// Add missed interface definition from the spec
14801526
InputJson.getAddedItems InputJson.Interface flavor |> Array.iter EmitAddedInterface

0 commit comments

Comments
 (0)