@@ -121,16 +121,16 @@ module InputJson =
121
121
open Helpers
122
122
open Types
123
123
124
- type InputJsonItem = JsonProvider< " inputfiles/sample.json" >
124
+ type InputJsonType = JsonProvider< " inputfiles/sample.json" >
125
125
126
126
let overriddenItems =
127
- File.ReadAllText( GlobalVars.inputFolder + @" /overridingTypes.json" ) |> InputJsonItem .Parse
127
+ File.ReadAllText( GlobalVars.inputFolder + @" /overridingTypes.json" ) |> InputJsonType .Parse
128
128
129
129
let removedItems =
130
- File.ReadAllText( GlobalVars.inputFolder + @" /removedTypes.json" ) |> InputJsonItem .Parse
130
+ File.ReadAllText( GlobalVars.inputFolder + @" /removedTypes.json" ) |> InputJsonType .Parse
131
131
132
132
let addedItems =
133
- File.ReadAllText( GlobalVars.inputFolder + @" /addedTypes.json" ) |> InputJsonItem .Parse
133
+ File.ReadAllText( GlobalVars.inputFolder + @" /addedTypes.json" ) |> InputJsonType .Parse
134
134
135
135
// This is the kind of items in the external json files that are used as a
136
136
// correction for the spec.
@@ -158,14 +158,14 @@ module InputJson =
158
158
| TypeDef _ -> " typedef"
159
159
| Extends _ -> " extends"
160
160
161
- let getItemByName ( allItems : InputJsonItem .Root []) ( itemName : string ) ( kind : ItemKind ) otherFilter =
162
- let filter ( item : InputJsonItem .Root) =
161
+ let getItemByName ( allItems : InputJsonType .Root []) ( itemName : string ) ( kind : ItemKind ) otherFilter =
162
+ let filter ( item : InputJsonType .Root) =
163
163
OptionCheckValue itemName item.Name &&
164
164
item.Kind.ToLower() = kind.ToString() &&
165
165
otherFilter item
166
166
allItems |> Array.tryFind filter
167
167
168
- let matchInterface iName ( item : InputJsonItem .Root) =
168
+ let matchInterface iName ( item : InputJsonType .Root) =
169
169
item.Interface.IsNone || item.Interface.Value = iName
170
170
171
171
let getOverriddenItemByName itemName ( kind : ItemKind ) iName =
@@ -177,7 +177,7 @@ module InputJson =
177
177
let getAddedItemByName itemName ( kind : ItemKind ) iName =
178
178
getItemByName addedItems itemName kind ( matchInterface iName)
179
179
180
- let getItems ( allItems : InputJsonItem .Root []) ( kind : ItemKind ) ( flavor : Flavor ) =
180
+ let getItems ( allItems : InputJsonType .Root []) ( kind : ItemKind ) ( flavor : Flavor ) =
181
181
allItems
182
182
|> Array.filter ( fun t ->
183
183
t.Kind.ToLower() = kind.ToString() &&
@@ -199,24 +199,33 @@ module InputJson =
199
199
getRemovedItems kind flavor |> Array.filter ( matchInterface iName)
200
200
201
201
module CommentJson =
202
- type CommentType = JsonProvider< " inputfiles/comments.json" >
202
+ type CommentJsonType = JsonProvider< " inputfiles/comments.json" , InferTypesFromValues = false >
203
203
204
- let comments = File.ReadAllText(__ SOURCE_ DIRECTORY__ + @" /inputfiles/comments.json" ) |> CommentType.Parse
204
+ let comments = File.ReadAllText( Path.Combine( GlobalVars.inputFolder, " comments.json" )) |> CommentJsonType.Parse
205
+
206
+ type InterfaceCommentItem = { Property: Map < string , string >; Method: Map < string , string >; Constructor: string option }
207
+
208
+ let commentMap =
209
+ comments.Interfaces
210
+ |> Array.map ( fun i ->
211
+ let propertyMap = i.Members.Property |> Array.map ( fun p -> ( p.Name, p.Comment)) |> Map.ofArray
212
+ let methodMap = i.Members.Method |> Array.map ( fun m -> ( m.Name, m.Comment)) |> Map.ofArray
213
+ ( i.Name, { Property = propertyMap; Method = methodMap; Constructor = i.Members.Constructor }))
214
+ |> Map.ofArray
205
215
206
216
let GetCommentForProperty iName pName =
207
- match comments.Interfaces |> Array.tryFind ( fun i -> i.Name = iName) with
208
- | Some i ->
209
- match i.Members.Property |> Array.tryFind ( fun p -> p.Name = pName) with
210
- | Some p -> Some p.Comment
211
- | _ -> None
217
+ match commentMap.TryFind iName with
218
+ | Some i -> i.Property.TryFind pName
212
219
| _ -> None
213
220
214
221
let GetCommentForMethod iName mName =
215
- match comments.Interfaces |> Array.tryFind ( fun i -> i.Name = iName) with
216
- | Some i ->
217
- match i.Members.Method |> Array.tryFind ( fun m -> m.Name = mName) with
218
- | Some m -> Some m.Comment
219
- | _ -> None
222
+ match commentMap.TryFind iName with
223
+ | Some i -> i.Method.TryFind mName
224
+ | _ -> None
225
+
226
+ let GetCommentForConstructor iName =
227
+ match commentMap.TryFind iName with
228
+ | Some i -> i.Constructor
220
229
| _ -> None
221
230
222
231
module Data =
@@ -232,7 +241,7 @@ module Data =
232
241
if isStatic.IsSome then scope = EmitScope.StaticOnly
233
242
else scope = EmitScope.InstanceOnly
234
243
235
- let matchInterface iName ( x : InputJson.InputJsonItem .Root ) =
244
+ let matchInterface iName ( x : InputJson.InputJsonType .Root ) =
236
245
x.Interface.IsNone || x.Interface.Value = iName
237
246
238
247
/// Parameter cannot be named "default" in JavaScript/Typescript so we need to rename it.
@@ -442,7 +451,7 @@ module Data =
442
451
443
452
let unUsedEvents =
444
453
GetNonCallbackInterfacesByFlavor Flavor.All
445
- |> Array.choose ( fun i ->
454
+ |> Array.choose ( fun i ->
446
455
if i.Extends = " Event" && i.Name.EndsWith( " Event" ) && not ( List.contains i.Name usedEvents) then Some( i.Name) else None)
447
456
|> Array.distinct
448
457
|> List.ofArray
@@ -621,7 +630,7 @@ module Data =
621
630
Nullable = isNullable } ]
622
631
623
632
/// Define the subset of events that dedicated workers will use
624
- let workerEventsMap =
633
+ let workerEventsMap =
625
634
[
626
635
( " close" , " CloseEvent" );
627
636
( " error" , " ErrorEvent" );
@@ -725,7 +734,7 @@ module Emit =
725
734
if nullable then makeNullable resolvedType else resolvedType
726
735
727
736
let EmitConstants ( i : Browser.Interface ) =
728
- let emitConstantFromJson ( c : InputJsonItem .Root) = Pt.Printl " readonly %s : %s ;" c.Name.Value c.Type.Value
737
+ let emitConstantFromJson ( c : InputJsonType .Root) = Pt.Printl " readonly %s : %s ;" c.Name.Value c.Type.Value
729
738
730
739
let emitConstant ( c : Browser.Constant ) =
731
740
if Option.isNone ( getRemovedItemByName c.Name ItemKind.Constant i.Name) then
@@ -827,7 +836,7 @@ module Emit =
827
836
Pt.Printl " "
828
837
829
838
let EmitCallBackFunctions flavor =
830
- let emitCallbackFunctionsFromJson ( cb : InputJson.InputJsonItem .Root ) =
839
+ let emitCallbackFunctionsFromJson ( cb : InputJson.InputJsonType .Root ) =
831
840
Pt.Printl " interface %s {" cb.Name.Value
832
841
cb.Signatures |> Array.iter ( Pt.PrintWithAddedIndent " %s ;" )
833
842
Pt.Printl " }"
@@ -859,7 +868,7 @@ module Emit =
859
868
| Some pollutor -> " this: " + pollutor.Name + " , "
860
869
| _ -> " "
861
870
let EmitProperties flavor prefix ( emitScope : EmitScope ) ( i : Browser.Interface )=
862
- let emitPropertyFromJson ( p : InputJsonItem .Root) =
871
+ let emitPropertyFromJson ( p : InputJsonType .Root) =
863
872
let readOnlyModifier =
864
873
match p.Readonly with
865
874
| Some( true ) -> " readonly "
@@ -917,7 +926,7 @@ module Emit =
917
926
// Note: two cases:
918
927
// 1. emit the members inside a interface -> no need to add prefix
919
928
// 2. emit the members outside to expose them (for "Window") -> need to add "declare"
920
- let emitMethodFromJson ( m : InputJsonItem .Root) =
929
+ let emitMethodFromJson ( m : InputJsonType .Root) =
921
930
m.Signatures |> Array.iter ( Pt.Printl " %s%s ;" prefix)
922
931
923
932
let emitCommentForMethod ( mName : string option ) =
@@ -1032,7 +1041,7 @@ module Emit =
1032
1041
fPrefix
1033
1042
1034
1043
let EmitConstructorSignature ( i : Browser.Interface ) =
1035
- let emitConstructorSigFromJson ( c : InputJsonItem .Root) =
1044
+ let emitConstructorSigFromJson ( c : InputJsonType .Root) =
1036
1045
c.Signatures |> Array.iter ( Pt.Printl " %s ;" )
1037
1046
1038
1047
let removedCtor = getRemovedItems ItemKind.Constructor Flavor.All |> Array.tryFind ( matchInterface i.Name)
@@ -1141,7 +1150,7 @@ module Emit =
1141
1150
false
1142
1151
1143
1152
let EmitIndexers emitScope ( i : Browser.Interface ) =
1144
- let emitIndexerFromJson ( id : InputJsonItem .Root) =
1153
+ let emitIndexerFromJson ( id : InputJsonType .Root) =
1145
1154
id.Signatures |> Array.iter ( Pt.Printl " %s ;" )
1146
1155
1147
1156
let removedIndexer = getRemovedItems ItemKind.Indexer Flavor.All |> Array.tryFind ( matchInterface i.Name)
@@ -1185,6 +1194,7 @@ module Emit =
1185
1194
Pt.DecreaseIndent()
1186
1195
Pt.Printl " }"
1187
1196
Pt.Printl " "
1197
+
1188
1198
let EmitInterface flavor ( i : Browser.Interface ) =
1189
1199
EmitInterfaceEventMap flavor i
1190
1200
@@ -1298,7 +1308,7 @@ module Emit =
1298
1308
| " Object" -> Pt.Printl " interface %s {" dict.Name
1299
1309
| _ -> Pt.Printl " interface %s extends %s {" dict.Name dict.Extends
1300
1310
1301
- let emitJsonProperty ( p : InputJsonItem .Root) =
1311
+ let emitJsonProperty ( p : InputJsonType .Root) =
1302
1312
Pt.Printl " %s : %s ;" p.Name.Value p.Type.Value
1303
1313
1304
1314
let removedPropNames =
@@ -1325,39 +1335,53 @@ module Emit =
1325
1335
|> Array.filter ( fun dict -> flavor <> Worker || knownWorkerInterfaces.Contains dict.Name)
1326
1336
|> Array.iter emitDictionary
1327
1337
1328
- let EmitAddedInterface ( ai : InputJson.InputJsonItem .Root) =
1338
+ let EmitAddedInterface ( ai : InputJsonType .Root) =
1329
1339
match ai.Extends with
1330
1340
| Some e -> Pt.Printl " interface %s extends %s {" ai.Name.Value ai.Extends.Value
1331
1341
| None -> Pt.Printl " interface %s {" ai.Name.Value
1332
1342
1333
- for p in ai.Properties do
1343
+ let emitProperty ( p : InputJsonType.Property ) =
1334
1344
let readOnlyModifier =
1335
1345
match p.Readonly with
1336
1346
| Some( true ) -> " readonly "
1337
1347
| _ -> " "
1348
+ match CommentJson.GetCommentForProperty ai.Name.Value p.Name with
1349
+ | Some comment -> Pt.PrintWithAddedIndent " %s " comment
1350
+ | _ -> ()
1338
1351
Pt.PrintWithAddedIndent " %s%s : %s ;" readOnlyModifier p.Name p.Type
1339
1352
1340
- ai.Methods |> Array.collect ( fun m -> m.Signatures) |> Array.iter ( Pt.PrintWithAddedIndent " %s ;" )
1353
+ let emitMethod ( m : InputJsonType.Method ) =
1354
+ match CommentJson.GetCommentForMethod ai.Name.Value m.Name with
1355
+ | Some comment -> Pt.PrintWithAddedIndent " %s " comment
1356
+ | _ -> ()
1357
+ m.Signatures |> Array.iter ( Pt.PrintWithAddedIndent " %s ;" )
1358
+
1359
+
1360
+ ai.Properties |> Array.iter emitProperty
1361
+ ai.Methods |> Array.iter emitMethod
1341
1362
ai.Indexer |> Array.collect ( fun i -> i.Signatures) |> Array.iter ( Pt.PrintWithAddedIndent " %s ;" )
1342
1363
Pt.Printl " }"
1343
1364
Pt.Printl " "
1344
1365
1345
1366
if ai.ConstructorSignatures.Length > 0 then
1346
1367
Pt.Printl " declare var %s : {" ai.Name.Value
1347
1368
Pt.PrintWithAddedIndent " prototype: %s ;" ai.Name.Value
1369
+ match CommentJson.GetCommentForConstructor ai.Name.Value with
1370
+ | Some comment -> Pt.PrintWithAddedIndent " %s " comment
1371
+ | _ -> ()
1348
1372
ai.ConstructorSignatures |> Array.iter ( Pt.PrintWithAddedIndent " %s ;" )
1349
1373
Pt.Printl " }"
1350
1374
Pt.Printl " "
1351
1375
1352
1376
let EmitTypeDefs flavor =
1353
1377
let emitTypeDef ( typeDef : Browser.Typedef ) =
1354
1378
Pt.Printl " type %s = %s ;" typeDef.NewType ( DomTypeToTsType typeDef.Type)
1355
- let emitTypeDefFromJson ( typeDef : InputJsonItem .Root) =
1379
+ let emitTypeDefFromJson ( typeDef : InputJsonType .Root) =
1356
1380
Pt.Printl " type %s = %s ;" typeDef.Name.Value typeDef.Type.Value
1357
1381
1358
1382
match flavor with
1359
1383
| Flavor.Worker ->
1360
- browser.Typedefs
1384
+ browser.Typedefs
1361
1385
|> Array.filter ( fun typedef -> knownWorkerInterfaces.Contains typedef.NewType)
1362
1386
|> Array.iter emitTypeDef
1363
1387
| _ ->
0 commit comments