Skip to content

Commit fdc6018

Browse files
committed
Add comments support for added interface
1 parent cd9d627 commit fdc6018

File tree

3 files changed

+113
-35
lines changed

3 files changed

+113
-35
lines changed

TS.fsx

Lines changed: 59 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,16 @@ module InputJson =
121121
open Helpers
122122
open Types
123123

124-
type InputJsonItem = JsonProvider<"inputfiles/sample.json">
124+
type InputJsonType = JsonProvider<"inputfiles/sample.json">
125125

126126
let overriddenItems =
127-
File.ReadAllText(GlobalVars.inputFolder + @"/overridingTypes.json") |> InputJsonItem.Parse
127+
File.ReadAllText(GlobalVars.inputFolder + @"/overridingTypes.json") |> InputJsonType.Parse
128128

129129
let removedItems =
130-
File.ReadAllText(GlobalVars.inputFolder + @"/removedTypes.json") |> InputJsonItem.Parse
130+
File.ReadAllText(GlobalVars.inputFolder + @"/removedTypes.json") |> InputJsonType.Parse
131131

132132
let addedItems =
133-
File.ReadAllText(GlobalVars.inputFolder + @"/addedTypes.json") |> InputJsonItem.Parse
133+
File.ReadAllText(GlobalVars.inputFolder + @"/addedTypes.json") |> InputJsonType.Parse
134134

135135
// This is the kind of items in the external json files that are used as a
136136
// correction for the spec.
@@ -158,14 +158,14 @@ module InputJson =
158158
| TypeDef _ -> "typedef"
159159
| Extends _ -> "extends"
160160

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) =
163163
OptionCheckValue itemName item.Name &&
164164
item.Kind.ToLower() = kind.ToString() &&
165165
otherFilter item
166166
allItems |> Array.tryFind filter
167167

168-
let matchInterface iName (item: InputJsonItem.Root) =
168+
let matchInterface iName (item: InputJsonType.Root) =
169169
item.Interface.IsNone || item.Interface.Value = iName
170170

171171
let getOverriddenItemByName itemName (kind: ItemKind) iName =
@@ -177,7 +177,7 @@ module InputJson =
177177
let getAddedItemByName itemName (kind: ItemKind) iName =
178178
getItemByName addedItems itemName kind (matchInterface iName)
179179

180-
let getItems (allItems: InputJsonItem.Root []) (kind: ItemKind) (flavor: Flavor) =
180+
let getItems (allItems: InputJsonType.Root []) (kind: ItemKind) (flavor: Flavor) =
181181
allItems
182182
|> Array.filter (fun t ->
183183
t.Kind.ToLower() = kind.ToString() &&
@@ -199,24 +199,33 @@ module InputJson =
199199
getRemovedItems kind flavor |> Array.filter (matchInterface iName)
200200

201201
module CommentJson =
202-
type CommentType = JsonProvider<"inputfiles/comments.json">
202+
type CommentJsonType = JsonProvider<"inputfiles/comments.json", InferTypesFromValues=false>
203203

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
205215

206216
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
212219
| _ -> None
213220

214221
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
220229
| _ -> None
221230

222231
module Data =
@@ -232,7 +241,7 @@ module Data =
232241
if isStatic.IsSome then scope = EmitScope.StaticOnly
233242
else scope = EmitScope.InstanceOnly
234243

235-
let matchInterface iName (x: InputJson.InputJsonItem.Root) =
244+
let matchInterface iName (x: InputJson.InputJsonType.Root) =
236245
x.Interface.IsNone || x.Interface.Value = iName
237246

238247
/// Parameter cannot be named "default" in JavaScript/Typescript so we need to rename it.
@@ -442,7 +451,7 @@ module Data =
442451

443452
let unUsedEvents =
444453
GetNonCallbackInterfacesByFlavor Flavor.All
445-
|> Array.choose (fun i ->
454+
|> Array.choose (fun i ->
446455
if i.Extends = "Event" && i.Name.EndsWith("Event") && not (List.contains i.Name usedEvents) then Some(i.Name) else None)
447456
|> Array.distinct
448457
|> List.ofArray
@@ -621,7 +630,7 @@ module Data =
621630
Nullable = isNullable } ]
622631

