evalParams method

Ruleset evalParams (Contexts context, Contexts mixinEnv, List<MixinArgs> args, List<Node> evaldArguments)

/ find: function () { return this.parent.find.apply(this, arguments); }, / rulesets: function () { return this.parent.rulesets.apply(this); },

Build a Ruleset where the rules are the mixin params evaluated Build evaldArguments

Implementation

//  }
//
//  rulesets() {
////    rulesets:  function ()     { return this.parent.rulesets.apply(this); },
//  }

  ///
  /// Build a Ruleset where the rules are the mixin params evaluated
  /// Build evaldArguments
  ///
  Ruleset evalParams(Contexts context, Contexts mixinEnv, List<MixinArgs> args,
      List<Node> evaldArguments) {
    int argIndex;
    int argsLength = 0;
    final Ruleset frame = Ruleset(null, null);
    bool isNamedFound;
    String name;
    final List<MixinArgs> params = this.params.sublist(0);

    //Fill with nulls the list
    evaldArguments.length = math.max(params?.length ?? 0, args?.length ?? 0);

    if ((mixinEnv.frames?.first as VariableMixin)?.functionRegistry != null) {
      frame.functionRegistry = FunctionRegistry.inherit(
          (mixinEnv.frames[0] as VariableMixin).functionRegistry);
    }

    final Contexts _mixinEnv =
        Contexts.eval(mixinEnv, <Node>[frame, ...mixinEnv.frames]);
    final List<MixinArgs> _args = args?.sublist(0);

    if (_args != null) {
      argsLength = _args.length;

      for (int i = 0; i < _args.length; i++) {
        final MixinArgs arg = _args[i];
        name = arg?.name;
        if (name != null) {
          isNamedFound = false;
          for (int j = 0; j < params.length; j++) {
            if (evaldArguments[j] == null && name == params[j].name) {
              evaldArguments[j] = arg.value.eval(context);
              frame.prependRule(Declaration(name, arg.value.eval(context)));
              isNamedFound = true;
              break;
            }
          }
          if (isNamedFound) {
            _args.removeAt(i);
            i--;
            continue;
          } else {
            throw LessExceptionError(LessError(
                type: 'Runtime',
                message:
                    'Named argument for ${this.name} ${_args[i].name}  not found'));
          }
        }
      }
    }

    argIndex = 0;
    for (int i = 0; i < params.length; i++) {
      if (i < evaldArguments.length && evaldArguments[i] != null) continue;

      final MixinArgs arg =
          (_args != null && argIndex < _args.length) ? _args[argIndex] : null;

      if ((name = params[i].name) != null) {
        if (params[i].variadic) {
          final List<Node> varargs = <Node>[];
          for (int j = argIndex; j < argsLength; j++) {
            varargs.add(_args[j].value.eval(context));
          }
          frame.prependRule(
              Declaration(name, Expression(varargs).eval(context)));
        } else {
          Node val = arg?.value;
          if (val != null) {
            // This was a mixin call, pass in a detached ruleset of it's eval'd rules
            val = (val is Nodeset)
                ? DetachedRuleset(Ruleset(null, val.rules))
                : val.eval(context);
          } else if (params[i].value != null) {
            val = params[i].value.eval(_mixinEnv);
            frame.resetCache();
          } else {
            throw LessExceptionError(LessError(
                type: 'Runtime',
                message:
                    'wrong number of arguments for ${this.name} ($argsLength for $arity)'));
          }
          frame.prependRule(Declaration(name, val));
          evaldArguments[i] = val;
        }
      }
      if (params[i].variadic && _args != null) {
        for (int j = argIndex; j < argsLength; j++) {
          evaldArguments[j] = _args[j].value.eval(context);
        }
      }
      argIndex++;
    }

    // Remove null elements at the end
    while (evaldArguments.isNotEmpty) {
      if (evaldArguments.last == null) {
        evaldArguments.removeLast();
      } else {
        break;
      }
    }
    return frame;

// 3.5.0.beta.4 20180630
//  Definition.prototype.evalParams = function (context, mixinEnv, args, evaldArguments) {
//      /* jshint boss:true */
//      var frame = new Ruleset(null, null),
//          varargs, arg,
//          params = utils.copyArray(this.params),
//          i, j, val, name, isNamedFound, argIndex, argsLength = 0;
//
//      if (mixinEnv.frames && mixinEnv.frames[0] && mixinEnv.frames[0].functionRegistry) {
//          frame.functionRegistry = mixinEnv.frames[0].functionRegistry.inherit();
//      }
//      mixinEnv = new contexts.Eval(mixinEnv, [frame].concat(mixinEnv.frames));
//
//      if (args) {
//          args = utils.copyArray(args);
//          argsLength = args.length;
//
//          for (i = 0; i < argsLength; i++) {
//              arg = args[i];
//              if (name = (arg && arg.name)) {
//                  isNamedFound = false;
//                  for (j = 0; j < params.length; j++) {
//                      if (!evaldArguments[j] && name === params[j].name) {
//                          evaldArguments[j] = arg.value.eval(context);
//                          frame.prependRule(new Declaration(name, arg.value.eval(context)));
//                          isNamedFound = true;
//                          break;
//                      }
//                  }
//                  if (isNamedFound) {
//                      args.splice(i, 1);
//                      i--;
//                      continue;
//                  } else {
//                      throw { type: 'Runtime', message: 'Named argument for ' + this.name +
//                          ' ' + args[i].name + ' not found' };
//                  }
//              }
//          }
//      }
//      argIndex = 0;
//      for (i = 0; i < params.length; i++) {
//          if (evaldArguments[i]) { continue; }
//
//          arg = args && args[argIndex];
//
//          if (name = params[i].name) {
//              if (params[i].variadic) {
//                  varargs = [];
//                  for (j = argIndex; j < argsLength; j++) {
//                      varargs.push(args[j].value.eval(context));
//                  }
//                  frame.prependRule(new Declaration(name, new Expression(varargs).eval(context)));
//              } else {
//                  val = arg && arg.value;
//                  if (val) {
//                      // This was a mixin call, pass in a detached ruleset of it's eval'd rules
//                      if (Array.isArray(val)) {
//                          val = new DetachedRuleset(new Ruleset('', val));
//                      }
//                      else {
//                          val = val.eval(context);
//                      }
//                  } else if (params[i].value) {
//                      val = params[i].value.eval(mixinEnv);
//                      frame.resetCache();
//                  } else {
//                      throw { type: 'Runtime', message: 'wrong number of arguments for ' + this.name +
//                          ' (' + argsLength + ' for ' + this.arity + ')' };
//                  }
//
//                  frame.prependRule(new Declaration(name, val));
//                  evaldArguments[i] = val;
//              }
//          }
//
//          if (params[i].variadic && args) {
//              for (j = argIndex; j < argsLength; j++) {
//                  evaldArguments[j] = args[j].value.eval(context);
//              }
//          }
//          argIndex++;
//      }
//
//      return frame;
//  };
  }