Refactor removeProxyRules

master
Futago-za Ryuu 6 years ago
parent dbdba75b15
commit af917b1e4d

@ -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;
}

Loading…
Cancel
Save