eval method

  1. @override
Node eval(
  1. Contexts context
)
override

Implementation

@override
Node eval(Contexts context) {
  //RegExp reJS = new RegExp(r'`([^`]+)`'); //javascript expresion
  final that = this;
  var value = this.value;

  //@f: 'ables';
  //@import 'vari@{f}.less';
  //result = @import 'variables.less';
  String variableReplacement(Match m) {
    final name = m[1];
    final v =
        Variable('@$name', that.index, that.currentFileInfo).eval(context);

    return (v is Quoted) ? v.value : v.toCSS(null);
  }

  String propertyReplacement(Match m) {
    final name = m[1];
    final v = Property('\$$name', index, currentFileInfo).eval(context);
    return (v is Quoted) ? v.value : v.toCSS(null);
  }

  String iterativeReplace(String value, RegExp regexp,
      String Function(Match match) replacementFnc) {
    var evaluatedValue = value;
    String _value;

    do {
      _value = evaluatedValue;
      evaluatedValue = _value.replaceAllMapped(regexp, replacementFnc);
    } while (_value != evaluatedValue);

    return evaluatedValue;
  }

  value = iterativeReplace(value, variableRegex, variableReplacement);
  value = iterativeReplace(value, propRegex, propertyReplacement);

  return Quoted('$quote$value$quote', value,
      escaped: escaped, index: index, currentFileInfo: currentFileInfo);

// 3.9.0 20190711
//Quoted.prototype.eval = function (context) {
//    var that = this, value = this.value;
//    var variableReplacement = function (_, name) {
//        var v = new Variable('@' + name, that.getIndex(), that.fileInfo()).eval(context, true);
//        return (v instanceof Quoted) ? v.value : v.toCSS();
//    };
//    var propertyReplacement = function (_, name) {
//        var v = new Property('$' + name, that.getIndex(), that.fileInfo()).eval(context, true);
//        return (v instanceof Quoted) ? v.value : v.toCSS();
//    };
//    function iterativeReplace(value, regexp, replacementFnc) {
//        var evaluatedValue = value;
//        do {
//            value = evaluatedValue.toString();
//            evaluatedValue = value.replace(regexp, replacementFnc);
//        } while (value !== evaluatedValue);
//        return evaluatedValue;
//    }
//    value = iterativeReplace(value, this.variableRegex, variableReplacement);
//    value = iterativeReplace(value, this.propRegex, propertyReplacement);
//    return new Quoted(this.quote + value + this.quote, value, this.escaped, this.getIndex(), this.fileInfo());
//};
}