Mirror: The magical sticky regex-based parser generator 🧙

Improve codegen.js output for astExpression()

+12 -6
+2 -2
src/babel/__snapshots__/plugin.test.js.snap
··· 185 185 break alt_3; 186 186 } 187 187 188 - if ((x = _node_expression3(state)) != null) {} else { 188 + if ((x = _node_expression3(state)) == null) { 189 189 state.y = y1; 190 190 state.x = x1; 191 191 node.length = ln2; ··· 196 196 var y3 = state.y, 197 197 x3 = state.x; 198 198 199 - if ((x = _node_expression3(state)) != null) {} else { 199 + if ((x = _node_expression3(state)) == null) { 200 200 state.y = y3; 201 201 state.x = x3; 202 202 break loop_3;
+10 -4
src/codegen.js
··· 30 30 const restoreLength = 31 31 (opts.length && opts.abort && js`${_node}.length = ln${opts.length};`) || 32 32 ''; 33 - const expression = `${ast.expression.id}(${_state})`; 33 + const condition = `(${_match} = ${ast.expression.id}(${_state})) ${ 34 + capture ? '!=' : '==' 35 + } null`; 34 36 return js` 35 - if ((${_match} = ${ast.expression.id}(${_state})) != null) { 36 - ${capture ? js`${_node}.push(${_match})` : ''} 37 - } else { 37 + if (${condition}) ${ 38 + capture 39 + ? js`{ 40 + ${_node}.push(${_match}); 41 + } else ` 42 + : '' 43 + }{ 38 44 ${restoreIndex(opts.index)} 39 45 ${restoreLength} 40 46 ${opts.abort}