forked from dsherret/ts-morph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumericLiteral.ts
30 lines (28 loc) · 870 Bytes
/
NumericLiteral.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
import { replaceNodeText } from "../../../manipulation";
import { ts } from "../../../typescript";
import { LiteralExpression } from "../expression";
export const NumericLiteralBase = LiteralExpression;
export class NumericLiteral extends NumericLiteralBase<ts.NumericLiteral> {
/**
* Gets the literal value.
*/
getLiteralValue(): number {
const text = this.compilerNode.text;
if (text.indexOf(".") >= 0)
return parseFloat(text);
return parseInt(text, 10);
}
/**
* Sets the literal value.
* @param value - Value to set.
*/
setLiteralValue(value: number) {
replaceNodeText({
sourceFile: this._sourceFile,
start: this.getStart(),
replacingLength: this.getWidth(),
newText: value.toString(10)
});
return this;
}
}