Skip to content

Commit 9975a65

Browse files
author
Muj.Beg
committed
Merge branch 'master' into TypeScript-Issue-18615
# Conflicts: # baselines/dom.generated.d.ts # inputfiles/addedTypes.json # inputfiles/overridingTypes.json
2 parents 2cf4012 + 5b748d4 commit 9975a65

File tree

9 files changed

+2575
-914
lines changed

9 files changed

+2575
-914
lines changed

TS.fsx

Lines changed: 58 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ module InputJson =
175175

176176
let getItemByName (allItems: InputJsonType.Root []) (itemName: string) (kind: ItemKind) otherFilter =
177177
let filter (item: InputJsonType.Root) =
178-
OptionCheckValue itemName item.Name &&
178+
(OptionCheckValue itemName item.Name || OptionCheckValue (sprintf "%s?" itemName) item.Name) &&
179179
item.Kind.ToLower() = kind.ToString() &&
180180
otherFilter item
181181
allItems |> Array.tryFind filter
@@ -779,6 +779,10 @@ module Emit =
779779
(DomTypeToNullableTsType m.Type m.Nullable.IsSome) = expectedMType &&
780780
m.Params.Length = 1 &&
781781
(DomTypeToTsType m.Params.[0].Type) = expectedParamType
782+
let processInterfaceType iName =
783+
match getOverriddenItems ItemKind.Interface Flavor.All |> Array.tryFind (matchInterface iName) with
784+
| Some it -> iName + "<" + (it.TypeParameters |> String.concat ", ") + ">"
785+
| _ -> iName
782786

783787
/// Emit overloads for the createElement method
784788
let EmitCreateElementOverloads (m: Browser.Method) =
@@ -789,45 +793,47 @@ module Emit =
789793
/// Emit overloads for the getElementsByTagName method
790794
let EmitGetElementsByTagNameOverloads (m: Browser.Method) =
791795
if matchSingleParamMethodSignature m "getElementsByTagName" "NodeList" "string" then
792-
Pt.Printl "getElementsByTagName<K extends keyof ElementListTagNameMap>(%s: K): ElementListTagNameMap[K];" m.Params.[0].Name
796+
Pt.Printl "getElementsByTagName<K extends keyof HTMLElementTagNameMap>(%s: K): NodeListOf<HTMLElementTagNameMap[K]>;" m.Params.[0].Name
797+
Pt.Printl "getElementsByTagName<K extends keyof SVGElementTagNameMap>(%s: K): NodeListOf<SVGElementTagNameMap[K]>;" m.Params.[0].Name
793798
Pt.Printl "getElementsByTagName(%s: string): NodeListOf<Element>;" m.Params.[0].Name
794799

795800
/// Emit overloads for the querySelector method
796801
let EmitQuerySelectorOverloads (m: Browser.Method) =
797802
if matchSingleParamMethodSignature m "querySelector" "Element" "string" then
798-
Pt.Printl "querySelector<K extends keyof ElementTagNameMap>(selectors: K): ElementTagNameMap[K] | null;"
803+
Pt.Printl "querySelector<K extends keyof HTMLElementTagNameMap>(selectors: K): HTMLElementTagNameMap[K] | null;"
804+
Pt.Printl "querySelector<K extends keyof SVGElementTagNameMap>(selectors: K): SVGElementTagNameMap[K] | null;"
799805
Pt.Printl "querySelector<E extends Element = Element>(selectors: string): E | null;"
800806

801807
/// Emit overloads for the querySelectorAll method
802808
let EmitQuerySelectorAllOverloads (m: Browser.Method) =
803809
if matchSingleParamMethodSignature m "querySelectorAll" "NodeList" "string" then
804-
Pt.Printl "querySelectorAll<K extends keyof ElementListTagNameMap>(selectors: K): ElementListTagNameMap[K];"
810+
Pt.Printl "querySelectorAll<K extends keyof HTMLElementTagNameMap>(selectors: K): NodeListOf<HTMLElementTagNameMap[K]>;"
811+
Pt.Printl "querySelectorAll<K extends keyof SVGElementTagNameMap>(selectors: K): NodeListOf<SVGElementTagNameMap[K]>;"
805812
Pt.Printl "querySelectorAll<E extends Element = Element>(selectors: string): NodeListOf<E>;"
806813

