Skip to content
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

Support implicit iterable interfaces #235

Merged
merged 5 commits into from
May 12, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
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 "/// MSEdge Worker APIs"
Copy link
Contributor

Choose a reason for hiding this comment

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

i would remove MSEdge / IE alltogether.

| _ -> 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>" (DomTypeToTsType 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
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 @@

/////////////////////////////
/// MSEdge 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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

@saschanaz saschanaz May 12, 2017

Choose a reason for hiding this comment

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

It meets the standard on WHATWG spec:

interface DOMStringList {
  readonly attribute unsigned long length;
  getter DOMString? item(unsigned long index);
  boolean contains(DOMString string);
};

It has 1. an indexed property getter and 2. an integer-typed attribute named “length”.

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