Skip to content
This repository was archived by the owner on May 5, 2021. It is now read-only.

Commit 2300369

Browse files
committed
[IMP] Core: improve Predicate typing
1 parent e401c0e commit 2300369

File tree

8 files changed

+86
-85
lines changed

8 files changed

+86
-85
lines changed

packages/core/src/ContextManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface CheckingContext extends Context {
1212

1313
export interface Contextual {
1414
context?: Context;
15-
selector?: Predicate<VNode | boolean>[];
15+
selector?: Predicate[];
1616
check?: (context: CheckingContext) => boolean;
1717
}
1818

packages/core/src/VNodes/AbstractNode.ts

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { VNode, RelativePosition, Predicate, Typeguard, isLeaf } from './VNode';
22
import { Constructor, nodeLength } from '../../../utils/src/utils';
3+
import { ContainerNode } from './ContainerNode';
4+
import { AtomicNode } from './AtomicNode';
35

46
let id = 0;
57
export abstract class AbstractNode {
@@ -14,9 +16,9 @@ export abstract class AbstractNode {
1416
*
1517
* @param predicate The predicate to check.
1618
*/
17-
static isConstructor<T extends VNode>(
18-
predicate: Constructor<T> | Predicate,
19-
): predicate is Constructor<T> {
19+
static isConstructor(
20+
predicate: Predicate,
21+
): predicate is Constructor<ContainerNode> | Constructor<AtomicNode> {
2022
return predicate.prototype instanceof AbstractNode;
2123
}
2224

@@ -78,7 +80,9 @@ export abstract class AbstractNode {
7880
*
7981
* @param predicate The subclass of VNode to test this node against.
8082
*/
81-
is<T extends VNode>(predicate: Constructor<T> | Typeguard<T>): this is T {
83+
is<T extends VNode>(predicate: Constructor<T> | Typeguard<T>): this is T;
84+
is(predicate: Predicate): false;
85+
is(predicate: Predicate): boolean {
8286
if (AbstractNode.isConstructor(predicate)) {
8387
return this instanceof predicate;
8488
} else {
@@ -96,11 +100,11 @@ export abstract class AbstractNode {
96100
*
97101
* @param predicate The predicate to test this node against.
98102
*/
99-
test<T extends VNode>(predicate?: Constructor<T> | Predicate): boolean {
103+
test(predicate?: Predicate): boolean {
100104
if (!predicate) {
101105
return true;
102106
} else if (AbstractNode.isConstructor(predicate)) {
103-
return this.is(predicate);
107+
return this instanceof predicate;
104108
} else {
105109
return predicate(this as VNode);
106110
}
@@ -161,8 +165,8 @@ export abstract class AbstractNode {
161165
* @param predicate
162166
*/
163167
closest<T extends VNode>(predicate: Predicate<T>): T;
164-
closest<T>(predicate: Predicate<T>): VNode;
165-
closest<T>(predicate: Predicate<T>): VNode {
168+
closest(predicate: Predicate): VNode;
169+
closest(predicate: Predicate): VNode {
166170
if (this.test(predicate)) {
167171
return this as VNode;
168172
} else {
@@ -176,8 +180,8 @@ export abstract class AbstractNode {
176180
* @param [predicate]
177181
*/
178182
ancestor<T extends VNode>(predicate?: Predicate<T>): T;
179-
ancestor<T>(predicate?: Predicate<T>): VNode;
180-
ancestor<T>(predicate?: Predicate<T>): VNode {
183+
ancestor(predicate?: Predicate): VNode;
184+
ancestor(predicate?: Predicate): VNode {
181185
let ancestor = this.parent;
182186
while (ancestor && !ancestor.test(predicate)) {
183187
ancestor = ancestor.parent;
@@ -192,8 +196,8 @@ export abstract class AbstractNode {
192196
* @param [predicate]
193197
*/
194198
ancestors<T extends VNode>(predicate?: Predicate<T>): T[];
195-
ancestors<T>(predicate?: Predicate<T>): VNode[];
196-
ancestors<T>(predicate?: Predicate<T>): VNode[] {
199+
ancestors(predicate?: Predicate): VNode[];
200+
ancestors(predicate?: Predicate): VNode[] {
197201
const ancestors: VNode[] = [];
198202
let parent = this.parent;
199203
while (parent) {
@@ -210,8 +214,8 @@ export abstract class AbstractNode {
210214
* @param node
211215
*/
212216
commonAncestor<T extends VNode>(node: VNode, predicate?: Predicate<T>): T;
213-
commonAncestor<T>(node: VNode, predicate?: Predicate<T>): VNode;
214-
commonAncestor<T>(node: VNode, predicate?: Predicate<T>): VNode {
217+
commonAncestor(node: VNode, predicate?: Predicate): VNode;
218+
commonAncestor(node: VNode, predicate?: Predicate): VNode {
215219
if (!this.parent) {
216220
return;
217221
} else if (this.parent === node.parent && this.parent.test(predicate)) {
@@ -231,8 +235,8 @@ export abstract class AbstractNode {
231235
* @param [predicate]
232236
*/
233237
siblings<T extends VNode>(predicate?: Predicate<T>): T[];
234-
siblings<T>(predicate?: Predicate<T>): VNode[];
235-
siblings<T>(predicate?: Predicate<T>): VNode[] {
238+
siblings(predicate?: Predicate): VNode[];
239+
siblings(predicate?: Predicate): VNode[] {
236240
const siblings: VNode[] = [];
237241
let sibling: VNode = this.previousSibling();
238242
while (sibling) {
@@ -254,8 +258,8 @@ export abstract class AbstractNode {
254258
* Return the nodes adjacent to this VNode that satisfy the given predicate.
255259
*/
256260
adjacents<T extends VNode>(predicate?: Predicate<T>): T[];
257-
adjacents<T>(predicate?: Predicate<T>): VNode[];
258-
adjacents<T>(predicate?: Predicate<T>): VNode[] {
261+
adjacents(predicate?: Predicate): VNode[];
262+
adjacents(predicate?: Predicate): VNode[] {
259263
const adjacents: VNode[] = [];
260264
let sibling: VNode = this.previousSibling();
261265
while (sibling && sibling.test(predicate)) {
@@ -276,8 +280,8 @@ export abstract class AbstractNode {
276280
* @param [predicate]
277281
*/
278282
previousSibling<T extends VNode>(predicate?: Predicate<T>): T;
279-
previousSibling<T>(predicate?: Predicate<T>): VNode;
280-
previousSibling<T>(predicate?: Predicate<T>): VNode {
283+
previousSibling(predicate?: Predicate): VNode;
284+
previousSibling(predicate?: Predicate): VNode {
281285
if (!this.parent) return;
282286
const index = this.parent.childVNodes.indexOf(this as VNode);
283287
let sibling = this.parent.childVNodes[index - 1];
@@ -294,8 +298,8 @@ export abstract class AbstractNode {
294298
* @param [predicate]
295299
*/
296300
nextSibling<T extends VNode>(predicate?: Predicate<T>): T;
297-
nextSibling<T>(predicate?: Predicate<T>): VNode;
298-
nextSibling<T>(predicate?: Predicate<T>): VNode {
301+
nextSibling(predicate?: Predicate): VNode;
302+
nextSibling(predicate?: Predicate): VNode {
299303
if (!this.parent) return;
300304
const index = this.parent.childVNodes.indexOf(this as VNode);
301305
let sibling = this.parent.childVNodes[index + 1];
@@ -313,8 +317,8 @@ export abstract class AbstractNode {
313317
* @param [predicate]
314318
*/
315319
previous<T extends VNode>(predicate?: Predicate<T>): T;
316-
previous<T>(predicate?: Predicate<T>): VNode;
317-
previous<T>(predicate?: Predicate<T>): VNode {
320+
previous(predicate?: Predicate): VNode;
321+
previous(predicate?: Predicate): VNode {
318322
let previous = this.previousSibling();
319323
if (previous) {
320324
// The previous node is the last leaf of the previous sibling.
@@ -336,8 +340,8 @@ export abstract class AbstractNode {
336340
* @param [predicate]
337341
*/
338342
next<T extends VNode>(predicate?: Predicate<T>): T;
339-
next<T>(predicate?: Predicate<T>): VNode;
340-
next<T>(predicate?: Predicate<T>): VNode {
343+
next(predicate?: Predicate): VNode;
344+
next(predicate?: Predicate): VNode {
341345
// The node after node is its first child.
342346
let next = this.firstChild();
343347
if (!next) {
@@ -366,8 +370,8 @@ export abstract class AbstractNode {
366370
* @param [predicate]
367371
*/
368372
previousLeaf<T extends VNode>(predicate?: Predicate<T>): T;
369-
previousLeaf<T>(predicate?: Predicate<T>): VNode;
370-
previousLeaf<T>(predicate?: Predicate<T>): VNode {
373+
previousLeaf(predicate?: Predicate): VNode;
374+
previousLeaf(predicate?: Predicate): VNode {
371375
return this.previous((node: VNode): boolean => {
372376
return isLeaf(node) && node.test(predicate);
373377
});
@@ -380,8 +384,8 @@ export abstract class AbstractNode {
380384
* @param [predicate]
381385
*/
382386
nextLeaf<T extends VNode>(predicate?: Predicate<T>): T;
383-
nextLeaf<T>(predicate?: Predicate<T>): VNode;
384-
nextLeaf<T>(predicate?: Predicate<T>): VNode {
387+
nextLeaf(predicate?: Predicate): VNode;
388+
nextLeaf(predicate?: Predicate): VNode {
385389
return this.next((node: VNode): boolean => {
386390
return isLeaf(node) && node.test(predicate);
387391
});
@@ -394,8 +398,8 @@ export abstract class AbstractNode {
394398
* @param [predicate]
395399
*/
396400
previousSiblings<T extends VNode>(predicate?: Predicate<T>): T[];
397-
previousSiblings<T>(predicate?: Predicate<T>): VNode[];
398-
previousSiblings<T>(predicate?: Predicate<T>): VNode[] {
401+
previousSiblings(predicate?: Predicate): VNode[];
402+
previousSiblings(predicate?: Predicate): VNode[] {
399403
const previousSiblings: VNode[] = [];
400404
let sibling = this.previousSibling();
401405
while (sibling) {
@@ -414,8 +418,8 @@ export abstract class AbstractNode {
414418
* @param [predicate]
415419
*/
416420
nextSiblings<T extends VNode>(predicate?: Predicate<T>): T[];
417-
nextSiblings<T>(predicate?: Predicate<T>): VNode[];
418-
nextSiblings<T>(predicate?: Predicate<T>): VNode[] {
421+
nextSiblings(predicate?: Predicate): VNode[];
422+
nextSiblings(predicate?: Predicate): VNode[] {
419423
const nextSiblings: VNode[] = [];
420424
let sibling = this.nextSibling();
421425
while (sibling) {
@@ -518,31 +522,31 @@ export abstract class AbstractNode {
518522
* @param [predicate]
519523
*/
520524
abstract firstChild<T extends VNode>(predicate?: Predicate<T>): T;
521-
abstract firstChild<T>(predicate?: Predicate<T>): VNode;
525+
abstract firstChild(predicate?: Predicate): VNode;
522526
/**
523527
* Return the last child of this VNode that satisfies the given predicate.
524528
* If no predicate is given, return the last child of this VNode.
525529
*
526530
* @param [predicate]
527531
*/
528532
abstract lastChild<T extends VNode>(predicate?: Predicate<T>): T;
529-
abstract lastChild<T>(predicate?: Predicate<T>): VNode;
533+
abstract lastChild(predicate?: Predicate): VNode;
530534
/**
531535
* Return the first leaf of this VNode that satisfies the given predicate.
532536
* If no predicate is given, return the first leaf of this VNode.
533537
*
534538
* @param [predicate]
535539
*/
536540
abstract firstLeaf<T extends VNode>(predicate?: Predicate<T>): T;
537-
abstract firstLeaf<T>(predicate?: Predicate<T>): VNode;
541+
abstract firstLeaf(predicate?: Predicate): VNode;
538542
/**
539543
* Return the last leaf of this VNode that satisfies the given predicate.
540544
* If no predicate is given, return the last leaf of this VNode.
541545
*
542546
* @param [predicate]
543547
*/
544548
abstract lastLeaf<T extends VNode>(predicate?: Predicate<T>): T;
545-
abstract lastLeaf<T>(predicate?: Predicate<T>): VNode;
549+
abstract lastLeaf(predicate?: Predicate): VNode;
546550
/**
547551
* Return all descendants of the current node that satisfy the given
548552
* predicate. If no predicate is given return all the ancestors of the
@@ -551,23 +555,23 @@ export abstract class AbstractNode {
551555
* @param [predicate]
552556
*/
553557
abstract descendants<T extends VNode>(predicate?: Predicate<T>): T[];
554-
abstract descendants<T>(predicate?: Predicate<T>): VNode[];
558+
abstract descendants(predicate?: Predicate): VNode[];
555559
/**
556560
* Return the first descendant of this VNode that satisfies the predicate.
557561
* If no predicate is given, return the first descendant of this VNode.
558562
*
559563
* @param [predicate]
560564
*/
561565
abstract firstDescendant<T extends VNode>(predicate?: Predicate<T>): T;
562-
abstract firstDescendant<T>(predicate?: Predicate<T>): VNode;
566+
abstract firstDescendant(predicate?: Predicate): VNode;
563567
/**
564568
* Return the last descendant of this VNode that satisfies the predicate.
565569
* If no predicate is given, return the last descendant of this VNode.
566570
*
567571
* @param [predicate]
568572
*/
569573
abstract lastDescendant<T extends VNode>(predicate?: Predicate<T>): T;
570-
abstract lastDescendant<T>(predicate?: Predicate<T>): VNode;
574+
abstract lastDescendant(predicate?: Predicate): VNode;
571575

572576
//--------------------------------------------------------------------------
573577
// Updating children. To be implemented by the concrete subclass.

packages/core/src/VNodes/AtomicNode.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class AtomicNode extends AbstractNode {
2323
*/
2424
children<T extends VNode>(predicate?: Predicate<T>): VNode[];
2525
children(predicate?: Predicate): VNode[];
26-
children<T>(predicate?: Predicate<T>): VNode[] {
26+
children(predicate?: Predicate): VNode[] {
2727
return [];
2828
}
2929
/**
@@ -48,8 +48,8 @@ export class AtomicNode extends AbstractNode {
4848
* @return Returns `undefined` since an atomic node cannot have children.
4949
*/
5050
firstChild<T extends VNode>(predicate?: Predicate<T>): undefined;
51-
firstChild<T>(predicate?: Predicate<T>): undefined;
52-
firstChild<T>(predicate?: Predicate<T>): undefined {
51+
firstChild(predicate?: Predicate): undefined;
52+
firstChild(predicate?: Predicate): undefined {
5353
return undefined;
5454
}
5555
/**
@@ -58,8 +58,8 @@ export class AtomicNode extends AbstractNode {
5858
* @return Returns `undefined` since an atomic node cannot have children.
5959
*/
6060
lastChild<T extends VNode>(predicate?: Predicate<T>): undefined;
61-
lastChild<T>(predicate?: Predicate<T>): undefined;
62-
lastChild<T>(predicate?: Predicate<T>): undefined {
61+
lastChild(predicate?: Predicate): undefined;
62+
lastChild(predicate?: Predicate): undefined {
6363
return undefined;
6464
}
6565
/**
@@ -68,8 +68,8 @@ export class AtomicNode extends AbstractNode {
6868
* @return Returns `this` since an atomic node cannot have children.
6969
*/
7070
firstLeaf<T extends VNode>(predicate?: Predicate<T>): this;
71-
firstLeaf<T>(predicate?: Predicate<T>): this;
72-
firstLeaf<T>(predicate?: Predicate<T>): this {
71+
firstLeaf(predicate?: Predicate): this;
72+
firstLeaf(predicate?: Predicate): this {
7373
return this;
7474
}
7575
/**
@@ -78,8 +78,8 @@ export class AtomicNode extends AbstractNode {
7878
* @return Returns `this` since an atomic node cannot have children.
7979
*/
8080
lastLeaf<T extends VNode>(predicate?: Predicate<T>): this;
81-
lastLeaf<T>(predicate?: Predicate<T>): this;
82-
lastLeaf<T>(predicate?: Predicate<T>): this {
81+
lastLeaf(predicate?: Predicate): this;
82+
lastLeaf(predicate?: Predicate): this {
8383
return this;
8484
}
8585
/**
@@ -88,8 +88,8 @@ export class AtomicNode extends AbstractNode {
8888
* @return Returns `undefined` since an atomic node cannot have children.
8989
*/
9090
firstDescendant<T extends VNode>(predicate?: Predicate<T>): undefined;
91-
firstDescendant<T>(predicate?: Predicate<T>): undefined;
92-
firstDescendant<T>(predicate?: Predicate<T>): undefined {
91+
firstDescendant(predicate?: Predicate): undefined;
92+
firstDescendant(predicate?: Predicate): undefined {
9393
return undefined;
9494
}
9595
/**
@@ -98,8 +98,8 @@ export class AtomicNode extends AbstractNode {
9898
* @return Returns `undefined` since an atomic node cannot have children.
9999
*/
100100
lastDescendant<T extends VNode>(predicate?: Predicate<T>): undefined;
101-
lastDescendant<T>(predicate?: Predicate<T>): undefined;
102-
lastDescendant<T>(predicate?: Predicate<T>): undefined {
101+
lastDescendant(predicate?: Predicate): undefined;
102+
lastDescendant(predicate?: Predicate): undefined {
103103
return undefined;
104104
}
105105
/**
@@ -108,8 +108,8 @@ export class AtomicNode extends AbstractNode {
108108
* @return Returns an empty array since an atomic node cannot have children.
109109
*/
110110
descendants<T extends VNode>(predicate?: Predicate<T>): VNode[];
111-
descendants<T>(predicate?: Predicate<T>): VNode[];
112-
descendants<T>(predicate?: Predicate<T>): VNode[] {
111+
descendants(predicate?: Predicate): VNode[];
112+
descendants(predicate?: Predicate): VNode[] {
113113
return [];
114114
}
115115

0 commit comments

Comments
 (0)