[Pkg-javascript-commits] [node-jade] 37/72: Allow render as a fallback for compileAsync

Jelmer Vernooij jelmer at moszumanska.debian.org
Sun Jul 3 18:03:27 UTC 2016


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

jelmer pushed a commit to annotated tag upstream/1.0.0
in repository node-jade.

commit 2707b2230bd66b68a82307a25f8e662b0c5d7bf4
Author: Timothy Gu <timothygu99 at gmail.com>
Date:   Fri Jul 3 00:16:21 2015 -0700

    Allow render as a fallback for compileAsync
    
    renderAsync is
        (str, options, locals) => Promise => {output, deps}
    while compileAsync is
        (str, options) => Promise => {fn: (locals) => output, deps}
    The most we can do is
        (str, options) => Promise => {fn: (locals) => Promise => output, deps}
---
 index.js              | 14 +++++---------
 test/compile-async.js | 38 +++++++++++++++++++-------------------
 2 files changed, 24 insertions(+), 28 deletions(-)

diff --git a/index.js b/index.js
index 5a54ef8..af4d5c4 100644
--- a/index.js
+++ b/index.js
@@ -99,7 +99,7 @@ function Transformer(tr) {
 
 var fallbacks = {
   compile: ['compile', 'render'],
-  compileAsync: ['compileAsync', 'compile'],
+  compileAsync: ['compileAsync', 'compile', 'render'],
   compileFile: ['compileFile', 'compile', 'renderFile', 'render'],
   compileFileAsync: ['compileFileAsync', 'compileFile', 'compileAsync', 'compile'],
   compileClient: ['compileClient'],
@@ -150,17 +150,13 @@ Transformer.prototype.compile = function (str, options) {
   return tr.normalizeFn(this._tr.compile(str, options));
 };
 Transformer.prototype.compileAsync = function (str, options, cb) {
-  if (!this.can('compileAsync')) {
-    if (this.can('compileFileAsync')) {
-      return Promise.reject(new Error('The Transform "' + this.name + '" does not support compiling plain strings')).nodeify(cb);
-    } else {
-      return Promise.reject(new Error('The Transform "' + this.name + '" does not support compilation')).nodeify(cb);
-    }
+  if (!this.can('compileAsync')) { // compileFile* || renderFile* || renderAsync || compile*Client*
+    return Promise.reject(new Error('The Transform "' + this.name + '" does not support compiling plain strings')).nodeify(cb);
   }
   if (this._hasMethod('compileAsync')) {
     return tr.normalizeFnAsync(this._tr.compileAsync(str, options), cb);
-  } else {
-    return tr.normalizeFnAsync(this._tr.compile(str, options), cb);
+  } else { // render || compile
+    return tr.normalizeFnAsync(this.compile(str, options), cb);
   }
 };
 Transformer.prototype.compileFile = function (filename, options) {
diff --git a/test/compile-async.js b/test/compile-async.js
index 41881d9..c142311 100644
--- a/test/compile-async.js
+++ b/test/compile-async.js
@@ -26,49 +26,49 @@ test('compileAsync - with tr.compileAsync(str, options) => Promise(fn)', functio
   });
   assert(tr.compileAsync('example input', sentinel, cbSentinel) === normalizedSentinel);
 });
-test('compileAsync - with tr.compile(str, options) => fn', function (override) {
+test('compileAsync - with tr.compile(str, options) => fn', function () {
   var sentinel = {};
-  var fnSentinel = {};
-  var cbSentinel = {};
-  var normalizedSentinel = {};
-  override('normalizeFnAsync', function (fn, cb) {
-    assert(fn === fnSentinel);
-    assert(cb === cbSentinel);
-    return normalizedSentinel;
-  });
+  var fnSentinel = function (locals) {};
   var tr = createTransformer({
     name: 'test',
     outputFormat: 'html',
     compile: function (str, options) {
       assert(str === 'example input');
       assert(options === sentinel);
-      return fnSentinel
+      return fnSentinel;
     }
   });
-  assert(tr.compileAsync('example input', sentinel, cbSentinel) === normalizedSentinel);
+  return tr.compileAsync('example input', sentinel).then(function (out) {
+    assert(out.fn === fnSentinel);
+  });
 });
-test('compileAsync - without tr.compile or tr.compileAsync', function () {
+test('compileAsync - with tr.render(str, options, locals) => output', function () {
+  var sentinel = {};
+  var localsSentinel = {};
   var tr = createTransformer({
     name: 'test',
     outputFormat: 'html',
-    render: function (str, options) {
+    render: function (str, options, locals) {
+      assert(str === 'example input');
+      assert(options === sentinel);
+      assert(locals === localsSentinel);
+      return 'example output';
     }
   });
-  var a = tr.compileAsync('example input', {}).then(function () {
-    throw new Error('Expected error');
-  }, function (err) {
-    if (!(/does not support compilation/.test(err.message))) throw err;
+  return tr.compileAsync('example input', sentinel).then(function (out) {
+    assert(out.fn(localsSentinel) === 'example output');
   });
+});
+test('compileAsync - without tr.compile, tr.compileAsync, or tr.render', function () {
   var tr = createTransformer({
     name: 'test',
     outputFormat: 'html',
     compileFile: function (filename, options) {
     }
   });
-  var b = tr.compileAsync('example input', {}).then(function () {
+  return tr.compileAsync('example input', {}).then(function () {
     throw new Error('Expected error');
   }, function (err) {
     if (!(/does not support compiling plain strings/.test(err.message))) throw err;
   });
-  return Promise.all([a, b]);
 });

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



More information about the Pkg-javascript-commits mailing list