Skip to content

Commit

Permalink
Expose compilerInfo and checkRevision as APIs
Browse files Browse the repository at this point in the history
Fixes #656
  • Loading branch information
kpdecker committed Dec 24, 2013
1 parent 1a751b2 commit bbc7c7d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
10 changes: 7 additions & 3 deletions lib/handlebars/compiler/javascript-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ JavaScriptCompiler.prototype = {
}
},

compilerInfo: function() {
var revision = COMPILER_REVISION,
versions = REVISION_CHANGES[revision];
return "this.compilerInfo = ["+revision+",'"+versions+"'];\n";
},

appendToBuffer: function(string) {
if (this.environment.isSimple) {
return "return " + string + ";";
Expand Down Expand Up @@ -165,9 +171,7 @@ JavaScriptCompiler.prototype = {
var source = this.mergeSource();

if (!this.isChild) {
var revision = COMPILER_REVISION,
versions = REVISION_CHANGES[revision];
source = "this.compilerInfo = ["+revision+",'"+versions+"'];\n"+source;
source = this.compilerInfo()+source;
}

if (asObject) {
Expand Down
4 changes: 2 additions & 2 deletions lib/handlebars/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Utils from "./utils";
import Exception from "./exception";
import { COMPILER_REVISION, REVISION_CHANGES } from "./base";

function checkRevision(compilerInfo) {
export function checkRevision(compilerInfo) {
var compilerRevision = compilerInfo && compilerInfo[0] || 1,
currentRevision = COMPILER_REVISION;

Expand Down Expand Up @@ -89,7 +89,7 @@ export function template(templateSpec, env) {
options.data);

if (!options.partial) {
checkRevision(container.compilerInfo);
env.VM.checkRevision(container.compilerInfo);
}

return result;
Expand Down
22 changes: 22 additions & 0 deletions spec/javascript-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,28 @@ describe('javascript-compiler api', function() {
shouldCompileTo("{{foo}}", { bar_foo: "food" }, "food");
});
});
describe('#compilerInfo', function() {
var $superCheck, $superInfo;
beforeEach(function() {
$superCheck = handlebarsEnv.VM.checkRevision;
$superInfo = handlebarsEnv.JavaScriptCompiler.prototype.compilerInfo;
});
afterEach(function() {
handlebarsEnv.VM.checkRevision = $superCheck;
handlebarsEnv.JavaScriptCompiler.prototype.compilerInfo = $superInfo;
});
it('should allow compilerInfo override', function() {
handlebarsEnv.JavaScriptCompiler.prototype.compilerInfo = function() {
return 'this.compilerInfo = "crazy";';
};
handlebarsEnv.VM.checkRevision = function(compilerInfo) {
if (compilerInfo !== 'crazy') {
throw new Error('It didn\'t work');
}
};
shouldCompileTo("{{foo}} ", { foo: "food" }, "food ");
});
});
describe('buffer', function() {
var $superAppend, $superCreate;
beforeEach(function() {
Expand Down

0 comments on commit bbc7c7d

Please # to comment.