Skip to content

Commit

Permalink
Merge branch 'master' into json
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielRosenwasser authored Nov 7, 2017
2 parents a15fdf1 + 1a54d1f commit 6378e14
Show file tree
Hide file tree
Showing 8 changed files with 1,665 additions and 811 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ The common steps to send a pull request are:
1. Add missing elements to `inputfiles/addedTypes.json`, overriding elements to `inputfiles/overridingTypes.json`, or elements to remove to `inputfiles/removedTypes.json`.
2. Run the build script locally to obtain new `dom.generated.d.ts` and `webworker.generated.d.ts`.
3. Update the files in the `baselines` folder using the newly generated files
under `generated` folder (`cp ./generated/* ./baseline/`).
under `generated` folder (`cp ./generated/* ./baselines/`).

### When should a DOM API be included here?

Expand Down
60 changes: 47 additions & 13 deletions TS.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ module InputJson =

let getItemByName (allItems: InputJsonType.Root []) (itemName: string) (kind: ItemKind) otherFilter =
let filter (item: InputJsonType.Root) =
OptionCheckValue itemName item.Name &&
(OptionCheckValue itemName item.Name || OptionCheckValue (sprintf "%s?" itemName) item.Name) &&
item.Kind.ToLower() = kind.ToString() &&
otherFilter item
allItems |> Array.tryFind filter
Expand Down Expand Up @@ -660,6 +660,7 @@ module Data =
|> Array.map (fun i -> (i.Name, getEventHandler i))
|> Map.ofArray

// Map of interface.Name -> List of base interfaces with event handlers
let iNameToEhParents =
let hasHandler (i : Browser.Interface) =
iNameToEhList.ContainsKey i.Name && not iNameToEhList.[i.Name].IsEmpty
Expand Down Expand Up @@ -1035,6 +1036,16 @@ module Emit =
| Some comment -> printLine "%s" comment
| _ -> ()

// A covariant EventHandler is one that is defined in a parent interface as then redefined in current interface with a more specific argument types
// These patterns are unsafe, and flagged as error under --strictFunctionTypes.
// Here we know the property is already defined on the interface, we elide its declaration if the parent has the same handler defined
let isCovariantEventHandler (p: Browser.Property) =
p.Type = "EventHandler" &&
iNameToEhParents.ContainsKey i.Name &&
not iNameToEhParents.[i.Name].IsEmpty &&
iNameToEhParents.[i.Name]
|> List.exists (fun i -> iNameToEhList.ContainsKey i.Name && not iNameToEhList.[i.Name].IsEmpty && iNameToEhList.[i.Name] |> List.exists (fun e-> e.Name = p.Name))

let emitProperty (p: Browser.Property) =
let printLine content =
if conflictedMembers.Contains p.Name then Pt.PrintlToStack content else Pt.Printl content
Expand Down Expand Up @@ -1071,6 +1082,7 @@ module Emit =
| Some ps ->
ps.Properties
|> Array.filter (ShouldKeep flavor)
|> Array.filter (isCovariantEventHandler >> not)
|> Array.iter emitProperty
| None -> ()

Expand All @@ -1096,7 +1108,12 @@ module Emit =
// Otherwise, this is EventTarget.addEventListener, we want to keep that.
let mFilter (m:Browser.Method) =
matchScope emitScope m &&
not (prefix <> "" && OptionCheckValue "addEventListener" m.Name)
not (
prefix <> "" && (
(OptionCheckValue "addEventListener" m.Name) ||
(OptionCheckValue "removeEventListener" m.Name)
)
)

let emitMethod flavor prefix (i:Browser.Interface) (m:Browser.Method) =
let printLine content =
Expand Down Expand Up @@ -1179,30 +1196,43 @@ module Emit =
| _ -> ()

let EmitEventHandlers (flavor: Flavor) (prefix: string) (i:Browser.Interface) =
let getOptionsType (addOrRemove: string) =
if addOrRemove = "add" then "AddEventListenerOptions" else "EventListenerOptions"

let fPrefix =
if prefix.StartsWith "declare var" then "declare function " else ""

let emitEventHandler prefix (iParent:Browser.Interface) =
let emitTypedEventHandler (prefix: string) (addOrRemove: string) (iParent:Browser.Interface) =
Pt.Printl
"%s%sEventListener<K extends keyof %sEventMap>(type: K, listener: (this: %s, ev: %sEventMap[K]) => any, options?: boolean | %s): void;"
prefix addOrRemove iParent.Name i.Name iParent.Name (getOptionsType addOrRemove)

let emitStringEventHandler (addOrRemove: string) =
Pt.Printl
"%saddEventListener<K extends keyof %sEventMap>(type: K, listener: (this: %s, ev: %sEventMap[K]) => any, useCapture?: boolean): void;"
prefix iParent.Name i.Name iParent.Name
"%s%sEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | %s): void;"
fPrefix addOrRemove (getOptionsType addOrRemove)

let shouldEmitStringEventHandler =
let tryEmitTypedEventHandlerForInterface (addOrRemove: string) =
if iNameToEhList.ContainsKey i.Name && not iNameToEhList.[i.Name].IsEmpty then
emitEventHandler fPrefix i
emitTypedEventHandler fPrefix addOrRemove i
true
elif iNameToEhParents.ContainsKey i.Name && not iNameToEhParents.[i.Name].IsEmpty then
iNameToEhParents.[i.Name]
|> List.sortBy (fun i -> i.Name)
|> List.iter (emitEventHandler fPrefix)
|> List.iter (emitTypedEventHandler fPrefix addOrRemove)
true
else
false

if shouldEmitStringEventHandler then
Pt.Printl
"%saddEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void;"
fPrefix
let emitEventHandler (addOrRemove: string) =
if tryEmitTypedEventHandlerForInterface addOrRemove then
// only emit the string event handler if we just emited a typed handler
emitStringEventHandler addOrRemove


emitEventHandler "add"
emitEventHandler "remove"


let EmitConstructorSignature flavor (i:Browser.Interface) =
let emitConstructorSigFromJson (c: InputJsonType.Root) =
Expand Down Expand Up @@ -1500,7 +1530,11 @@ module Emit =
| _ -> Pt.Printl "interface %s extends %s {" dict.Name dict.Extends

let emitJsonProperty (p: InputJsonType.Root) =
Pt.Printl "%s: %s;" p.Name.Value p.Type.Value
let readOnlyModifier =
match p.Readonly with
| Some(true) -> "readonly "
| _ -> ""
Pt.Printl "%s%s: %s;" readOnlyModifier p.Name.Value p.Type.Value

let removedPropNames =
getRemovedItems ItemKind.Property flavor
Expand Down
Loading

0 comments on commit 6378e14

Please sign in to comment.