@@ -28,7 +28,7 @@ const extendConflictsBaseTypes: Record<string, { extendType: string[], memberNam
28
28
"AudioContext" : { extendType : [ "OfflineContext" ] , memberNames : new Set ( [ "suspend" ] ) } ,
29
29
"HTMLCollection" : { extendType : [ "HTMLFormControlsCollection" ] , memberNames : new Set ( [ "namedItem" ] ) } ,
30
30
} ;
31
- const evetTypeMap : Record < string , string > = {
31
+ const eventTypeMap : Record < string , string > = {
32
32
"abort" : "UIEvent" ,
33
33
"complete" : "Event" ,
34
34
"click" : "MouseEvent" ,
@@ -60,6 +60,7 @@ function createTextWriter(newLine: string) {
60
60
let output : string ;
61
61
let indent : number ;
62
62
let lineStart : boolean ;
63
+ /** print declarations conflicting with base interface to a side list to write them under a diffrent name later */
63
64
let stack : { content : string , indent : number } [ ] = [ ] ;
64
65
65
66
const indentStrings : string [ ] = [ "" , " " ] ;
@@ -95,7 +96,6 @@ function createTextWriter(newLine: string) {
95
96
reset ( ) ;
96
97
97
98
return {
98
-
99
99
reset : reset ,
100
100
101
101
resetIndent ( ) { indent = 0 ; } ,
@@ -105,8 +105,6 @@ function createTextWriter(newLine: string) {
105
105
print : write ,
106
106
printLine ( c : string ) { writeLine ( ) ; write ( c ) ; } ,
107
107
108
- printWithAddedIndent ( c : string ) { this . increaseIndent ( ) ; this . printLine ( c ) ; this . decreaseIndent ( ) ; } ,
109
-
110
108
clearStack ( ) { stack = [ ] ; } ,
111
109
stackIsEmpty ( ) { return stack . length === 0 ; } ,
112
110
printLineToStack ( content : string ) { stack . push ( { content, indent } ) ; } ,
@@ -145,7 +143,7 @@ export function emitWebIDl(webidl: Browser.WebIdl, flavor: Flavor) {
145
143
const allTypeDefsMap = new Set ( webidl . typedefs && webidl . typedefs . typedef . map ( td => td [ "new-type" ] ) ) ;
146
144
147
145
/// Event name to event type map
148
- const eNameToEType = arrayToMap ( flatMap ( allNonCallbackInterfaces , i => i . events ? i . events . event : [ ] ) , e => e . name , e => evetTypeMap [ e . name ] || e . type ) ;
146
+ const eNameToEType = arrayToMap ( flatMap ( allNonCallbackInterfaces , i => i . events ? i . events . event : [ ] ) , e => e . name , e => eventTypeMap [ e . name ] || e . type ) ;
149
147
150
148
/// Tag name to element name map
151
149
const tagNameToEleName = getTagNameToElementNameMap ( ) ;
@@ -512,7 +510,9 @@ export function emitWebIDl(webidl: Browser.WebIdl, flavor: Flavor) {
512
510
function emitCallBackInterface ( i : Browser . Interface ) {
513
511
if ( i . name === "EventListener" ) {
514
512
printer . printLine ( `interface ${ i . name } {` ) ;
515
- printer . printWithAddedIndent ( "(evt: Event): void;" ) ;
513
+ printer . increaseIndent ( ) ;
514
+ printer . printLine ( "(evt: Event): void;" ) ;
515
+ printer . decreaseIndent ( ) ;
516
516
printer . printLine ( "}" ) ;
517
517
}
518
518
else {
@@ -612,27 +612,23 @@ export function emitWebIDl(webidl: Browser.WebIdl, flavor: Flavor) {
612
612
}
613
613
}
614
614
615
- function emitComments ( entity : { comment ?: string ; deprecated ?: 1 } | undefined , print : ( s : string ) => void ) {
616
- if ( entity ) {
617
- if ( entity . comment ) {
618
- print ( entity . comment ) ;
619
- }
620
- if ( entity . deprecated ) {
621
- print ( `/** @deprecated */` ) ;
622
- }
615
+ function emitComments ( entity : { comment ?: string ; deprecated ?: 1 } , print : ( s : string ) => void ) {
616
+ if ( entity . comment ) {
617
+ print ( entity . comment ) ;
618
+ }
619
+ if ( entity . deprecated ) {
620
+ print ( `/** @deprecated */` ) ;
623
621
}
624
622
}
625
623
626
624
function emitProperties ( prefix : string , emitScope : EmitScope , i : Browser . Interface , conflictedMembers : Set < string > ) {
627
625
// Note: the schema file shows the property doesn't have "static" attribute,
628
626
// therefore all properties are emited for the instance type.
629
- if ( emitScope !== EmitScope . StaticOnly ) {
630
- if ( i . properties ) {
631
- mapToArray ( i . properties . property )
632
- . filter ( p => ! isCovariantEventHandler ( i , p ) )
633
- . sort ( compareName )
634
- . forEach ( p => emitProperty ( prefix , i , emitScope , p , conflictedMembers ) ) ;
635
- }
627
+ if ( emitScope !== EmitScope . StaticOnly && i . properties ) {
628
+ mapToArray ( i . properties . property )
629
+ . filter ( p => ! isCovariantEventHandler ( i , p ) )
630
+ . sort ( compareName )
631
+ . forEach ( p => emitProperty ( prefix , i , emitScope , p , conflictedMembers ) ) ;
636
632
}
637
633
}
638
634
@@ -680,14 +676,9 @@ export function emitWebIDl(webidl: Browser.WebIdl, flavor: Flavor) {
680
676
function emitMethods ( prefix : string , emitScope : EmitScope , i : Browser . Interface , conflictedMembers : Set < string > ) {
681
677
// If prefix is not empty, then this is the global declare function addEventListener, we want to override this
682
678
// Otherwise, this is EventTarget.addEventListener, we want to keep that.
683
- function mFilter ( m : Browser . Method ) {
684
- return matchScope ( emitScope , m ) &&
685
- ! ( prefix !== "" && ( m . name === "addEventListener" || m . name === "removeEventListener" ) ) ;
686
- }
687
-
688
679
if ( i . methods ) {
689
680
mapToArray ( i . methods . method )
690
- . filter ( mFilter )
681
+ . filter ( m => matchScope ( emitScope , m ) && ! ( prefix !== "" && ( m . name === "addEventListener" || m . name === "removeEventListener" ) ) )
691
682
. sort ( compareName )
692
683
. forEach ( m => emitMethod ( prefix , i , m , conflictedMembers ) ) ;
693
684
}
@@ -710,8 +701,7 @@ export function emitWebIDl(webidl: Browser.WebIdl, flavor: Flavor) {
710
701
/// Emit all members of every interfaces at the root level.
711
702
/// Called only once on the global polluter object
712
703
function emitAllMembers ( i : Browser . Interface ) {
713
- const prefix = "declare var " ;
714
- emitMembers ( prefix , EmitScope . All , i ) ;
704
+ emitMembers ( /*prefix*/ "declare var " , EmitScope . All , i ) ;
715
705
716
706
for ( const relatedIName of iNameToIDependList [ i . name ] ) {
717
707
const i = allInterfacesMap [ relatedIName ] ;
@@ -722,52 +712,43 @@ export function emitWebIDl(webidl: Browser.WebIdl, flavor: Flavor) {
722
712
}
723
713
724
714
function emitEventHandlers ( prefix : string , i : Browser . Interface ) {
725
- function getOptionsType ( addOrRemove : string ) {
726
- return addOrRemove === "add" ? "AddEventListenerOptions" : "EventListenerOptions" ;
727
- }
728
-
729
715
const fPrefix = prefix . startsWith ( "declare var" ) ? "declare function " : "" ;
730
716
731
- function emitTypedEventHandler ( prefix : string , addOrRemove : string , iParent : Browser . Interface ) {
732
- printer . printLine ( `${ prefix } ${ addOrRemove } EventListener<K extends keyof ${ iParent . name } EventMap>(type: K, listener: (this: ${ i . name } , ev: ${ iParent . name } EventMap[K]) => any, options?: boolean | ${ getOptionsType ( addOrRemove ) } ): void;` ) ;
717
+ for ( const addOrRemove of [ "add" , "remove" ] ) {
718
+ const optionsType = addOrRemove === "add" ? "AddEventListenerOptions" : "EventListenerOptions" ;
719
+ if ( tryEmitTypedEventHandlerForInterface ( addOrRemove , optionsType ) ) {
720
+ // only emit the string event handler if we just emited a typed handler
721
+ printer . printLine ( `${ fPrefix } ${ addOrRemove } EventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | ${ optionsType } ): void;` ) ;
722
+ }
733
723
}
734
724
735
- function emitStringEventHandler ( addOrRemove : string ) {
736
- printer . printLine ( `${ fPrefix } ${ addOrRemove } EventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | ${ getOptionsType ( addOrRemove ) } ): void;` ) ;
725
+ return ;
726
+
727
+ function emitTypedEventHandler ( prefix : string , addOrRemove : string , iParent : Browser . Interface , optionsType : string ) {
728
+ printer . printLine ( `${ prefix } ${ addOrRemove } EventListener<K extends keyof ${ iParent . name } EventMap>(type: K, listener: (this: ${ i . name } , ev: ${ iParent . name } EventMap[K]) => any, options?: boolean | ${ optionsType } ): void;` ) ;
737
729
}
738
730
739
- function tryEmitTypedEventHandlerForInterface ( addOrRemove : string ) {
731
+ function tryEmitTypedEventHandlerForInterface ( addOrRemove : string , optionsType : string ) {
740
732
if ( iNameToEhList [ i . name ] && iNameToEhList [ i . name ] . length ) {
741
- emitTypedEventHandler ( fPrefix , addOrRemove , i ) ;
733
+ emitTypedEventHandler ( fPrefix , addOrRemove , i , optionsType ) ;
742
734
return true ;
743
735
}
744
736
if ( iNameToEhParents [ i . name ] && iNameToEhParents [ i . name ] . length ) {
745
737
iNameToEhParents [ i . name ]
746
738
. sort ( compareName )
747
- . forEach ( i => emitTypedEventHandler ( fPrefix , addOrRemove , i ) ) ;
739
+ . forEach ( i => emitTypedEventHandler ( fPrefix , addOrRemove , i , optionsType ) ) ;
748
740
return true ;
749
741
}
750
742
return false ;
751
743
}
752
-
753
- function emitEventHandler ( addOrRemove : string ) {
754
- if ( tryEmitTypedEventHandlerForInterface ( addOrRemove ) ) {
755
- // only emit the string event handler if we just emited a typed handler
756
- emitStringEventHandler ( addOrRemove ) ;
757
- }
758
- }
759
-
760
- emitEventHandler ( "add" ) ;
761
- emitEventHandler ( "remove" ) ;
762
744
}
763
745
764
746
function emitConstructorSignature ( i : Browser . Interface ) {
765
747
const constructor = typeof i . constructor === "object" ? i . constructor : undefined ;
766
748
767
- emitComments ( constructor , s => printer . print ( s ) ) ;
768
-
769
749
// Emit constructor signature
770
750
if ( constructor ) {
751
+ emitComments ( constructor , s => printer . print ( s ) ) ;
771
752
emitSignatures ( constructor , "" , "new" , s => printer . printLine ( s ) ) ;
772
753
}
773
754
else {
@@ -782,7 +763,7 @@ export function emitWebIDl(webidl: Browser.WebIdl, flavor: Flavor) {
782
763
printer . printLine ( `prototype: ${ i . name } ;` ) ;
783
764
emitConstructorSignature ( i ) ;
784
765
emitConstants ( i ) ;
785
- emitMembers ( "" , EmitScope . StaticOnly , i ) ;
766
+ emitMembers ( /*prefix*/ "" , EmitScope . StaticOnly , i ) ;
786
767
787
768
printer . decreaseIndent ( ) ;
788
769
printer . printLine ( "};" ) ;
@@ -793,7 +774,9 @@ export function emitWebIDl(webidl: Browser.WebIdl, flavor: Flavor) {
793
774
const nc = i [ "named-constructor" ] ;
794
775
if ( nc ) {
795
776
printer . printLine ( `declare var ${ nc . name } : {` ) ;
796
- nc . signature . forEach ( s => printer . printWithAddedIndent ( `new(${ s . param ? paramsToString ( s . param ) : "" } ): ${ i . name } ;` ) ) ;
777
+ printer . increaseIndent ( ) ;
778
+ nc . signature . forEach ( s => printer . printLine ( `new(${ s . param ? paramsToString ( s . param ) : "" } ): ${ i . name } ;` ) ) ;
779
+ printer . decreaseIndent ( ) ;
797
780
printer . printLine ( `};` ) ;
798
781
}
799
782
}
0 commit comments