Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Commit 31ad3c4

Browse files
soda0289JamesHenry
authored andcommitted
Fix: Create RegExp object for RegExp literals (fixes #287) (#291)
1 parent 525a544 commit 31ad3c4

10 files changed

+812
-5
lines changed

lib/convert.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,17 +1507,28 @@ module.exports = function convert(config) {
15071507
});
15081508
break;
15091509

1510-
case SyntaxKind.RegularExpressionLiteral:
1510+
case SyntaxKind.RegularExpressionLiteral: {
1511+
const pattern = node.text.slice(1, node.text.lastIndexOf("/"));
1512+
const flags = node.text.slice(node.text.lastIndexOf("/") + 1);
1513+
1514+
let regex = null;
1515+
try {
1516+
regex = new RegExp(pattern, flags);
1517+
} catch (exception) {
1518+
regex = null;
1519+
}
1520+
15111521
Object.assign(result, {
15121522
type: AST_NODE_TYPES.Literal,
1513-
value: Number(node.text),
1523+
value: regex,
15141524
raw: node.text,
15151525
regex: {
1516-
pattern: node.text.slice(1, node.text.lastIndexOf("/")),
1517-
flags: node.text.slice(node.text.lastIndexOf("/") + 1)
1526+
pattern,
1527+
flags
15181528
}
15191529
});
15201530
break;
1531+
}
15211532

15221533
case SyntaxKind.TrueKeyword:
15231534
Object.assign(result, {
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
module.exports = {
2+
"type": "Program",
3+
"loc": {
4+
"start": {
5+
"line": 1,
6+
"column": 0
7+
},
8+
"end": {
9+
"line": 1,
10+
"column": 17
11+
}
12+
},
13+
"range": [
14+
0,
15+
17
16+
],
17+
"body": [
18+
{
19+
"type": "VariableDeclaration",
20+
"loc": {
21+
"start": {
22+
"line": 1,
23+
"column": 0
24+
},
25+
"end": {
26+
"line": 1,
27+
"column": 17
28+
}
29+
},
30+
"range": [
31+
0,
32+
17
33+
],
34+
"declarations": [
35+
{
36+
"type": "VariableDeclarator",
37+
"loc": {
38+
"start": {
39+
"line": 1,
40+
"column": 4
41+
},
42+
"end": {
43+
"line": 1,
44+
"column": 16
45+
}
46+
},
47+
"range": [
48+
4,
49+
16
50+
],
51+
"id": {
52+
"type": "Identifier",
53+
"loc": {
54+
"start": {
55+
"line": 1,
56+
"column": 4
57+
},
58+
"end": {
59+
"line": 1,
60+
"column": 7
61+
}
62+
},
63+
"range": [
64+
4,
65+
7
66+
],
67+
"name": "foo"
68+
},
69+
"init": {
70+
"type": "Literal",
71+
"loc": {
72+
"start": {
73+
"line": 1,
74+
"column": 10
75+
},
76+
"end": {
77+
"line": 1,
78+
"column": 16
79+
}
80+
},
81+
"range": [
82+
10,
83+
16
84+
],
85+
"value": {},
86+
"raw": "/foo./",
87+
"regex": {
88+
"pattern": "foo.",
89+
"flags": ""
90+
}
91+
}
92+
}
93+
],
94+
"kind": "var"
95+
}
96+
],
97+
"sourceType": "script",
98+
"tokens": [
99+
{
100+
"type": "Keyword",
101+
"value": "var",
102+
"loc": {
103+
"start": {
104+
"line": 1,
105+
"column": 0
106+
},
107+
"end": {
108+
"line": 1,
109+
"column": 3
110+
}
111+
},
112+
"range": [
113+
0,
114+
3
115+
]
116+
},
117+
{
118+
"type": "Identifier",
119+
"value": "foo",
120+
"loc": {
121+
"start": {
122+
"line": 1,
123+
"column": 4
124+
},
125+
"end": {
126+
"line": 1,
127+
"column": 7
128+
}
129+
},
130+
"range": [
131+
4,
132+
7
133+
]
134+
},
135+
{
136+
"type": "Punctuator",
137+
"value": "=",
138+
"loc": {
139+
"start": {
140+
"line": 1,
141+
"column": 8
142+
},
143+
"end": {
144+
"line": 1,
145+
"column": 9
146+
}
147+
},
148+
"range": [
149+
8,
150+
9
151+
]
152+
},
153+
{
154+
"type": "RegularExpression",
155+
"value": "/foo./",
156+
"loc": {
157+
"start": {
158+
"line": 1,
159+
"column": 10
160+
},
161+
"end": {
162+
"line": 1,
163+
"column": 16
164+
}
165+
},
166+
"range": [
167+
10,
168+
16
169+
],
170+
"regex": {
171+
"flags": "",
172+
"pattern": "foo."
173+
}
174+
},
175+
{
176+
"type": "Punctuator",
177+
"value": ";",
178+
"loc": {
179+
"start": {
180+
"line": 1,
181+
"column": 16
182+
},
183+
"end": {
184+
"line": 1,
185+
"column": 17
186+
}
187+
},
188+
"range": [
189+
16,
190+
17
191+
]
192+
}
193+
]
194+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var foo = /foo./;

0 commit comments

Comments
 (0)