Skip to content

Commit baf84a4

Browse files
committed
Merge pull request #72 from Microsoft/updateXML
Update the XML spec
2 parents 6d23139 + ca0acc5 commit baf84a4

9 files changed

+6150
-3712
lines changed

Shared.fsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,15 @@ module JsonItems =
6060
// This is the kind of items in the external json files that are used as a
6161
// correction for the spec.
6262
type ItemKind =
63-
Property | Method | Constant | Constructor | Interface | Callback | Indexer | SignatureOverload
63+
| Property
64+
| Method
65+
| Constant
66+
| Constructor
67+
| Interface
68+
| Callback
69+
| Indexer
70+
| SignatureOverload
71+
| TypeDef
6472
override x.ToString() = (unionToString x).ToLower()
6573

6674
let findItem (allItems: ItemsType.Root []) (itemName: string) (kind: ItemKind) otherFilter =
@@ -600,6 +608,9 @@ let workerEventsMap =
600608
("progress", "ProgressEvent") ]
601609
|> Map.ofList
602610

611+
let typeDefSet =
612+
browser.Typedefs |> Array.map (fun td -> td.NewType) |> Set.ofArray
613+
603614
module Option =
604615
let runIfSome f x =
605616
match x with

TS.fsx

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ open Shared
66
open Shared.Comments
77
open Shared.JsonItems
88
open System.IO
9+
open System.Web
910

1011
// Global print target
1112
let Pt = StringPrinter()
@@ -33,6 +34,7 @@ let rec DomTypeToTsType (objDomType: string) =
3334
| "double" | "float" -> "number"
3435
| "Function" -> "Function"
3536
| "long" | "long long" | "signed long" | "signed long long" | "unsigned long" | "unsigned long long" -> "number"
37+
| "octet" | "byte" -> "number"
3638
| "object" -> "any"
3739
| "Promise" -> "Promise"
3840
| "ReadyState" -> "string"
@@ -49,22 +51,26 @@ let rec DomTypeToTsType (objDomType: string) =
4951
allCallbackFuncs.ContainsKey objDomType ||
5052
allDictionariesMap.ContainsKey objDomType then
5153
objDomType
54+
// Name of a type alias. Just return itself
55+
elif typeDefSet.Contains objDomType then objDomType
5256
// Enum types are all treated as string
5357
elif allEnumsMap.ContainsKey objDomType then "string"
5458
// Union types
55-
elif (objDomType.Contains(" or ")) then
59+
elif objDomType.Contains(" or ") then
5660
let allTypes = objDomType.Trim('(', ')').Split([|" or "|], StringSplitOptions.None)
57-
|> Array.map DomTypeToTsType
61+
|> Array.map (fun t -> DomTypeToTsType (t.Trim('?', ' ')))
5862
if Seq.contains "any" allTypes then "any" else String.concat " | " allTypes
5963
else
6064
// Check if is array type, which looks like "sequence<DOMString>"
61-
let genericMatch = Regex.Match(objDomType, @"^(\w+)<(\w+)>$")
65+
let unescaped = System.Web.HttpUtility.HtmlDecode(objDomType)
66+
let genericMatch = Regex.Match(unescaped, @"^(\w+)<(\w+)>$")
6267
if genericMatch.Success then
6368
let tName = DomTypeToTsType (genericMatch.Groups.[1].Value)
69+
let paramName = DomTypeToTsType (genericMatch.Groups.[2].Value)
6470
match tName with
65-
| "Promise" -> "any"
71+
| "Promise" ->
72+
"PromiseLike<" + paramName + ">"
6673
| _ ->
67-
let paramName = DomTypeToTsType (genericMatch.Groups.[2].Value)
6874
if tName = "Array" then paramName + "[]"
6975
else tName + "<" + paramName + ">"
7076
elif objDomType.EndsWith("[]") then
@@ -605,6 +611,18 @@ let EmitAddedInterface (ai: JsonItems.ItemsType.Root) =
605611
Pt.printl "}"
606612
Pt.printl ""
607613

614+
let EmitTypeDefs flavor =
615+
let EmitTypeDef (typeDef: Browser.Typedef) =
616+
Pt.printl "type %s = %s;" typeDef.NewType (DomTypeToTsType typeDef.Type)
617+
let EmitTypeDefFromJson (typeDef: ItemsType.Root) =
618+
Pt.printl "type %s = %s;" typeDef.Name.Value typeDef.Type.Value
619+
620+
if flavor <> Flavor.Worker then
621+
browser.Typedefs |> Array.iter EmitTypeDef
622+
623+
JsonItems.getAddedItems ItemKind.TypeDef flavor
624+
|> Array.iter EmitTypeDefFromJson
625+
608626
let EmitTheWholeThing flavor (target:TextWriter) =
609627
Pt.reset()
610628
Pt.printl "/////////////////////////////"
@@ -635,6 +653,8 @@ let EmitTheWholeThing flavor (target:TextWriter) =
635653
EmitEventHandlers "declare var " gp
636654
| _ -> ()
637655

656+
EmitTypeDefs flavor
657+
638658
fprintf target "%s" (Pt.getResult())
639659
target.Flush()
640660
target.Close()

0 commit comments

Comments
 (0)