-
Notifications
You must be signed in to change notification settings - Fork 189
/
NodeHandlerHelper.ts
65 lines (57 loc) · 3.17 KB
/
NodeHandlerHelper.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
import { ArrayUtils, SyntaxKind, ts } from "@ts-morph/common";
import { Node, SourceFile } from "../../compiler";
import { ExtendedParser, hasParsedTokens } from "../../compiler/ast/utils";
import { CompilerFactory } from "../../factories";
import { AdvancedIterator } from "../../utils";
import { NodeHandler } from "./NodeHandler";
export class NodeHandlerHelper {
constructor(private readonly compilerFactory: CompilerFactory) {
}
handleForValues(handler: NodeHandler, currentNode: ts.Node, newNode: ts.Node, newSourceFile: ts.SourceFile) {
if (this.compilerFactory.hasCompilerNode(currentNode))
handler.handleNode(this.compilerFactory.getExistingNodeFromCompilerNode(currentNode)!, newNode, newSourceFile);
else if (currentNode.kind === SyntaxKind.SyntaxList) {
// always handle syntax lists because their children might be in the cache
// todo: pass this in for performance reasons
const sourceFile = this.compilerFactory.getExistingNodeFromCompilerNode(currentNode.getSourceFile())! as SourceFile;
handler.handleNode(this.compilerFactory.getNodeFromCompilerNode(currentNode, sourceFile), newNode, newSourceFile);
}
}
forgetNodeIfNecessary(currentNode: ts.Node) {
if (this.compilerFactory.hasCompilerNode(currentNode))
this.compilerFactory.getExistingNodeFromCompilerNode(currentNode)!.forget();
}
getCompilerChildrenAsIterators(currentNode: Node, newNode: ts.Node, newSourceFile: ts.SourceFile): [AdvancedIterator<ts.Node>, AdvancedIterator<ts.Node>] {
const children = this.getCompilerChildren(currentNode, newNode, newSourceFile);
return [
new AdvancedIterator(ArrayUtils.toIterator(children[0])),
new AdvancedIterator(ArrayUtils.toIterator(children[1]))
];
}
getCompilerChildren(currentNode: Node, newNode: ts.Node, newSourceFile: ts.SourceFile): [ts.Node[], ts.Node[]] {
const currentCompilerNode = currentNode.compilerNode;
const currentSourceFile = currentNode._sourceFile.compilerNode;
return [
ExtendedParser.getCompilerChildren(currentCompilerNode, currentSourceFile),
ExtendedParser.getCompilerChildren(newNode, newSourceFile)
];
}
/**
* Gets the children of the node according to whether the tokens have previously been parsed.
*/
getChildrenFast(currentNode: Node, newNode: ts.Node, newSourceFile: ts.SourceFile): [ts.Node[], ts.Node[]] {
const currentCompilerNode = currentNode.compilerNode;
const currentSourceFile = currentNode._sourceFile.compilerNode;
if (hasParsedTokens(currentCompilerNode)) {
return [
ExtendedParser.getCompilerChildren(currentCompilerNode, currentSourceFile),
ExtendedParser.getCompilerChildren(newNode, newSourceFile)
];
}
// great, we don't have to parse the tokens and can instead just use ts.forEachChild (faster)
return [
ExtendedParser.getCompilerForEachChildren(currentCompilerNode, currentSourceFile),
ExtendedParser.getCompilerForEachChildren(newNode, newSourceFile)
];
}
}