Mirror: The magical sticky regex-based parser generator 🧙

Add transform exceptions for string literals

Changed files
+26 -1
src
+17
src/babel/__snapshots__/plugin.test.js.snap
···
exports[`deduplicates hoisted expressions 1`] = `
"import { match, __private } from \\"reghex\\";
const re = /1/;
+
const str = '1';
var _re_expression = __private.pattern(re);
···
var x;
if (x = __private.exec(state, _re_expression)) {
+
node.push(x);
+
} else {
+
state.y = y1;
+
state.x = x1;
+
return;
+
}
+
+
if (x = __private.exec(state, str)) {
node.push(x);
} else {
state.y = y1;
···
var x;
if (x = __private.exec(state, _re_expression)) {
+
node.push(x);
+
} else {
+
state.y = y1;
+
state.x = x1;
+
return;
+
}
+
+
if (x = __private.exec(state, \\"2\\")) {
node.push(x);
} else {
state.y = y1;
+4 -1
src/babel/plugin.test.js
···
).toMatchSnapshot();
});
-
it('deduplicates hoisted expressions', () => {
+
it.only('deduplicates hoisted expressions', () => {
const code = `
import { match } from 'reghex/macro';
const re = /1/;
+
const str = '1';
const a = match('a')\`
\${re}
+
\${str}
\`;
const b = match('b')\`
\${re}
+
\${'2'}
\`;
`;
+5
src/babel/transform.js
···
t.isIdentifier(expression.body.body[0].argument)
) {
expression = expression.body.body[0].argument;
+
} else if (t.isStringLiteral(expression)) {
+
return expression;
}
const isBindingExpression =
···
if (t.isVariableDeclarator(binding.path.node)) {
const matchPath = binding.path.get('init');
if (this.isMatch(matchPath)) {
+
return expression;
+
} else if (t.isStringLiteral(matchPath)) {
return expression;
} else if (_hoistedExpressions.has(expression.name)) {
return t.identifier(_hoistedExpressions.get(expression.name));
···
if (t.isIdentifier(expression)) {
_hoistedExpressions.set(expression.name, id.name);
}
+
return id;
}
);