Skip to content

Update the XML spec #72

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 3, 2016
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Shared.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,15 @@ module JsonItems =
// This is the kind of items in the external json files that are used as a
// correction for the spec.
type ItemKind =
Property | Method | Constant | Constructor | Interface | Callback | Indexer | SignatureOverload
| Property
| Method
| Constant
| Constructor
| Interface
| Callback
| Indexer
| SignatureOverload
| TypeDef
override x.ToString() = (unionToString x).ToLower()

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

let typeDefSet =
browser.Typedefs |> Array.map (fun td -> td.NewType) |> Set.ofArray

module Option =
let runIfSome f x =
match x with
Expand Down
30 changes: 25 additions & 5 deletions TS.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ open Shared
open Shared.Comments
open Shared.JsonItems
open System.IO
open System.Web

// Global print target
let Pt = StringPrinter()
Expand Down Expand Up @@ -33,6 +34,7 @@ let rec DomTypeToTsType (objDomType: string) =
| "double" | "float" -> "number"
| "Function" -> "Function"
| "long" | "long long" | "signed long" | "signed long long" | "unsigned long" | "unsigned long long" -> "number"
| "octet" | "byte" -> "number"
| "object" -> "any"
| "Promise" -> "Promise"
| "ReadyState" -> "string"
Expand All @@ -49,22 +51,26 @@ let rec DomTypeToTsType (objDomType: string) =
allCallbackFuncs.ContainsKey objDomType ||
allDictionariesMap.ContainsKey objDomType then
objDomType
// Name of a type alias. Just return itself
elif typeDefSet.Contains objDomType then objDomType
// Enum types are all treated as string
elif allEnumsMap.ContainsKey objDomType then "string"
// Union types
elif (objDomType.Contains(" or ")) then
elif objDomType.Contains(" or ") then
let allTypes = objDomType.Trim('(', ')').Split([|" or "|], StringSplitOptions.None)
|> Array.map DomTypeToTsType
|> Array.map (fun t -> DomTypeToTsType (t.Trim('?', ' ')))
if Seq.contains "any" allTypes then "any" else String.concat " | " allTypes
else
// Check if is array type, which looks like "sequence<DOMString>"
let genericMatch = Regex.Match(objDomType, @"^(\w+)<(\w+)>$")
let unescaped = System.Web.HttpUtility.HtmlDecode(objDomType)
let genericMatch = Regex.Match(unescaped, @"^(\w+)<(\w+)>$")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about multiple type arguments? What about nested type arguments?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The nested case should be taken care of, as the implementation is recursive. In line 69 the paramName is obtained via another run of DomTypeToTsType using the matched part, so even if it has a type argument, it will be recognized.

As for the case with multiple type arguments, the logic would become much more complex, because it is non-trivial to tell whether ',' delimits two types or is just inside a type argument. Up to now I haven't seen the case appeared yet in the spec, it might be worth another PR to address the issue.

if genericMatch.Success then
let tName = DomTypeToTsType (genericMatch.Groups.[1].Value)
let paramName = DomTypeToTsType (genericMatch.Groups.[2].Value)
match tName with
| "Promise" -> "any"
| "Promise" ->
"PromiseLike<" + paramName + ">"
| _ ->
let paramName = DomTypeToTsType (genericMatch.Groups.[2].Value)
if tName = "Array" then paramName + "[]"
else tName + "<" + paramName + ">"
elif objDomType.EndsWith("[]") then
Expand Down Expand Up @@ -605,6 +611,18 @@ let EmitAddedInterface (ai: JsonItems.ItemsType.Root) =
Pt.printl "}"
Pt.printl ""

let EmitTypeDefs flavor =
let EmitTypeDef (typeDef: Browser.Typedef) =
Pt.printl "type %s = %s;" typeDef.NewType (DomTypeToTsType typeDef.Type)
let EmitTypeDefFromJson (typeDef: ItemsType.Root) =
Pt.printl "type %s = %s;" typeDef.Name.Value typeDef.Type.Value

if flavor <> Flavor.Worker then
browser.Typedefs |> Array.iter EmitTypeDef

JsonItems.getAddedItems ItemKind.TypeDef flavor
|> Array.iter EmitTypeDefFromJson

let EmitTheWholeThing flavor (target:TextWriter) =
Pt.reset()
Pt.printl "/////////////////////////////"
Expand Down Expand Up @@ -635,6 +653,8 @@ let EmitTheWholeThing flavor (target:TextWriter) =
EmitEventHandlers "declare var " gp
| _ -> ()

EmitTypeDefs flavor

fprintf target "%s" (Pt.getResult())
target.Flush()
target.Close()
Expand Down
Loading