807814
let EmitHTMLElementTagNameMap () =
808815
Pt.Printl "interface HTMLElementTagNameMap {"
809816
Pt.IncreaseIndent()
810817
for e in tagNameToEleName do
811-
if iNameToIDependList.ContainsKey e.Value && Seq.contains "HTMLElement" iNameToIDependList.[e.Value] then
818+
if iNameToIDependList.ContainsKey e.Value && not (Seq.contains "SVGElement" iNameToIDependList.[e.Value]) then
812819
Pt.Printl "\"%s\": %s;" (e.Key.ToLower()) e.Value
813820
Pt.DecreaseIndent()
814821
Pt.Printl "}"
815822
Pt.Printl ""
816823

817-
let EmitElementTagNameMap () =
818-
Pt.Printl "interface ElementTagNameMap extends HTMLElementTagNameMap {"
824+
let EmitSVGElementTagNameMap () =
825+
Pt.Printl "interface SVGElementTagNameMap {"
819826
Pt.IncreaseIndent()
820827
for e in tagNameToEleName do
821-
if iNameToIDependList.ContainsKey e.Value && not (Seq.contains "HTMLElement" iNameToIDependList.[e.Value]) then
828+
if iNameToIDependList.ContainsKey e.Value && Seq.contains "SVGElement" iNameToIDependList.[e.Value] then
822829
Pt.Printl "\"%s\": %s;" (e.Key.ToLower()) e.Value
823830
Pt.DecreaseIndent()
824831
Pt.Printl "}"
825832
Pt.Printl ""
826833

827-
let EmitElementListTagNameMap () =
828-
Pt.Printl "type ElementListTagNameMap = {"
829-
Pt.PrintWithAddedIndent "[key in keyof ElementTagNameMap]: NodeListOf<ElementTagNameMap[key]>"
830-
Pt.Printl "};"
834+
let EmitElementTagNameMap () =
835+
Pt.Printl "/** @deprecated Directly use HTMLElementTagNameMap or SVGElementTagNameMap as appropriate, instead. */"
836+
Pt.Printl "interface ElementTagNameMap extends HTMLElementTagNameMap, SVGElementTagNameMap { }"
831837
Pt.Printl ""
832838

833839
/// Emit overloads for the createEvent method
@@ -978,7 +984,12 @@ module Emit =
978984
// Otherwise, this is EventTarget.addEventListener, we want to keep that.
979985
let mFilter (m:Browser.Method) =
980986
matchScope emitScope m &&
981-
not (prefix <> "" && OptionCheckValue "addEventListener" m.Name)
987+
not (
988+
prefix <> "" && (
989+
(OptionCheckValue "addEventListener" m.Name) ||
990+
(OptionCheckValue "removeEventListener" m.Name)
991+
)
992+
)
982993

983994
let emitMethod flavor prefix (i:Browser.Interface) (m:Browser.Method) =
984995
let printLine content =
@@ -1061,30 +1072,43 @@ module Emit =
10611072
| _ -> ()
10621073

10631074
let EmitEventHandlers (flavor: Flavor) (prefix: string) (i:Browser.Interface) =
1075+
let getOptionsType (addOrRemove: string) =
1076+
if addOrRemove = "add" then "AddEventListenerOptions" else "EventListenerOptions"
1077+
10641078
let fPrefix =
10651079
if prefix.StartsWith "declare var" then "declare function " else ""
10661080

1067-
let emitEventHandler prefix (iParent:Browser.Interface) =
1081+
let emitTypedEventHandler (prefix: string) (addOrRemove: string) (iParent:Browser.Interface) =
1082+
Pt.Printl
1083+
"%s%sEventListener<K extends keyof %sEventMap>(type: K, listener: (this: %s, ev: %sEventMap[K]) => any, options?: boolean | %s): void;"
1084+
prefix addOrRemove iParent.Name i.Name iParent.Name (getOptionsType addOrRemove)
1085+
1086+
let emitStringEventHandler (addOrRemove: string) =
10681087
Pt.Printl
1069-
"%saddEventListener<K extends keyof %sEventMap>(type: K, listener: (this: %s, ev: %sEventMap[K]) => any, useCapture?: boolean): void;"
1070-
prefix iParent.Name i.Name iParent.Name
1088+
"%s%sEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | %s): void;"
1089+
fPrefix addOrRemove (getOptionsType addOrRemove)
10711090

