From bdbcbfe1a6897e5f4339e42a51a192f4316612d9 Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Fri, 21 Apr 2017 17:06:13 +0900 Subject: [PATCH] support implicit iterable interfaces --- TS.fsx | 57 ++++++++++++++-- baselines/dom.generated.d.ts | 2 +- baselines/dom.generated.es6.d.ts | 104 +++++++++++++++++++++++++++++ baselines/webworker.generated.d.ts | 2 +- 4 files changed, 158 insertions(+), 7 deletions(-) create mode 100644 baselines/dom.generated.es6.d.ts diff --git a/TS.fsx b/TS.fsx index 0213e4b0a..a406d16b8 100644 --- a/TS.fsx +++ b/TS.fsx @@ -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" @@ -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 @@ -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" @@ -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 "" @@ -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 diff --git a/baselines/dom.generated.d.ts b/baselines/dom.generated.d.ts index 8749944b8..ec7a54f77 100644 --- a/baselines/dom.generated.d.ts +++ b/baselines/dom.generated.d.ts @@ -1,6 +1,6 @@ ///////////////////////////// -/// IE DOM APIs +/// MSEdge DOM APIs ///////////////////////////// interface Account { diff --git a/baselines/dom.generated.es6.d.ts b/baselines/dom.generated.es6.d.ts new file mode 100644 index 000000000..82aaeb173 --- /dev/null +++ b/baselines/dom.generated.es6.d.ts @@ -0,0 +1,104 @@ + +///////////////////////////// +/// MSEdge DOM ES6 APIs +///////////////////////////// + +interface AudioTrackList { + [Symbol.iterator](): IterableIterator +} + +interface CSSRuleList { + [Symbol.iterator](): IterableIterator +} + +interface CSSStyleDeclaration { + [Symbol.iterator](): IterableIterator +} + +interface ClientRectList { + [Symbol.iterator](): IterableIterator +} + +interface DOMStringList { + [Symbol.iterator](): IterableIterator +} + +interface DOMTokenList { + [Symbol.iterator](): IterableIterator +} + +interface DataTransferItemList { + [Symbol.iterator](): IterableIterator +} + +interface FileList { + [Symbol.iterator](): IterableIterator +} + +interface HTMLAllCollection { + [Symbol.iterator](): IterableIterator +} + +interface HTMLCollection { + [Symbol.iterator](): IterableIterator +} + +interface MSRangeCollection { + [Symbol.iterator](): IterableIterator +} + +interface MediaList { + [Symbol.iterator](): IterableIterator +} + +interface MimeTypeArray { + [Symbol.iterator](): IterableIterator +} + +interface NamedNodeMap { + [Symbol.iterator](): IterableIterator +} + +interface NodeList { + [Symbol.iterator](): IterableIterator +} + +interface Plugin { + [Symbol.iterator](): IterableIterator +} + +interface PluginArray { + [Symbol.iterator](): IterableIterator +} + +interface SourceBufferList { + [Symbol.iterator](): IterableIterator +} + +interface Storage { + [Symbol.iterator](): IterableIterator +} + +interface StyleSheetList { + [Symbol.iterator](): IterableIterator +} + +interface StyleSheetPageList { + [Symbol.iterator](): IterableIterator +} + +interface TextTrackCueList { + [Symbol.iterator](): IterableIterator +} + +interface TextTrackList { + [Symbol.iterator](): IterableIterator +} + +interface TouchList { + [Symbol.iterator](): IterableIterator +} + +interface VideoTrackList { + [Symbol.iterator](): IterableIterator +} diff --git a/baselines/webworker.generated.d.ts b/baselines/webworker.generated.d.ts index 1db3674ae..3704ce060 100644 --- a/baselines/webworker.generated.d.ts +++ b/baselines/webworker.generated.d.ts @@ -1,6 +1,6 @@ ///////////////////////////// -/// IE Worker APIs +/// MSEdge Worker APIs ///////////////////////////// interface Algorithm {