forked from dsherret/ts-morph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPropertyAssignment.ts
91 lines (79 loc) · 3.83 KB
/
PropertyAssignment.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
import { insertIntoParentTextRange } from "../../../../manipulation";
import { WriterFunction } from "../../../../types";
import { SyntaxKind, ts } from "../../../../typescript";
import { getTextFromStringOrWriter } from "../../../../utils";
import { InitializerExpressionGetableNode, PropertyNamedNode, QuestionTokenableNode } from "../../base";
import { ShorthandPropertyAssignment } from "./ShorthandPropertyAssignment";
import { PropertyAssignmentStructure, PropertyAssignmentSpecificStructure, StructureKind } from "../../../../structures";
import { callBaseSet } from "../../callBaseSet";
import { callBaseGetStructure } from "../../callBaseGetStructure";
import { ObjectLiteralElement } from "./ObjectLiteralElement";
// This node only has a question token in order to tell the user about bad code.
// (See https://github.com/Microsoft/TypeScript/pull/5121/files)
export const PropertyAssignmentBase = InitializerExpressionGetableNode(QuestionTokenableNode(PropertyNamedNode(ObjectLiteralElement)));
export class PropertyAssignment extends PropertyAssignmentBase<ts.PropertyAssignment> {
/**
* Removes the initializer and returns the new shorthand property assignment.
*
* Note: The current node will no longer be valid because it's no longer a property assignment.
*/
removeInitializer(): ShorthandPropertyAssignment {
const initializer = this.getInitializerOrThrow();
const colonToken = initializer.getPreviousSiblingIfKindOrThrow(SyntaxKind.ColonToken);
const childIndex = this.getChildIndex();
const sourceFileText = this._sourceFile.getFullText();
const insertPos = this.getStart();
const newText = sourceFileText.substring(insertPos, colonToken.getPos()) + sourceFileText.substring(initializer.getEnd(), this.getEnd());
const parent = this.getParentSyntaxList() || this.getParentOrThrow();
insertIntoParentTextRange({
insertPos,
newText,
parent,
replacing: {
textLength: this.getWidth()
}
});
return parent.getChildAtIndexIfKindOrThrow(childIndex, SyntaxKind.ShorthandPropertyAssignment) as ShorthandPropertyAssignment;
}
/**
* Sets the initializer.
* @param textOrWriterFunction - New text ot set for the initializer.
*/
setInitializer(textOrWriterFunction: string | WriterFunction): this {
const initializer = this.getInitializerOrThrow();
insertIntoParentTextRange({
insertPos: initializer.getStart(),
newText: getTextFromStringOrWriter(this._getWriterWithQueuedChildIndentation(), textOrWriterFunction),
parent: this,
replacing: {
textLength: initializer.getWidth()
}
});
return this;
}
/**
* Sets the node from a structure.
* @param structure - Structure to set the node with.
*/
set(structure: Partial<PropertyAssignmentStructure>) {
callBaseSet(PropertyAssignmentBase.prototype, this, structure);
if (structure.initializer != null)
this.setInitializer(structure.initializer);
else if (structure.hasOwnProperty(nameof(structure.initializer)))
return this.removeInitializer();
return this;
}
/**
* Gets the structure equivalent to this node.
*/
getStructure() {
const initializer = this.getInitializerOrThrow();
const structure = callBaseGetStructure<PropertyAssignmentSpecificStructure>(PropertyAssignmentBase.prototype, this, {
kind: StructureKind.PropertyAssignment,
initializer: initializer.getText()
}) as any as PropertyAssignmentStructure;
// only has a question token for bad code. Don't include it in the structure.
delete (structure as any).hasQuestionToken;
return structure;
}
}