[Pkg-javascript-commits] [less.js] 83/285: move transform out of the parser

Jonas Smedegaard dr at jones.dk
Mon Oct 26 23:23:41 UTC 2015


This is an automated email from the git hooks/post-receive script.

js pushed a commit to annotated tag v2.0.0
in repository less.js.

commit b55b596740fb9004c6b86d9348a07c854ad1a01e
Author: Luke Page <luke.a.page at gmail.com>
Date:   Wed Sep 3 18:21:25 2014 +0100

    move transform out of the parser
---
 lib/less/parser/parser.js  | 72 ++++++----------------------------------------
 lib/less/transform-tree.js | 71 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 79 insertions(+), 64 deletions(-)

diff --git a/lib/less/parser/parser.js b/lib/less/parser/parser.js
index dfc6ace..d870c8f 100644
--- a/lib/less/parser/parser.js
+++ b/lib/less/parser/parser.js
@@ -140,72 +140,16 @@ var Parser = function Parser(env) {
             }
 
             root.toCSS = (function (evaluate) {
-                return function (options, variables) {
-                    options = options || {};
-                    var evaldRoot,
-                        css,
-                        evalEnv = new contexts.evalEnv(options);
-
-                    //
-                    // Allows setting variables with a hash, so:
-                    //
-                    //   `{ color: new(tree.Color)('#f01') }` will become:
-                    //
-                    //   new(tree.Rule)('@color',
-                    //     new(tree.Value)([
-                    //       new(tree.Expression)([
-                    //         new(tree.Color)('#f01')
-                    //       ])
-                    //     ])
-                    //   )
-                    //
-                    if (typeof(variables) === 'object' && !Array.isArray(variables)) {
-                        variables = Object.keys(variables).map(function (k) {
-                            var value = variables[k];
-
-                            if (! (value instanceof tree.Value)) {
-                                if (! (value instanceof tree.Expression)) {
-                                    value = new(tree.Expression)([value]);
-                                }
-                                value = new(tree.Value)([value]);
-                            }
-                            return new(tree.Rule)('@' + k, value, false, null, 0);
-                        });
-                        evalEnv.frames = [new(tree.Ruleset)(null, variables)];
+                return function (options) {
+                    var transformTree = require("../transform-tree.js"),
+                        evaldRoot;
+                    try {
+                        evaldRoot = transformTree(this, options);
+                    } catch (e) {
+                        throw new LessError(parser, e, env);
                     }
-
+                    var css;
                     try {
-                        var preEvalVisitors = [],
-                            visitors = [
-                                new(visitor.JoinSelectorVisitor)(),
-                                new(visitor.ExtendVisitor)(),
-                                new(visitor.ToCSSVisitor)({compress: Boolean(options.compress)})
-                            ], i, root = this;
-
-                        if (options.plugins) {
-                            for(i =0; i < options.plugins.length; i++) {
-                                if (options.plugins[i].isPreEvalVisitor) {
-                                    preEvalVisitors.push(options.plugins[i]);
-                                } else {
-                                    if (options.plugins[i].isPreVisitor) {
-                                        visitors.splice(0, 0, options.plugins[i]);
-                                    } else {
-                                        visitors.push(options.plugins[i]);
-                                    }
-                                }
-                            }
-                        }
-
-                        for(i = 0; i < preEvalVisitors.length; i++) {
-                            preEvalVisitors[i].run(root);
-                        }
-
-                        evaldRoot = evaluate.call(root, evalEnv);
-
-                        for(i = 0; i < visitors.length; i++) {
-                            visitors[i].run(evaldRoot);
-                        }
-
                         if (options.sourceMap) {
                             evaldRoot = new SourceMapOutput(
                                 {
diff --git a/lib/less/transform-tree.js b/lib/less/transform-tree.js
new file mode 100644
index 0000000..bbfba9e
--- /dev/null
+++ b/lib/less/transform-tree.js
@@ -0,0 +1,71 @@
+var contexts = require("./contexts.js"),
+    visitor = require("./visitor/index.js"),
+    tree = require("./tree/index.js");
+
+module.exports = function(root, options) {
+    options = options || {};
+    var evaldRoot,
+        variables = options.variables,
+        evalEnv = new contexts.evalEnv(options);
+
+    //
+    // Allows setting variables with a hash, so:
+    //
+    //   `{ color: new(tree.Color)('#f01') }` will become:
+    //
+    //   new(tree.Rule)('@color',
+    //     new(tree.Value)([
+    //       new(tree.Expression)([
+    //         new(tree.Color)('#f01')
+    //       ])
+    //     ])
+    //   )
+    //
+    if (typeof(variables) === 'object' && !Array.isArray(variables)) {
+        variables = Object.keys(variables).map(function (k) {
+            var value = variables[k];
+
+            if (! (value instanceof tree.Value)) {
+                if (! (value instanceof tree.Expression)) {
+                    value = new(tree.Expression)([value]);
+                }
+                value = new(tree.Value)([value]);
+            }
+            return new(tree.Rule)('@' + k, value, false, null, 0);
+        });
+        evalEnv.frames = [new(tree.Ruleset)(null, variables)];
+    }
+
+    var preEvalVisitors = [],
+        visitors = [
+            new(visitor.JoinSelectorVisitor)(),
+            new(visitor.ExtendVisitor)(),
+            new(visitor.ToCSSVisitor)({compress: Boolean(options.compress)})
+        ], i;
+
+    if (options.plugins) {
+        for(i =0; i < options.plugins.length; i++) {
+            if (options.plugins[i].isPreEvalVisitor) {
+                preEvalVisitors.push(options.plugins[i]);
+            } else {
+                if (options.plugins[i].isPreVisitor) {
+                    visitors.splice(0, 0, options.plugins[i]);
+                } else {
+                    visitors.push(options.plugins[i]);
+                }
+            }
+        }
+    }
+
+    for(i = 0; i < preEvalVisitors.length; i++) {
+        preEvalVisitors[i].run(root);
+    }
+
+    evaldRoot = root.eval(evalEnv);
+
+    for(i = 0; i < visitors.length; i++) {
+        visitors[i].run(evaldRoot);
+    }
+
+    return evaldRoot;
+};

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/less.js.git



More information about the Pkg-javascript-commits mailing list