1072-
let shouldEmitStringEventHandler =
1091+
let tryEmitTypedEventHandlerForInterface (addOrRemove: string) =
10731092
if iNameToEhList.ContainsKey i.Name && not iNameToEhList.[i.Name].IsEmpty then
1074-
emitEventHandler fPrefix i
1093+
emitTypedEventHandler fPrefix addOrRemove i
10751094
true
10761095
elif iNameToEhParents.ContainsKey i.Name && not iNameToEhParents.[i.Name].IsEmpty then
10771096
iNameToEhParents.[i.Name]
10781097
|> List.sortBy (fun i -> i.Name)
1079-
|> List.iter (emitEventHandler fPrefix)
1098+
|> List.iter (emitTypedEventHandler fPrefix addOrRemove)
10801099
true
10811100
else
10821101
false
10831102

1084-
if shouldEmitStringEventHandler then
1085-
Pt.Printl
1086-
"%saddEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void;"
1087-
fPrefix
1103+
let emitEventHandler (addOrRemove: string) =
1104+
if tryEmitTypedEventHandlerForInterface addOrRemove then
1105+
// only emit the string event handler if we just emited a typed handler
1106+
emitStringEventHandler addOrRemove
1107+
1108+
1109+
emitEventHandler "add"
1110+
emitEventHandler "remove"
1111+
10881112

10891113
let EmitConstructorSignature flavor (i:Browser.Interface) =
10901114
let emitConstructorSigFromJson (c: InputJsonType.Root) =
@@ -1142,9 +1166,9 @@ module Emit =
11421166

11431167
let processedIName = processIName i.Name
11441168
if processedIName <> i.Name then
1145-
Pt.PrintlToStack "interface %s extends %s {" i.Name processedIName
1169+
Pt.PrintlToStack "interface %s extends %s {" (processInterfaceType i.Name) processedIName
11461170

1147-
Pt.Printl "interface %s" processedIName
1171+
Pt.Printl "interface %s" (processInterfaceType processedIName)
11481172
let finalExtends =
11491173
let overridenExtendsFromJson =
11501174
InputJson.getOverriddenItemsByInterfaceName ItemKind.Extends Flavor.All i.Name
@@ -1369,13 +1393,18 @@ module Emit =
13691393
EmitConstructor flavor i
13701394

13711395
let EmitDictionaries flavor =
1396+
13721397
let emitDictionary (dict:Browser.Dictionary) =
13731398
match dict.Extends with
1374-
| "Object" -> Pt.Printl "interface %s {" dict.Name
1375-
| _ -> Pt.Printl "interface %s extends %s {" dict.Name dict.Extends
1399+
| "Object" -> Pt.Printl "interface %s {" (processInterfaceType dict.Name)
1400+
| _ -> Pt.Printl "interface %s extends %s {" (processInterfaceType dict.Name) dict.Extends
13761401

13771402
let emitJsonProperty (p: InputJsonType.Root) =
1378-
Pt.Printl "%s: %s;" p.Name.Value p.Type.Value
1403+
let readOnlyModifier =
1404+
match p.Readonly with
1405+
| Some(true) -> "readonly "
1406+
| _ -> ""
1407+
Pt.Printl "%s%s: %s;" readOnlyModifier p.Name.Value p.Type.Value
13791408

13801409
let removedPropNames =
13811410
getRemovedItems ItemKind.Property flavor
@@ -1489,8 +1518,8 @@ module Emit =
14891518

14901519
if flavor <> Worker then
14911520
EmitHTMLElementTagNameMap()
1521+
EmitSVGElementTagNameMap()
14921522
EmitElementTagNameMap()
1493-
EmitElementListTagNameMap()
14941523
EmitNamedConstructors()
14951524

14961525
match GetGlobalPollutor flavor with

0 commit comments

Comments
 (0)