-
Notifications
You must be signed in to change notification settings - Fork 13
/
collectionExtensions.ts
134 lines (121 loc) · 4.39 KB
/
collectionExtensions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { AstNode, NamedType, NodePath, Printable } from 'ast-types';
import * as astTypes from 'ast-types';
import { Collection, JsCodeShift } from 'jscodeshift';
import * as jscodeshift from 'jscodeshift';
function isPositionWithinNode(position: number, node: Printable) {
return node.start <= position && position <= node.end;
}
function createCollection<TNode>(
nodeOrPathOrArray: TNode | NodePath<TNode> | TNode[] | Array<NodePath<TNode>>
) {
return (jscodeshift as any)(nodeOrPathOrArray);
}
export function registerCollectionExtensions(j: JsCodeShift) {
j.registerMethods({
firstNode<TNode>(this: Collection<TNode>): TNode | null {
return this.nodes()[0] || null;
},
firstPath<TNode>(this: Collection<TNode>): NodePath<TNode> | null {
return this.paths()[0] || null;
},
parents<TNode>(this: Collection<TNode>): AstNode[] {
return this.thisAndParents().slice(1);
},
thisAndParents<TNode extends AstNode>(this: Collection<TNode>): AstNode[] {
if (this.length > 1) {
throw new Error('The operation is only available for single-node collections.');
}
const path = this.firstPath();
if (!path) {
return [];
}
const result: AstNode[] = [path.node];
let parent = path.parent;
while (parent) {
result.push(parent.node);
parent = parent.parent;
}
return result;
},
findNodeAtPosition<TNode extends AstNode>(
this: Collection<TNode>,
pos: number
): Collection {
const path = this.firstPath();
if (!path) {
return createCollection([]);
}
let targets: Array<NodePath<AstNode>> = [];
let r = astTypes.visit(path, {
visitNode(p: NodePath<AstNode>) {
if (isPositionWithinNode(pos, p.node as Printable)) {
targets.push(p);
this.traverse(p);
return;
} else {
return false;
}
}
});
const last = targets[targets.length - 1];
return createCollection(last || []);
},
findNodeInRange<TNode extends AstNode>(
this: Collection<TNode>,
start: number,
end: number
): Collection {
if (start > end) {
[start, end] = [end, start];
}
const path = this.firstPath();
if (!path) {
return createCollection([]);
}
let targets: Array<NodePath<AstNode>> = [];
let r = astTypes.visit(path, {
visitNode(p: NodePath<AstNode>) {
const n = p.node as Printable;
if (n.start <= start && end <= n.end) {
targets.push(p);
this.traverse(p);
return;
} else {
return false;
}
}
});
const last = targets[targets.length - 1];
return createCollection(last || []);
},
thisOrClosest<TNode>(
this: Collection<TNode>,
type: NamedType<TNode>,
filter?: any
): Collection {
return this.map(path => {
let target = path as NodePath<any>;
while (
target &&
!(type.check(target.value) && (!filter || j.match(target.value, filter)))
) {
target = target.parent;
}
return target || null;
});
},
furthest<TNode>(this: Collection<TNode>, type: NamedType<TNode>, filter?: any): Collection {
return this.map(path => {
let furthestPath = null;
let target = path.parent;
while (target) {
if (type.check(target.value) && (!filter || j.match(target.value, filter))) {
furthestPath = target;
}
target = target.parent;
}
return furthestPath;
});
}
});
}