14
14
*/
15
15
export = parser ;
16
16
17
+ // A type that's T but not U.
18
+ type Diff < T , U > = T extends U ? never : T ;
19
+
17
20
// TODO: Conditional types in TS 1.8 will really clean this up.
18
21
declare function parser ( ) : parser . Processor < never > ;
19
22
declare function parser < Transform > ( processor : parser . AsyncProcessor < Transform > ) : parser . Processor < Transform , never > ;
@@ -203,11 +206,14 @@ declare namespace parser {
203
206
interface ContainerOptions extends NodeOptions {
204
207
nodes ?: Array < Node > ;
205
208
}
206
- interface Container < Value extends string | undefined = string > extends Base < Value > {
207
- nodes : Array < Node > ;
208
- append ( selector : Selector ) : Container ;
209
- prepend ( selector : Selector ) : Container ;
210
- at ( index : number ) : Node ;
209
+ interface Container <
210
+ Value extends string | undefined = string ,
211
+ Child extends Node = Node
212
+ > extends Base < Value > {
213
+ nodes : Array < Child > ;
214
+ append ( selector : Selector ) : this;
215
+ prepend ( selector : Selector ) : this;
216
+ at ( index : number ) : Child ;
211
217
/**
212
218
* Return the most specific node at the line and column number given.
213
219
* The source location is based on the original parsed location, locations aren't
@@ -221,33 +227,74 @@ declare namespace parser {
221
227
* @param line The line number of the node to find. (1-based index)
222
228
* @param col The column number of the node to find. (1-based index)
223
229
*/
224
- atPosition ( line : number , column : number ) : Node ;
225
- index ( child : Node ) : number ;
226
- readonly first : Node ;
227
- readonly last : Node ;
230
+ atPosition ( line : number , column : number ) : Child ;
231
+ index ( child : Child ) : number ;
232
+ readonly first : Child ;
233
+ readonly last : Child ;
228
234
readonly length : number ;
229
- removeChild ( child : Node ) : Container ;
235
+ removeChild ( child : Child ) : this ;
230
236
removeAll ( ) : Container ;
231
237
empty ( ) : Container ;
232
- insertAfter ( oldNode : Node , newNode : Node ) : Container ;
233
- insertBefore ( oldNode : Node , newNode : Node ) : Container ;
234
- each ( callback : ( node : Node ) => boolean | void ) : boolean | undefined ;
235
- walk ( callback : ( node : Node ) => boolean | void ) : boolean | undefined ;
236
- walkAttributes ( callback : ( node : Node ) => boolean | void ) : boolean | undefined ;
237
- walkClasses ( callback : ( node : Node ) => boolean | void ) : boolean | undefined ;
238
- walkCombinators ( callback : ( node : Node ) => boolean | void ) : boolean | undefined ;
239
- walkComments ( callback : ( node : Node ) => boolean | void ) : boolean | undefined ;
240
- walkIds ( callback : ( node : Node ) => boolean | void ) : boolean | undefined ;
241
- walkNesting ( callback : ( node : Node ) => boolean | void ) : boolean | undefined ;
242
- walkPseudos ( callback : ( node : Node ) => boolean | void ) : boolean | undefined ;
243
- walkTags ( callback : ( node : Node ) => boolean | void ) : boolean | undefined ;
244
- split ( callback : ( node : Node ) => boolean ) : [ Node [ ] , Node [ ] ] ;
245
- map ( callback : ( node : Node ) => Node ) : Node [ ] ;
246
- reduce < T > ( callback : ( node : Node ) => Node , memo : T ) : T ;
247
- every ( callback : ( node : Node ) => boolean ) : boolean ;
248
- some ( callback : ( node : Node ) => boolean ) : boolean ;
249
- filter ( callback : ( node : Node ) => boolean ) : Node [ ] ;
250
- sort ( callback : ( nodeA : Node , nodeB : Node ) => number ) : Node [ ] ;
238
+ insertAfter ( oldNode : Child , newNode : Child ) : this;
239
+ insertBefore ( oldNode : Child , newNode : Child ) : this;
240
+ each ( callback : ( node : Child ) => boolean | void ) : boolean | undefined ;
241
+ walk (
242
+ callback : ( node : Node ) => boolean | void
243
+ ) : boolean | undefined ;
244
+ walkAttributes (
245
+ callback : ( node : Attribute ) => boolean | void
246
+ ) : boolean | undefined ;
247
+ walkClasses (
248
+ callback : ( node : ClassName ) => boolean | void
249
+ ) : boolean | undefined ;
250
+ walkCombinators (
251
+ callback : ( node : Combinator ) => boolean | void
252
+ ) : boolean | undefined ;
253
+ walkComments (
254
+ callback : ( node : Comment ) => boolean | void
255
+ ) : boolean | undefined ;
256
+ walkIds (
257
+ callback : ( node : Identifier ) => boolean | void
258
+ ) : boolean | undefined ;
259
+ walkNesting (
260
+ callback : ( node : Nesting ) => boolean | void
261
+ ) : boolean | undefined ;
262
+ walkPseudos (
263
+ callback : ( node : Pseudo ) => boolean | void
264
+ ) : boolean | undefined ;
265
+ walkTags ( callback : ( node : Tag ) => boolean | void ) : boolean | undefined ;
266
+ split ( callback : ( node : Child ) => boolean ) : [ Child [ ] , Child [ ] ] ;
267
+ map < T > ( callback : ( node : Child ) => T ) : T [ ] ;
268
+ reduce (
269
+ callback : (
270
+ previousValue : Child ,
271
+ currentValue : Child ,
272
+ currentIndex : number ,
273
+ array : readonly Child [ ]
274
+ ) => Child
275
+ ) : Child ;
276
+ reduce (
277
+ callback : (
278
+ previousValue : Child ,
279
+ currentValue : Child ,
280
+ currentIndex : number ,
281
+ array : readonly Child [ ]
282
+ ) => Child ,
283
+ initialValue : Child
284
+ ) : Child ;
285
+ reduce < T > (
286
+ callback : (
287
+ previousValue : T ,
288
+ currentValue : Child ,
289
+ currentIndex : number ,
290
+ array : readonly Child [ ]
291
+ ) => T ,
292
+ initialValue : T
293
+ ) : T ;
294
+ every ( callback : ( node : Child ) => boolean ) : boolean ;
295
+ some ( callback : ( node : Child ) => boolean ) : boolean ;
296
+ filter ( callback : ( node : Child ) => boolean ) : Child [ ] ;
297
+ sort ( callback : ( nodeA : Child , nodeB : Child ) => number ) : Child [ ] ;
251
298
toString ( ) : string ;
252
299
}
253
300
function isContainer ( node : any ) : node is Root | Selector | Pseudo ;
@@ -273,7 +320,7 @@ declare namespace parser {
273
320
}
274
321
function isNamespace ( node : any ) : node is Attribute | Tag ;
275
322
276
- interface Root extends Container < undefined > {
323
+ interface Root extends Container < undefined , Selector > {
277
324
type : "root" ;
278
325
/**
279
326
* Raises an error, if the processor is invoked on
@@ -285,7 +332,7 @@ declare namespace parser {
285
332
function root ( opts : ContainerOptions ) : Root ;
286
333
function isRoot ( node : any ) : node is Root ;
287
334
288
- interface Selector extends Container {
335
+ interface Selector extends Container < string , Diff < Node , Selector > > {
289
336
type : "selector" ;
290
337
}
291
338
function selector ( opts : ContainerOptions ) : Selector ;
@@ -439,7 +486,7 @@ declare namespace parser {
439
486
function attribute ( opts : AttributeOptions ) : Attribute ;
440
487
function isAttribute ( node : any ) : node is Attribute ;
441
488
442
- interface Pseudo extends Container {
489
+ interface Pseudo extends Container < string , Selector > {
443
490
type : "pseudo" ;
444
491
}
445
492
function pseudo ( opts : ContainerOptions ) : Pseudo ;
0 commit comments