forked from dsherret/ts-morph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegularExpressionLiteral.ts
46 lines (42 loc) · 1.48 KB
/
RegularExpressionLiteral.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
import { replaceNodeText } from "../../../manipulation";
import { ts } from "../../../typescript";
import { LiteralExpression } from "../expression";
export const RegularExpressionLiteralBase = LiteralExpression;
export class RegularExpressionLiteral extends RegularExpressionLiteralBase<ts.RegularExpressionLiteral> {
/**
* Gets the literal value.
*/
getLiteralValue(): RegExp {
const pattern = /^\/(.*)\/([^\/]*)$/;
const text = this.compilerNode.text;
const matches = pattern.exec(text)!;
return new RegExp(matches[1], matches[2]);
}
/**
* Sets the literal value according to a pattern and some flags.
* @param pattern - Pattern.
* @param flags - Flags.
*/
setLiteralValue(pattern: string, flags?: string): this;
/**
* Sets the literal value according to a regular expression object.
* @param regExp - Regular expression.
*/
setLiteralValue(regExp: RegExp): this;
setLiteralValue(regExpOrPattern: RegExp | string, flags?: string) {
let pattern: string;
if (typeof regExpOrPattern === "string")
pattern = regExpOrPattern;
else {
pattern = regExpOrPattern.source;
flags = regExpOrPattern.flags;
}
replaceNodeText({
sourceFile: this._sourceFile,
start: this.getStart(),
replacingLength: this.getWidth(),
newText: `/${pattern}/${flags || ""}`
});
return this;
}
}