forked from dsherret/ts-morph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVariableDeclaration.ts
83 lines (75 loc) · 3.4 KB
/
VariableDeclaration.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
import * as errors from "../../../errors";
import { removeChildren, removeCommaSeparatedChild } from "../../../manipulation";
import { VariableDeclarationStructure, VariableDeclarationSpecificStructure, StructureKind } from "../../../structures";
import { TypeGuards } from "../../../utils";
import { SyntaxKind, ts } from "../../../typescript";
import { BindingNamedNode, ExclamationTokenableNode, InitializerExpressionableNode, TypedNode, ExportGetableNode } from "../base";
import { callBaseSet } from "../callBaseSet";
import { Node } from "../common";
import { callBaseGetStructure } from "../callBaseGetStructure";
export const VariableDeclarationBase = ExportGetableNode(ExclamationTokenableNode(TypedNode(InitializerExpressionableNode(BindingNamedNode(Node)))));
export class VariableDeclaration extends VariableDeclarationBase<ts.VariableDeclaration> {
/**
* Removes this variable declaration.
*/
remove() {
const parent = this.getParentOrThrow();
switch (parent.getKind()) {
case SyntaxKind.VariableDeclarationList:
removeFromDeclarationList(this);
break;
case SyntaxKind.CatchClause:
removeFromCatchClause(this);
break;
default:
throw new errors.NotImplementedError(`Not implemented for syntax kind: ${parent.getKindName()}`);
}
function removeFromDeclarationList(node: VariableDeclaration) {
const variableStatement = parent.getParentIfKindOrThrow(SyntaxKind.VariableStatement);
const declarations = variableStatement.getDeclarations();
if (declarations.length === 1)
variableStatement.remove();
else
removeCommaSeparatedChild(node);
}
function removeFromCatchClause(node: VariableDeclaration) {
removeChildren({
children: [
node.getPreviousSiblingIfKindOrThrow(SyntaxKind.OpenParenToken),
node,
node.getNextSiblingIfKindOrThrow(SyntaxKind.CloseParenToken)
],
removePrecedingSpaces: true
});
}
}
/**
* Gets the corresponding variable statement if it exists. Throws for variable declarations in for statements.
*/
getVariableStatementOrThrow() {
return errors.throwIfNullOrUndefined(this.getVariableStatement(), "Expected the grandparent to be a variable statement.");
}
/**
* Gets the corresponding variable statement if it exists. Returns undefined for variable declarations in for statements.
*/
getVariableStatement() {
const grandParent = this.getParentOrThrow().getParentOrThrow();
return TypeGuards.isVariableStatement(grandParent) ? grandParent : undefined;
}
/**
* Sets the node from a structure.
* @param structure - Structure to set the node with.
*/
set(structure: Partial<VariableDeclarationStructure>) {
callBaseSet(VariableDeclarationBase.prototype, this, structure);
return this;
}
/**
* Gets the structure equivalent to this node.
*/
getStructure(): VariableDeclarationStructure {
return callBaseGetStructure<VariableDeclarationSpecificStructure>(VariableDeclarationBase.prototype, this, {
kind: StructureKind.VariableDeclaration
}) as any as VariableDeclarationStructure;
}
}