tangled
alpha
login
or
join now
kitten.sh
/
reghex
0
fork
atom
Mirror: The magical sticky regex-based parser generator 🧙
0
fork
atom
overview
issues
pulls
pipelines
Add transform exceptions for string literals
kitten.sh
5 years ago
4ed3dbf2
345306b5
+26
-1
3 changed files
expand all
collapse all
unified
split
src
babel
__snapshots__
plugin.test.js.snap
plugin.test.js
transform.js
+17
src/babel/__snapshots__/plugin.test.js.snap
···
3
3
exports[`deduplicates hoisted expressions 1`] = `
4
4
"import { match, __private } from \\"reghex\\";
5
5
const re = /1/;
6
6
+
const str = '1';
6
7
7
8
var _re_expression = __private.pattern(re);
8
9
···
13
14
var x;
14
15
15
16
if (x = __private.exec(state, _re_expression)) {
17
17
+
node.push(x);
18
18
+
} else {
19
19
+
state.y = y1;
20
20
+
state.x = x1;
21
21
+
return;
22
22
+
}
23
23
+
24
24
+
if (x = __private.exec(state, str)) {
16
25
node.push(x);
17
26
} else {
18
27
state.y = y1;
···
31
40
var x;
32
41
33
42
if (x = __private.exec(state, _re_expression)) {
43
43
+
node.push(x);
44
44
+
} else {
45
45
+
state.y = y1;
46
46
+
state.x = x1;
47
47
+
return;
48
48
+
}
49
49
+
50
50
+
if (x = __private.exec(state, \\"2\\")) {
34
51
node.push(x);
35
52
} else {
36
53
state.y = y1;
+4
-1
src/babel/plugin.test.js
···
34
34
).toMatchSnapshot();
35
35
});
36
36
37
37
-
it('deduplicates hoisted expressions', () => {
37
37
+
it.only('deduplicates hoisted expressions', () => {
38
38
const code = `
39
39
import { match } from 'reghex/macro';
40
40
41
41
const re = /1/;
42
42
+
const str = '1';
42
43
43
44
const a = match('a')\`
44
45
\${re}
46
46
+
\${str}
45
47
\`;
46
48
47
49
const b = match('b')\`
48
50
\${re}
51
51
+
\${'2'}
49
52
\`;
50
53
`;
51
54
+5
src/babel/transform.js
···
116
116
t.isIdentifier(expression.body.body[0].argument)
117
117
) {
118
118
expression = expression.body.body[0].argument;
119
119
+
} else if (t.isStringLiteral(expression)) {
120
120
+
return expression;
119
121
}
120
122
121
123
const isBindingExpression =
···
126
128
if (t.isVariableDeclarator(binding.path.node)) {
127
129
const matchPath = binding.path.get('init');
128
130
if (this.isMatch(matchPath)) {
131
131
+
return expression;
132
132
+
} else if (t.isStringLiteral(matchPath)) {
129
133
return expression;
130
134
} else if (_hoistedExpressions.has(expression.name)) {
131
135
return t.identifier(_hoistedExpressions.get(expression.name));
···
149
153
if (t.isIdentifier(expression)) {
150
154
_hoistedExpressions.set(expression.name, id.name);
151
155
}
156
156
+
152
157
return id;
153
158
}
154
159
);