forked from dsherret/ts-morph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringLiteral.ts
38 lines (35 loc) · 1.13 KB
/
StringLiteral.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
import { replaceNodeText } from "../../../manipulation";
import { ts } from "../../../typescript";
import { StringUtils } from "../../../utils";
import { LiteralExpression } from "../expression";
import { QuoteKind } from "./QuoteKind";
export const StringLiteralBase = LiteralExpression;
export class StringLiteral extends StringLiteralBase<ts.StringLiteral> {
/**
* Gets the literal value.
*
* This is equivalent to .getLiteralText() for string literals and only exists for consistency with other literals.
*/
getLiteralValue() {
return this.compilerNode.text;
}
/**
* Sets the literal value.
* @param value - Value to set.
*/
setLiteralValue(value: string) {
replaceNodeText({
sourceFile: this._sourceFile,
start: this.getStart() + 1,
replacingLength: this.getWidth() - 2,
newText: StringUtils.escapeForWithinString(value, this.getQuoteKind())
});
return this;
}
/**
* Gets the quote kind.
*/
getQuoteKind() {
return this.getText()[0] === "'" ? QuoteKind.Single : QuoteKind.Double;
}
}