forked from dsherret/ts-morph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TemplateExpression.ts
42 lines (38 loc) · 1.39 KB
/
TemplateExpression.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
import { replaceNodeText } from "../../../../manipulation";
import { ts } from "../../../../typescript";
import { TemplateLiteral } from "../../aliases";
import { PrimaryExpression } from "../../expression";
import { TemplateHead } from "./TemplateHead";
export const TemplateExpressionBase = PrimaryExpression;
export class TemplateExpression extends TemplateExpressionBase<ts.TemplateExpression> {
/**
* Gets the template head.
*/
getHead(): TemplateHead {
return this._getNodeFromCompilerNode(this.compilerNode.head);
}
/**
* Gets the template spans.
*/
getTemplateSpans() {
return this.compilerNode.templateSpans.map(s => this._getNodeFromCompilerNode(s));
}
/**
* Sets the literal value.
*
* Note: This could possibly replace the node if you remove all the tagged templates.
* @param value - Value to set.
* @returns The new node if the kind changed; the current node otherwise.
*/
setLiteralValue(value: string) {
const childIndex = this.getChildIndex();
const parent = this.getParentSyntaxList() || this.getParentOrThrow();
replaceNodeText({
sourceFile: this._sourceFile,
start: this.getStart() + 1,
replacingLength: this.getWidth() - 2,
newText: value
});
return parent.getChildAtIndex(childIndex) as TemplateLiteral;
}
}