From af917b1e4d719f25d04e5443e676779afd5f5ce5 Mon Sep 17 00:00:00 2001 From: Futago-za Ryuu Date: Thu, 29 Mar 2018 03:55:50 +0100 Subject: [PATCH] Refactor removeProxyRules --- lib/compiler/passes/remove-proxy-rules.js | 39 +++++++---------------- 1 file changed, 12 insertions(+), 27 deletions(-) diff --git a/lib/compiler/passes/remove-proxy-rules.js b/lib/compiler/passes/remove-proxy-rules.js index 690dc08..d40b3b0 100644 --- a/lib/compiler/passes/remove-proxy-rules.js +++ b/lib/compiler/passes/remove-proxy-rules.js @@ -9,48 +9,33 @@ function removeProxyRules( ast, session, options ) { } - function replaceRuleRefs( ast, from, to ) { + const replaceRuleRefs = session.buildVisitor( { - const replace = session.buildVisitor( { - rule_ref( node ) { + rule_ref( node, proxy, real ) { - if ( node.name === from ) { + if ( node.name === proxy ) node.name = real; - node.name = to; - - } - - } - } ); - - replace( ast ); + } - } + } ); - const indices = []; + const allowedStartRules = options.allowedStartRules; + const rules = []; - ast.rules.forEach( ( rule, i ) => { + for ( const rule of ast.rules ) { if ( isProxyRule( rule ) ) { replaceRuleRefs( ast, rule.name, rule.expression.name ); - if ( options.allowedStartRules.indexOf( rule.name ) === -1 ) { - - indices.push( i ); - - } + if ( allowedStartRules.indexOf( rule.name ) < 0 ) continue; } - } ); - - indices.reverse(); + rules.push( rule ); - indices.forEach( i => { - - ast.rules.splice( i, 1 ); + } - } ); + ast.rules = rules; }