forked from dsherret/ts-morph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShorthandPropertyAssignment.ts
123 lines (105 loc) · 4.44 KB
/
ShorthandPropertyAssignment.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
import * as errors from "../../../../errors";
import { insertIntoParentTextRange, removeChildren, removeCommaSeparatedChild } from "../../../../manipulation";
import { SyntaxKind, ts } from "../../../../typescript";
import { InitializerGetExpressionableNode, NamedNode, QuestionTokenableNode } from "../../base";
import { Node } from "../../common/Node";
import { Expression } from "../Expression";
import { PropertyAssignment } from "./PropertyAssignment";
import { ShorthandPropertyAssignmentStructure, ShorthandPropertyAssignmentSpecificStructure, QuestionTokenableNodeStructure } from "../../../../structures";
import { callBaseGetStructure } from "../../callBaseGetStructure";
import { callBaseSet } from "../../callBaseSet";
// This node only has an object assignment initializer, equals token, and question token, in order to tell the user about bad code
// (See https://github.com/Microsoft/TypeScript/pull/5121/files)
export const ShorthandPropertyAssignmentBase = InitializerGetExpressionableNode(QuestionTokenableNode(NamedNode(Node)));
export class ShorthandPropertyAssignment extends ShorthandPropertyAssignmentBase<ts.ShorthandPropertyAssignment> {
/**
* Gets if the shorthand property assignment has an object assignment initializer.
*/
hasObjectAssignmentInitializer() {
return this.compilerNode.objectAssignmentInitializer != null;
}
/**
* Gets the object assignment initializer or throws if it doesn't exist.
*/
getObjectAssignmentInitializerOrThrow() {
return errors.throwIfNullOrUndefined(this.getObjectAssignmentInitializer(), "Expected to find an object assignment initializer.");
}
/**
* Gets the object assignment initializer if it exists.
*/
getObjectAssignmentInitializer(): Expression | undefined {
return this._getNodeFromCompilerNodeIfExists(this.compilerNode.objectAssignmentInitializer);
}
/**
* Gets the equals token or throws if it doesn't exist.
*/
getEqualsTokenOrThrow() {
return errors.throwIfNullOrUndefined(this.getEqualsToken(), "Expected to find an equals token.");
}
/**
* Gets the equals token if it exists.
*/
getEqualsToken() {
const equalsToken = this.compilerNode.equalsToken;
if (equalsToken == null)
return undefined;
return this._getNodeFromCompilerNode(equalsToken);
}
/**
* Remove the object assignment initializer.
*
* This is only useful to remove bad code.
*/
removeObjectAssignmentInitializer() {
if (!this.hasObjectAssignmentInitializer())
return this;
removeChildren({
children: [this.getEqualsTokenOrThrow(), this.getObjectAssignmentInitializerOrThrow()],
removePrecedingSpaces: true
});
return this;
}
/**
* Sets the initializer.
*
* Note: The current node will no longer be valid because it's no longer a shorthand property assignment.
* @param text - New text to set for the initializer.
*/
setInitializer(text: string): PropertyAssignment {
const parent = this.getParentSyntaxList() || this.getParentOrThrow();
const childIndex = this.getChildIndex();
insertIntoParentTextRange({
insertPos: this.getStart(),
newText: this.getText() + `: ${text}`,
parent,
replacing: {
textLength: this.getWidth()
}
});
return parent.getChildAtIndexIfKindOrThrow(childIndex, SyntaxKind.PropertyAssignment) as PropertyAssignment;
}
/**
* Removes this property.
*/
remove() {
removeCommaSeparatedChild(this);
}
/**
* Sets the node from a structure.
* @param structure - Structure to set the node with.
*/
set(structure: Partial<ShorthandPropertyAssignmentStructure>) {
callBaseSet(ShorthandPropertyAssignmentBase.prototype, this, structure);
return this;
}
/**
* Gets the structure equivalent to this node.
*/
getStructure(): ShorthandPropertyAssignmentStructure {
const structure = callBaseGetStructure<ShorthandPropertyAssignmentSpecificStructure>(ShorthandPropertyAssignmentBase.prototype, this, {
}) as any as ShorthandPropertyAssignmentStructure;
// remove since this is only used to tell the user about incorrect code
delete (structure as QuestionTokenableNodeStructure).hasQuestionToken;
return structure;
}
}