visitRuleset method

dynamic visitRuleset (Ruleset rulesetNode, VisitArgs visitArgs)

return Node | List

Implementation

dynamic visitRuleset(Ruleset rulesetNode, VisitArgs visitArgs) {
  //at this point rulesets are nested into each other
  final List<dynamic> rulesets = <dynamic>[]; //Node || List<Node>

  // error test for rules at first level, not inside a ruleset ?? TODO if?
  if (rulesetNode.firstRoot) {
    checkValidNodes(rulesetNode.rules, isRoot: rulesetNode.firstRoot);
  }

  if (!rulesetNode.root) {
    //remove invisible paths
    _compileRulesetPaths(rulesetNode);

    // remove rulesets from this ruleset body and compile them separately
    final List<Node> nodeRules = rulesetNode.rules;
    int nodeRuleCnt = nodeRules?.length ?? 0;

    for (int i = 0; i < nodeRuleCnt;) {
      final Node rule = nodeRules[i];
      if (rule?.rules != null) {
        // visit because we are moving them out from being a child
        rulesets.add(_visitor.visit(rule));
        nodeRules.removeAt(i);
        nodeRuleCnt--;
        continue;
      }
      i++;
    }

    // accept the visitor to remove rules and refactor itself
    // then we can decide now whether we want it or not
    // compile body
    if (nodeRuleCnt > 0) {
      rulesetNode.accept(_visitor);
    } else {
      rulesetNode.rules = null;
    }
    visitArgs.visitDeeper = false;
  } else {
    //if (! rulesetNode.root)
    rulesetNode.accept(_visitor);
    visitArgs.visitDeeper = false;
  }

  if (rulesetNode.rules != null) {
    mergeRules(rulesetNode.rules);
    _removeDuplicateRules(rulesetNode.rules);
  }

  //now decide whether we keep the ruleset
  if (utils.isVisibleRuleset(rulesetNode)) {
    rulesetNode.ensureVisibility();
    rulesets.insert(0, rulesetNode);
  }

  return (rulesets.length == 1) ? rulesets.first : rulesets;

//2.6.1 20160305
// visitRuleset: function (rulesetNode, visitArgs) {
//     //at this point rulesets are nested into each other
//     var rule, rulesets = [];
//
//     this.checkValidNodes(rulesetNode.rules, rulesetNode.firstRoot);
//
//     if (! rulesetNode.root) {
//         //remove invisible paths
//         this._compileRulesetPaths(rulesetNode);
//
//         // remove rulesets from this ruleset body and compile them separately
//         var nodeRules = rulesetNode.rules, nodeRuleCnt = nodeRules ? nodeRules.length : 0;
//         for (var i = 0; i < nodeRuleCnt; ) {
//             rule = nodeRules[i];
//             if (rule && rule.rules) {
//                 // visit because we are moving them out from being a child
//                 rulesets.push(this._visitor.visit(rule));
//                 nodeRules.splice(i, 1);
//                 nodeRuleCnt--;
//                 continue;
//             }
//             i++;
//         }
//         // accept the visitor to remove rules and refactor itself
//         // then we can decide nogw whether we want it or not
//         // compile body
//         if (nodeRuleCnt > 0) {
//             rulesetNode.accept(this._visitor);
//         } else {
//             rulesetNode.rules = null;
//         }
//         visitArgs.visitDeeper = false;
//
//     } else { //if (! rulesetNode.root) {
//         rulesetNode.accept(this._visitor);
//         visitArgs.visitDeeper = false;
//     }
//
//     if (rulesetNode.rules) {
//         this._mergeRules(rulesetNode.rules);
//         this._removeDuplicateRules(rulesetNode.rules);
//     }
//
//     //now decide whether we keep the ruleset
//     if (this.utils.isVisibleRuleset(rulesetNode)) {
//         rulesetNode.ensureVisibility();
//         rulesets.splice(0, 0, rulesetNode);
//     }
//
//     if (rulesets.length === 1) {
//         return rulesets[0];
//     }
//     return rulesets;
// },
}