@@ -118,6 +118,10 @@ module Types =
118
118
| Ctor of Browser.Constructor
119
119
| CallBackFun of Browser.CallbackFunction
120
120
121
+ type InterfaceOrNamespace =
122
+ | Interface of Browser.Interface
123
+ | Namespace of Browser.Namespace
124
+
121
125
// Note:
122
126
// Eventhandler's name and the eventName are not just off by "on".
123
127
// For example, handlers named "onabort" may handle "SVGAbort" event in the XML file
@@ -159,6 +163,7 @@ module InputJson =
159
163
| SignatureOverload
160
164
| TypeDef
161
165
| Extends
166
+ | Namespace
162
167
override x.ToString () =
163
168
match x with
164
169
| Property _ -> " property"
@@ -171,6 +176,7 @@ module InputJson =
171
176
| SignatureOverload _ -> " signatureoverload"
172
177
| TypeDef _ -> " typedef"
173
178
| Extends _ -> " extends"
179
+ | Namespace _ -> " namespace"
174
180
175
181
let getItemByName ( allItems : InputJsonType.Root []) ( itemName : string ) ( kind : ItemKind ) otherFilter =
176
182
let filter ( item : InputJsonType.Root ) =
@@ -367,6 +373,11 @@ module Data =
367
373
let isFromBrowserXml = browser.Enums |> Array.filter ( fun i -> knownWorkerEnums.Contains i.Name)
368
374
Array.append isFromBrowserXml worker.Enums
369
375
376
+ let GetNamespacesByFlavor flavor =
377
+ match flavor with
378
+ | Flavor.Web | Flavor.All -> browser.Namespaces
379
+ | Flavor.Worker -> worker.Namespaces
380
+
370
381
/// Event name to event type map
371
382
let eNameToEType =
372
383
[ for i in allWebNonCallbackInterfaces do
@@ -902,7 +913,16 @@ module Emit =
902
913
| Some pollutor -> " this: " + pollutor.Name + " , "
903
914
| _ -> " "
904
915
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
+
906
926
let emitPropertyFromJson ( p : InputJsonType.Root ) =
907
927
let readOnlyModifier =
908
928
match p.Readonly with
@@ -911,7 +931,7 @@ module Emit =
911
931
Pt.Printl " %s%s%s : %s ;" prefix readOnlyModifier p.Name.Value p.Type.Value
912
932
913
933
let emitCommentForProperty ( printLine : Printf.StringFormat < _ , unit > -> _ ) pName =
914
- match CommentJson.GetCommentForProperty i.Name pName with
934
+ match CommentJson.GetCommentForProperty name pName with
915
935
| Some comment -> printLine " %s " comment
916
936
| _ -> ()
917
937
@@ -921,24 +941,24 @@ module Emit =
921
941
emitCommentForProperty printLine p.Name
922
942
923
943
// 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
925
945
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
928
948
| Some p' -> emitPropertyFromJson p'
929
949
| None ->
930
950
let pType =
931
- match p.Type with
932
- | " EventHandler" ->
951
+ match ( i , p.Type) with
952
+ | ( InterfaceOrNamespace.Interface it , " EventHandler" ) ->
933
953
// Sometimes event handlers with the same name may actually handle different
934
954
// events in different interfaces. For example, "onerror" handles "ErrorEvent"
935
955
// normally, but in "SVGSVGElement" it handles "SVGError" event instead.
936
956
let eType =
937
957
if p.EventHandler.IsSome then
938
- getEventTypeInInterface p.EventHandler.Value i
958
+ getEventTypeInInterface p.EventHandler.Value it
939
959
else
940
960
" 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)
942
962
| _ -> DomTypeToTsType p.Type
943
963
let pTypeAndNull = if p.Nullable.IsSome then makeNullable pType else pType
944
964
let readOnlyModifier = if p.ReadOnly.IsSome && prefix = " " then " readonly " else " "
@@ -947,19 +967,29 @@ module Emit =
947
967
// Note: the schema file shows the property doesn't have "static" attribute,
948
968
// therefore all properties are emited for the instance type.
949
969
if emitScope <> StaticOnly then
950
- match i.Properties with
970
+ match properties with
951
971
| Some ps ->
952
972
ps.Properties
953
973
|> Array.filter ( ShouldKeep flavor)
954
974
|> Array.iter emitProperty
955
975
| None -> ()
956
976
957
977
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
959
979
emitCommentForProperty Pt.Printl addedItem.Name.Value
960
980
emitPropertyFromJson addedItem
961
981
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
+
963
993
// Note: two cases:
964
994
// 1. emit the members inside a interface -> no need to add prefix
965
995
// 2. emit the members outside to expose them (for "Window") -> need to add "declare"
@@ -968,7 +998,7 @@ module Emit =
968
998
969
999
let emitCommentForMethod ( printLine : Printf.StringFormat < _ , unit > -> _ ) ( mName : string option ) =
970
1000
if mName.IsSome then
971
- match CommentJson.GetCommentForMethod i.Name mName.Value with
1001
+ match CommentJson.GetCommentForMethod name mName.Value with
972
1002
| Some comment -> printLine " %s " comment
973
1003
| _ -> ()
974
1004
@@ -978,7 +1008,7 @@ module Emit =
978
1008
matchScope emitScope m &&
979
1009
not ( prefix <> " " && OptionCheckValue " addEventListener" m.Name)
980
1010
981
- let emitMethod flavor prefix ( i : Browser.Interface ) ( m : Browser.Method ) =
1011
+ let emitMethod flavor prefix ( i : InterfaceOrNamespace ) ( m : Browser.Method ) =
982
1012
let printLine content =
983
1013
if m.Name.IsSome && conflictedMembers.Contains m.Name.Value then Pt.PrintlToStack content else Pt.Printl content
984
1014
// print comment
@@ -989,8 +1019,8 @@ module Emit =
989
1019
// - removedType: meaning the type is marked as removed in the json file
990
1020
// if there is any conflicts between the two, the "removedType" has a higher priority over
991
1021
// 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
994
1024
995
1025
if removedType.IsNone then
996
1026
match overridenType with
@@ -1000,7 +1030,7 @@ module Emit =
1000
1030
| _ -> ()
1001
1031
t.Signatures |> Array.iter ( printLine " %s%s ;" prefix)
1002
1032
| None ->
1003
- match i.Name , m.Name with
1033
+ match name , m.Name with
1004
1034
| _, Some " createElement" -> EmitCreateElementOverloads m
1005
1035
| _, Some " createEvent" -> EmitCreateEventOverloads m
1006
1036
| _, Some " getElementsByTagName" -> EmitGetElementsByTagNameOverloads m
@@ -1009,7 +1039,7 @@ module Emit =
1009
1039
| _ ->
1010
1040
if m.Name.IsSome then
1011
1041
// 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
1013
1043
| Some ol -> ol.Signatures |> Array.iter ( printLine " %s " )
1014
1044
| _ -> ()
1015
1045
@@ -1021,25 +1051,29 @@ module Emit =
1021
1051
if isNullable then makeNullable returnType else returnType
1022
1052
printLine " %s%s (%s ): %s ;" prefix ( if m.Name.IsSome then m.Name.Value else " " ) paramsString returnString
1023
1053
1024
- if i.Methods .IsSome then
1025
- i.Methods .Value.Methods
1054
+ if methods .IsSome then
1055
+ methods .Value.Methods
1026
1056
|> Array.filter mFilter
1027
1057
|> Array.iter ( emitMethod flavor prefix i)
1028
1058
1029
1059
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
1031
1061
emitCommentForMethod Pt.Printl addedItem.Name
1032
1062
emitMethodFromJson addedItem
1033
1063
1034
1064
// The window interface inherited some methods from "Object",
1035
1065
// which need to explicitly exposed
1036
- if i.Name = " Window" && prefix = " declare function " then
1066
+ if name = " Window" && prefix = " declare function " then
1037
1067
Pt.Printl " declare function toString(): string;"
1038
1068
1039
1069
/// 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
1041
1075
let conflictedMembers =
1042
- match Map.tryFind i.Name extendConflictsBaseTypes with
1076
+ match Map.tryFind name extendConflictsBaseTypes with
1043
1077
| Some conflict -> conflict.MemberNames
1044
1078
| _ -> []
1045
1079
|> Set.ofList
@@ -1051,7 +1085,7 @@ module Emit =
1051
1085
/// Called only once on the global polluter object
1052
1086
let rec EmitAllMembers flavor ( i : Browser.Interface ) =
1053
1087
let prefix = " declare var "
1054
- EmitMembers flavor prefix EmitScope.All i
1088
+ EmitMembers flavor prefix EmitScope.All ( InterfaceOrNamespace.Interface i )
1055
1089
1056
1090
for relatedIName in iNameToIDependList.[ i.Name] do
1057
1091
match GetInterfaceByName relatedIName with
@@ -1117,7 +1151,7 @@ module Emit =
1117
1151
EmitConstructorSignature i
1118
1152
EmitConstants i
1119
1153
let prefix = " "
1120
- EmitMembers flavor prefix EmitScope.StaticOnly i
1154
+ EmitMembers flavor prefix EmitScope.StaticOnly ( InterfaceOrNamespace.Interface i )
1121
1155
1122
1156
Pt.DecreaseIndent()
1123
1157
Pt.Printl " }"
@@ -1265,7 +1299,7 @@ module Emit =
1265
1299
Pt.IncreaseIndent()
1266
1300
1267
1301
let prefix = " "
1268
- EmitMembers flavor prefix EmitScope.InstanceOnly i
1302
+ EmitMembers flavor prefix EmitScope.InstanceOnly ( InterfaceOrNamespace.Interface i )
1269
1303
EmitConstants i
1270
1304
EmitEventHandlers flavor prefix i
1271
1305
EmitIndexers EmitScope.InstanceOnly i
@@ -1324,7 +1358,7 @@ module Emit =
1324
1358
Pt.IncreaseIndent()
1325
1359
1326
1360
let prefix = " "
1327
- EmitMembers flavor prefix EmitScope.InstanceOnly i
1361
+ EmitMembers flavor prefix EmitScope.InstanceOnly ( InterfaceOrNamespace.Interface i )
1328
1362
EmitEventHandlers flavor prefix i
1329
1363
EmitIndexers EmitScope.InstanceOnly i
1330
1364
@@ -1334,7 +1368,7 @@ module Emit =
1334
1368
Pt.Printl " declare var %s : {" i.Name
1335
1369
Pt.IncreaseIndent()
1336
1370
EmitConstants i
1337
- EmitMembers flavor prefix EmitScope.StaticOnly i
1371
+ EmitMembers flavor prefix EmitScope.StaticOnly ( InterfaceOrNamespace.Interface i )
1338
1372
emitAddedConstructor ()
1339
1373
Pt.DecreaseIndent()
1340
1374
Pt.Printl " }"
@@ -1346,7 +1380,7 @@ module Emit =
1346
1380
Pt.IncreaseIndent()
1347
1381
1348
1382
let prefix = " "
1349
- EmitMembers flavor prefix EmitScope.StaticOnly i
1383
+ EmitMembers flavor prefix EmitScope.StaticOnly ( InterfaceOrNamespace.Interface i )
1350
1384
EmitConstants i
1351
1385
EmitEventHandlers flavor prefix i
1352
1386
EmitIndexers EmitScope.StaticOnly i
@@ -1462,6 +1496,17 @@ module Emit =
1462
1496
1463
1497
InputJson.getAddedItems ItemKind.TypeDef flavor
1464
1498
|> 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
1465
1510
1466
1511
let EmitTheWholeThing flavor ( target : TextWriter ) =
1467
1512
Pt.Reset()
@@ -1475,6 +1520,7 @@ module Emit =
1475
1520
EmitDictionaries flavor
1476
1521
browser.CallbackInterfaces.Interfaces |> Array.iter EmitCallBackInterface
1477
1522
EmitNonCallbackInterfaces flavor
1523
+ EmitNamespaces flavor
1478
1524
1479
1525
// Add missed interface definition from the spec
1480
1526
InputJson.getAddedItems InputJson.Interface flavor |> Array.iter EmitAddedInterface
0 commit comments