vendor/lib/visitor/evaluator.js in stylus-source-0.20.1 vs vendor/lib/visitor/evaluator.js in stylus-source-0.21.0
- old
+ new
@@ -8,10 +8,11 @@
/**
* Module dependencies.
*/
var Visitor = require('./')
+ , units = require('../units')
, nodes = require('../nodes')
, Stack = require('../stack')
, Frame = require('../stack/frame')
, Scope = require('../stack/scope')
, utils = require('../utils')
@@ -249,18 +250,21 @@
/**
* Visit Each.
*/
Evaluator.prototype.visitEach = function(each){
+ var _ = this.return;
+ this.return = true;
var expr = utils.unwrap(this.visit(utils.unwrap(each.expr)))
, len = expr.nodes.length
, val = new nodes.Ident(each.val)
, key = new nodes.Ident(each.key || '__index__')
, scope = this.currentScope
, block = this.currentBlock
, vals = []
, body;
+ this.return = _;
each.block.scope = false;
for (var i = 0; i < len; ++i) {
val.val = expr.nodes[i];
key.val = new nodes.Unit(i);
@@ -447,10 +451,14 @@
Evaluator.prototype.visitExpression = function(expr){
for (var i = 0, len = expr.nodes.length; i < len; ++i) {
expr.nodes[i] = this.visit(expr.nodes[i]);
}
+
+ // support (n * 5)px etc
+ if (this.castable(expr)) expr = this.cast(expr);
+
return expr;
};
/**
* Visit Arguments.
@@ -1074,9 +1082,35 @@
}
replace(val);
expr.push(val);
return expr;
+};
+
+/**
+ * Cast `expr` to the trailing ident.
+ *
+ * @param {Expression} expr
+ * @return {Unit}
+ * @api private
+ */
+
+Evaluator.prototype.cast = function(expr){
+ return new nodes.Unit(expr.first.val, expr.nodes[1].name);
+};
+
+/**
+ * Check if `expr` is castable.
+ *
+ * @param {Expression} expr
+ * @return {Boolean}
+ * @api private
+ */
+
+Evaluator.prototype.castable = function(expr){
+ return 2 == expr.nodes.length
+ && 'unit' == expr.first.nodeName
+ && ~units.indexOf(expr.nodes[1].name);
};
/**
* Warn with the given `msg`.
*