Skip to content

Commit

Permalink
Merge pull request #235 from SaschaNaz/implicit-iterable
Browse files Browse the repository at this point in the history
Support implicit iterable interfaces
  • Loading branch information
mhegazy authored May 12, 2017
2 parents c5acf04 + a724ac8 commit 4e3b084
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 9 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.es6.generated.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 "/// Worker APIs"
| _ -> Pt.Printl "/// 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>" (DomTypeToTsType iterableGetter.Value.Type)
Pt.DecreaseIndent()
Pt.Printl "}"
Pt.Printl ""

let EmitES6Thing (target: TextWriter) =
Pt.Reset()
Pt.Printl "/////////////////////////////"
Pt.Printl "/// 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
104 changes: 104 additions & 0 deletions baselines/dom.es6.generated.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@

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

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

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

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

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

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

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

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<string>
}

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<string>
}

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>
}
4 changes: 2 additions & 2 deletions baselines/dom.generated.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

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

interface Account {
Expand Down Expand Up @@ -15036,4 +15036,4 @@ type ServiceWorkerState = "installing" | "installed" | "activating" | "activated
type Transport = "usb" | "nfc" | "ble";
type VideoFacingModeEnum = "user" | "environment" | "left" | "right";
type VisibilityState = "hidden" | "visible" | "prerender" | "unloaded";
type XMLHttpRequestResponseType = "" | "arraybuffer" | "blob" | "document" | "json" | "text";
type XMLHttpRequestResponseType = "" | "arraybuffer" | "blob" | "document" | "json" | "text";
4 changes: 2 additions & 2 deletions baselines/webworker.generated.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

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

interface Algorithm {
Expand Down Expand Up @@ -1840,4 +1840,4 @@ type ServiceWorkerState = "installing" | "installed" | "activating" | "activated
type VisibilityState = "hidden" | "visible" | "prerender" | "unloaded";
type XMLHttpRequestResponseType = "" | "arraybuffer" | "blob" | "document" | "json" | "text";
type ClientType = "window" | "worker" | "sharedworker" | "all";
type FrameType = "auxiliary" | "top-level" | "nested" | "none";
type FrameType = "auxiliary" | "top-level" | "nested" | "none";

0 comments on commit 4e3b084

Please sign in to comment.