623632
/// Define the subset of events that dedicated workers will use
624-
let workerEventsMap =
633+
let workerEventsMap =
625634
[
626635
("close", "CloseEvent");
627636
("error", "ErrorEvent");
@@ -725,7 +734,7 @@ module Emit =
725734
if nullable then makeNullable resolvedType else resolvedType
726735

727736
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
729738

730739
let emitConstant (c: Browser.Constant) =
731740
if Option.isNone (getRemovedItemByName c.Name ItemKind.Constant i.Name) then
@@ -827,7 +836,7 @@ module Emit =
827836
Pt.Printl ""
828837

829838
let EmitCallBackFunctions flavor =
830-
let emitCallbackFunctionsFromJson (cb: InputJson.InputJsonItem.Root) =
839+
let emitCallbackFunctionsFromJson (cb: InputJson.InputJsonType.Root) =
831840
Pt.Printl "interface %s {" cb.Name.Value
832841
cb.Signatures |> Array.iter (Pt.PrintWithAddedIndent "%s;")
833842
Pt.Printl "}"
@@ -859,7 +868,7 @@ module Emit =
859868
| Some pollutor -> "this: " + pollutor.Name + ", "
860869
| _ -> ""
861870
let EmitProperties flavor prefix (emitScope: EmitScope) (i: Browser.Interface)=
862-
let emitPropertyFromJson (p: InputJsonItem.Root) =
871+
let emitPropertyFromJson (p: InputJsonType.Root) =
863872
let readOnlyModifier =
864873
match p.Readonly with
865874
| Some(true) -> "readonly "
@@ -917,7 +926,7 @@ module Emit =
917926
// Note: two cases:
918927
// 1. emit the members inside a interface -> no need to add prefix
919928
// 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) =
921930
m.Signatures |> Array.iter (Pt.Printl "%s%s;" prefix)
922931

923932
let emitCommentForMethod (mName: string option) =
@@ -1032,7 +1041,7 @@ module Emit =
10321041
fPrefix
10331042

10341043
let EmitConstructorSignature (i:Browser.Interface) =
1035-
let emitConstructorSigFromJson (c: InputJsonItem.Root) =
1044+
let emitConstructorSigFromJson (c: InputJsonType.Root) =
10361045
c.Signatures |> Array.iter (Pt.Printl "%s;")
10371046

10381047
let removedCtor = getRemovedItems ItemKind.Constructor Flavor.All |> Array.tryFind (matchInterface i.Name)
@@ -1141,7 +1150,7 @@ module Emit =
11411150
false
11421151

11431152
let EmitIndexers emitScope (i: Browser.Interface) =
1144-
let emitIndexerFromJson (id: InputJsonItem.Root) =
1153+
let emitIndexerFromJson (id: InputJsonType.Root) =
11451154
id.Signatures |> Array.iter (Pt.Printl "%s;")
11461155

11471156
let removedIndexer = getRemovedItems ItemKind.Indexer Flavor.All |> Array.tryFind (matchInterface i.Name)
@@ -1185,6 +1194,7 @@ module Emit =
11851194
Pt.DecreaseIndent()
11861195
Pt.Printl "}"
11871196
Pt.Printl ""
1197+
11881198
let EmitInterface flavor (i:Browser.Interface) =
11891199
EmitInterfaceEventMap flavor i
11901200

@@ -1298,7 +1308,7 @@ module Emit =
12981308
| "Object" -> Pt.Printl "interface %s {" dict.Name
12991309
| _ -> Pt.Printl "interface %s extends %s {" dict.Name dict.Extends
13001310

1301-
let emitJsonProperty (p: InputJsonItem.Root) =
1311+
let emitJsonProperty (p: InputJsonType.Root) =
13021312
Pt.Printl "%s: %s;" p.Name.Value p.Type.Value
13031313

13041314
let removedPropNames =
@@ -1325,39 +1335,53 @@ module Emit =
13251335
|> Array.filter (fun dict -> flavor <> Worker || knownWorkerInterfaces.Contains dict.Name)
13261336
|> Array.iter emitDictionary
13271337

1328-
let EmitAddedInterface (ai: InputJson.InputJsonItem.Root) =
1338+
let EmitAddedInterface (ai: InputJsonType.Root) =
13291339
match ai.Extends with
13301340
| Some e -> Pt.Printl "interface %s extends %s {" ai.Name.Value ai.Extends.Value
13311341
| None -> Pt.Printl "interface %s {" ai.Name.Value
13321342

1333-
for p in ai.Properties do
1343+
let emitProperty (p: InputJsonType.Property) =
13341344
let readOnlyModifier =
13351345
match p.Readonly with
13361346
| Some(true) -> "readonly "
13371347
| _ -> ""
1348+
match CommentJson.GetCommentForProperty ai.Name.Value p.Name with
1349+
| Some comment -> Pt.PrintWithAddedIndent "%s" comment
1350+
| _ -> ()
13381351
Pt.PrintWithAddedIndent "%s%s: %s;" readOnlyModifier p.Name p.Type
13391352

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
13411362
ai.Indexer |> Array.collect (fun i -> i.Signatures) |> Array.iter (Pt.PrintWithAddedIndent "%s;")
13421363
Pt.Printl "}"
13431364
Pt.Printl ""
13441365

13451366
if ai.ConstructorSignatures.Length > 0 then
13461367
Pt.Printl "declare var %s: {" ai.Name.Value
13471368
Pt.PrintWithAddedIndent "prototype: %s;" ai.Name.Value
1369+
match CommentJson.GetCommentForConstructor ai.Name.Value with
1370+
| Some comment -> Pt.PrintWithAddedIndent "%s" comment
1371+
| _ -> ()
13481372
ai.ConstructorSignatures |> Array.iter (Pt.PrintWithAddedIndent "%s;")
13491373
Pt.Printl "}"
13501374
Pt.Printl ""
13511375

13521376
let EmitTypeDefs flavor =
13531377
let emitTypeDef (typeDef: Browser.Typedef) =
13541378
Pt.Printl "type %s = %s;" typeDef.NewType (DomTypeToTsType typeDef.Type)
1355-
let emitTypeDefFromJson (typeDef: InputJsonItem.Root) =
1379+
let emitTypeDefFromJson (typeDef: InputJsonType.Root) =
13561380
Pt.Printl "type %s = %s;" typeDef.Name.Value typeDef.Type.Value
13571381

13581382
match flavor with
13591383
| Flavor.Worker ->
1360-
browser.Typedefs
1384+
browser.Typedefs
13611385
|> Array.filter (fun typedef -> knownWorkerInterfaces.Contains typedef.NewType)
13621386
|> Array.iter emitTypeDef
13631387
| _ ->

baselines/dom.generated.d.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12732,16 +12732,37 @@ interface Canvas2DContextAttributes {
1273212732
}
1273312733

1273412734
interface URLSearchParams {
12735+
/**
12736+
* Appends a specified key/value pair as a new search parameter.
12737+
*/
1273512738
append(name: string, value: string): void;
12739+
/**
12740+
* Deletes the given search parameter, and its associated value, from the list of all search parameters.
12741+
*/
1273612742
delete(name: string): void;
12743+
/**
12744+
* Returns the first value associated to the given search parameter.
12745+
*/
1273712746
get(name: string): string | null;
12747+
/**
12748+
* Returns all the values association with a given search parameter.
12749+
*/
1273812750
getAll(name: string): string[];
12751+
/**
12752+
* Returns a Boolean indicating if such a search parameter exists.
12753+
*/
1273912754
has(name: string): boolean;
12755+
/**
12756+
* Sets the value associated to a given search parameter to the given value. If there were several values, delete the others.
12757+
*/
1274012758
set(name: string, value: string): void;
1274112759
}
1274212760

1274312761
declare var URLSearchParams: {
1274412762
prototype: URLSearchParams;
12763+
/**
12764+
* Constructor returning a URLSearchParams object.
12765+
*/
1274512766
new (init?: string | URLSearchParams): URLSearchParams;
1274612767
}
1274712768

inputfiles/comments.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3043,6 +3043,39 @@
30433043
],
30443044
"method": []
30453045
}
3046+
},
3047+
{
3048+
"name": "URLSearchParams",
3049+
"members": {
3050+
"method": [
3051+
{
3052+
"name": "append",
3053+
"comment": "/**\r\n * Appends a specified key/value pair as a new search parameter.\r\n */"
3054+
},
3055+
{
3056+
"name": "delete",
3057+
"comment": "/**\r\n * Deletes the given search parameter, and its associated value, from the list of all search parameters.\r\n */"
3058+
},
3059+
{
3060+
"name": "get",
3061+
"comment": "/**\r\n * Returns the first value associated to the given search parameter.\r\n */"
3062+
},
3063+
{
3064+
"name": "getAll",
3065+
"comment": "/**\r\n * Returns all the values association with a given search parameter.\r\n */"
3066+
},
3067+
{
3068+
"name": "has",
3069+
"comment": "/**\r\n * Returns a Boolean indicating if such a search parameter exists.\r\n */"
3070+
},
3071+
{
3072+
"name": "set",
3073+
"comment": "/**\r\n * Sets the value associated to a given search parameter to the given value. If there were several values, delete the others.\r\n */"
3074+
}
3075+
],
3076+
"property": [],
3077+
"constructor": "/**\r\n * Constructor returning a URLSearchParams object.\r\n */"
3078+
}
30463079
}
30473080
]
30483081
}

0 commit comments

Comments
 (0)