Skip to content

Commit

Permalink
support implicit iterable interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
saschanaz committed Apr 21, 2017
1 parent 0e4ebf4 commit bdbcbfe
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 7 deletions.
57 changes: 52 additions & 5 deletions TS.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module GlobalVars =

let makeTextWriter fileName = File.CreateText(Path.Combine(outputFolder, fileName)) :> TextWriter
let tsWebOutput = makeTextWriter "dom.generated.d.ts"
let tsWebES6Output = makeTextWriter "dom.generated.es6.d.ts"
let tsWorkerOutput = makeTextWriter "webworker.generated.d.ts"
let defaultEventType = "Event"

Expand Down Expand Up @@ -698,6 +699,9 @@ module Emit =
let extendedTypes =
["ArrayBuffer";"ArrayBufferView";"Int8Array";"Uint8Array";"Int16Array";"Uint16Array";"Int32Array";"Uint32Array";"Float32Array";"Float64Array"]

let integerTypes =
["byte";"octet";"short";"unsigned short";"long";"unsigned long";"long long";"unsigned long long"]

/// Get typescript type using object dom type, object name, and it's associated interface name
let rec DomTypeToTsType (objDomType: string) =
match objDomType.Trim('?') with
Expand All @@ -713,15 +717,13 @@ module Emit =
| "EventListener" -> "EventListenerOrEventListenerObject"
| "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"
| "sequence" -> "Array"
| "short" | "signed short" | "unsigned short" -> "number"
| "UnrestrictedDouble" -> "number"
| "void" -> "void"
| integerType when List.contains integerType integerTypes -> "number"
| extendedType when List.contains extendedType extendedTypes -> extendedType
| _ ->
if ignoreDOMTypes && Seq.contains objDomType ["Element"; "Window"; "Document"] then "any"
Expand Down Expand Up @@ -1466,8 +1468,8 @@ module Emit =
Pt.Reset()
Pt.Printl "/////////////////////////////"
match flavor with
| Worker -> Pt.Printl "/// IE Worker APIs"
| _ -> Pt.Printl "/// IE DOM APIs"
| Worker -> Pt.Printl "/// MSEdge Worker APIs"
| _ -> Pt.Printl "/// MSEdge DOM APIs"
Pt.Printl "/////////////////////////////"
Pt.Printl ""

Expand Down Expand Up @@ -1502,8 +1504,53 @@ module Emit =
target.Flush()
target.Close()

let EmitIterator (i: Browser.Interface) =
let isIntegerKeyParam (p: Browser.Param) =
List.contains p.Type integerTypes

// check anonymous unsigned long getter and length property
let isIterableGetter (m: Browser.Method) =
m.Getter = Some 1 && m.Params.Length = 1 && isIntegerKeyParam m.Params.[0]

let findIterableGetter() =
let anonymousGetter =
if (i.AnonymousMethods.IsSome) then Array.tryFind isIterableGetter i.AnonymousMethods.Value.Methods
else None

if (anonymousGetter.IsSome) then anonymousGetter
else if (i.Methods.IsSome) then Array.tryFind isIterableGetter i.Methods.Value.Methods
else None

let findLengthProperty (p: Browser.Property) =
p.Name = "length" && List.contains p.Type integerTypes

if i.Name <> "Window" && i.Properties.IsSome then
let iterableGetter = findIterableGetter()
let lengthProperty = Array.tryFind findLengthProperty i.Properties.Value.Properties
if iterableGetter.IsSome && lengthProperty.IsSome then
Pt.Printl "interface %s {" i.Name
Pt.IncreaseIndent()
Pt.Printl "[Symbol.iterator](): IterableIterator<%s>" iterableGetter.Value.Type
Pt.DecreaseIndent()
Pt.Printl "}"
Pt.Printl ""

let EmitES6Thing (target: TextWriter) =
Pt.Reset()
Pt.Printl "/////////////////////////////"
Pt.Printl "/// MSEdge DOM ES6 APIs"
Pt.Printl "/////////////////////////////"
Pt.Printl ""

browser.Interfaces |> Array.iter EmitIterator

fprintf target "%s" (Pt.GetResult())
target.Flush()
target.Close()

let EmitDomWeb () =
EmitTheWholeThing Flavor.All GlobalVars.tsWebOutput
EmitES6Thing GlobalVars.tsWebES6Output

let EmitDomWorker () =
ignoreDOMTypes <- true
Expand Down
2 changes: 1 addition & 1 deletion baselines/dom.generated.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

/////////////////////////////
/// IE DOM APIs
/// MSEdge DOM APIs
/////////////////////////////

interface Account {
Expand Down
104 changes: 104 additions & 0 deletions baselines/dom.generated.es6.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@

/////////////////////////////
/// MSEdge DOM ES6 APIs
/////////////////////////////

interface AudioTrackList {
[Symbol.iterator](): IterableIterator<AudioTrack>
}

interface CSSRuleList {
[Symbol.iterator](): IterableIterator<CSSRule>
}

interface CSSStyleDeclaration {
[Symbol.iterator](): IterableIterator<DOMString>
}

interface ClientRectList {
[Symbol.iterator](): IterableIterator<ClientRect>
}

interface DOMStringList {
[Symbol.iterator](): IterableIterator<DOMString>
}

interface DOMTokenList {
[Symbol.iterator](): IterableIterator<DOMString>
}

interface DataTransferItemList {
[Symbol.iterator](): IterableIterator<File>
}

interface FileList {
[Symbol.iterator](): IterableIterator<File>
}

interface HTMLAllCollection {
[Symbol.iterator](): IterableIterator<Element>
}

interface HTMLCollection {
[Symbol.iterator](): IterableIterator<Element>
}

interface MSRangeCollection {
[Symbol.iterator](): IterableIterator<Range>
}

interface MediaList {
[Symbol.iterator](): IterableIterator<DOMString>
}

interface MimeTypeArray {
[Symbol.iterator](): IterableIterator<Plugin>
}

interface NamedNodeMap {
[Symbol.iterator](): IterableIterator<Attr>
}

interface NodeList {
[Symbol.iterator](): IterableIterator<Node>
}

interface Plugin {
[Symbol.iterator](): IterableIterator<MimeType>
}

interface PluginArray {
[Symbol.iterator](): IterableIterator<Plugin>
}

interface SourceBufferList {
[Symbol.iterator](): IterableIterator<SourceBuffer>
}

interface Storage {
[Symbol.iterator](): IterableIterator<DOMString>
}

interface StyleSheetList {
[Symbol.iterator](): IterableIterator<StyleSheet>
}

interface StyleSheetPageList {
[Symbol.iterator](): IterableIterator<CSSPageRule>
}

interface TextTrackCueList {
[Symbol.iterator](): IterableIterator<TextTrackCue>
}

interface TextTrackList {
[Symbol.iterator](): IterableIterator<TextTrack>
}

interface TouchList {
[Symbol.iterator](): IterableIterator<Touch>
}

interface VideoTrackList {
[Symbol.iterator](): IterableIterator<VideoTrack>
}
2 changes: 1 addition & 1 deletion baselines/webworker.generated.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

/////////////////////////////
/// IE Worker APIs
/// MSEdge Worker APIs
/////////////////////////////

interface Algorithm {
Expand Down

0 comments on commit bdbcbfe

Please sign in to comment.