[Pkg-javascript-commits] [node-jade] 36/72: Allow render and renderFile as fallbacks for compileFile

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 33143f1608517f7eb2e39887d148f3ab1dd7920e
Author: Timothy Gu <timothygu99 at gmail.com>
Date:   Thu Jul 2 23:31:31 2015 -0700

    Allow render and renderFile as fallbacks for compileFile
---
 index.js             | 18 +++++++++---------
 test/compile-file.js | 39 ++++++++++++++++++++++++++++++++-------
 2 files changed, 41 insertions(+), 16 deletions(-)

diff --git a/index.js b/index.js
index 0c1d217..5a54ef8 100644
--- a/index.js
+++ b/index.js
@@ -100,7 +100,7 @@ function Transformer(tr) {
 var fallbacks = {
   compile: ['compile', 'render'],
   compileAsync: ['compileAsync', 'compile'],
-  compileFile: ['compileFile', 'compile'],
+  compileFile: ['compileFile', 'compile', 'renderFile', 'render'],
   compileFileAsync: ['compileFileAsync', 'compileFile', 'compileAsync', 'compile'],
   compileClient: ['compileClient'],
   compileClientAsync: ['compileClientAsync', 'compileClient'],
@@ -164,17 +164,17 @@ Transformer.prototype.compileAsync = function (str, options, cb) {
   }
 };
 Transformer.prototype.compileFile = function (filename, options) {
-  if (!this.can('compileFile')) {
-    if (this.can('compileFileAsync')) {
-      throw new Error('The Transform "' + this.name + '" does not support synchronous compilation');
-    } else {
-      throw new Error('The Transform "' + this.name + '" does not support compilation');
-    }
+  if (!this.can('compileFile')) { // compile*Client* || compile*Async || render*Async
+    throw new Error('The Transform "' + this.name + '" does not support synchronous compilation');
   }
   if (this._hasMethod('compileFile')) {
     return tr.normalizeFn(this._tr.compileFile(filename, options));
-  } else {
-    return tr.normalizeFn(this._tr.compile(tr.readFileSync(filename, 'utf8'), options));
+  } else if (this._hasMethod('renderFile')) {
+    return tr.normalizeFn(function (locals) {
+      return tr.normalize(this._tr.renderFile(filename, options, locals)).body;
+    }.bind(this));
+  } else { // render || compile
+    return this.compile(tr.readFileSync(filename, 'utf8'), options);
   }
 };
 Transformer.prototype.compileFileAsync = function (filename, options, cb) {
diff --git a/test/compile-file.js b/test/compile-file.js
index 5d37a10..82b34ce 100644
--- a/test/compile-file.js
+++ b/test/compile-file.js
@@ -46,23 +46,48 @@ test('compileFile - with tr.compile(src, options) => fn', function (override) {
   });
   assert(tr.compileFile('example-input.txt', sentinel) === normalizedSentinel);
 });
-test('compileFile - without tr.compile or tr.compileFile', function () {
+test('compileFile - with tr.renderFile(src, options, locals) => output', function (override) {
+  var sentinel = {};
+  var localsSentinel = {};
   var tr = createTransformer({
     name: 'test',
     outputFormat: 'html',
-    compileAsync: function () {
+    renderFile: function (file, options, locals) {
+      assert(file === 'example-input.txt');
+      assert(options === sentinel);
+      assert(locals === localsSentinel);
+      return 'example output';
     }
   });
-  assert.throws(function () {
-    tr.compileFile('example input', {});
-  }, /does not support synchronous compilation/);
+  assert(tr.compileFile('example-input.txt', sentinel).fn(localsSentinel) === 'example output');
+});
+test('compileFile - with tr.render(src, options, locals) => output', function (override) {
+  var sentinel = {};
+  var localsSentinel = {};
+  override('readFileSync', function (filename) {
+    assert(filename === 'example-input.txt');
+    return 'example input';
+  });
   var tr = createTransformer({
     name: 'test',
     outputFormat: 'html',
-    render: function () {
+    render: function (str, options, locals) {
+      assert(str === 'example input');
+      assert(options === sentinel);
+      assert(locals === localsSentinel);
+      return 'example output';
+    }
+  });
+  assert(tr.compileFile('example-input.txt', sentinel).fn(localsSentinel) === 'example output');
+});
+test('compileFile - without tr.compile, tr.compileFile, tr.render, or tr.renderFile', function () {
+  var tr = createTransformer({
+    name: 'test',
+    outputFormat: 'html',
+    compileAsync: function () {
     }
   });
   assert.throws(function () {
     tr.compileFile('example input', {});
-  }, /does not support compilation/);
+  }, /does not support synchronous compilation/);
 });

-- 
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