Mirror: The magical sticky regex-based parser generator 🧙

Improve codegen.js output for astExpression()

Changed files
+12 -6
src
babel
__snapshots__
+2 -2
src/babel/__snapshots__/plugin.test.js.snap
···
break alt_3;
}
-
if ((x = _node_expression3(state)) != null) {} else {
+
if ((x = _node_expression3(state)) == null) {
state.y = y1;
state.x = x1;
node.length = ln2;
···
var y3 = state.y,
x3 = state.x;
-
if ((x = _node_expression3(state)) != null) {} else {
+
if ((x = _node_expression3(state)) == null) {
state.y = y3;
state.x = x3;
break loop_3;
+10 -4
src/codegen.js
···
const restoreLength =
(opts.length && opts.abort && js`${_node}.length = ln${opts.length};`) ||
'';
-
const expression = `${ast.expression.id}(${_state})`;
+
const condition = `(${_match} = ${ast.expression.id}(${_state})) ${
+
capture ? '!=' : '=='
+
} null`;
return js`
-
if ((${_match} = ${ast.expression.id}(${_state})) != null) {
-
${capture ? js`${_node}.push(${_match})` : ''}
-
} else {
+
if (${condition}) ${
+
capture
+
? js`{
+
${_node}.push(${_match});
+
} else `
+
: ''
+
}{
${restoreIndex(opts.index)}
${restoreLength}
${opts.abort}