Skip to content

Commit

Permalink
Throw exception if id tracking args are passed
Browse files Browse the repository at this point in the history
Fixes #1151
  • Loading branch information
kpdecker committed Dec 27, 2015
1 parent ee6fadf commit 118836f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 28 deletions.
52 changes: 26 additions & 26 deletions lib/handlebars/compiler/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,41 +421,20 @@ Compiler.prototype = {
}
};

export function precompile(input, options, env) {
if (input == null || (typeof input !== 'string' && input.type !== 'Program')) {
throw new Exception('You must pass a string or Handlebars AST to Handlebars.precompile. You passed ' + input);
}

options = options || {};
if (!('data' in options)) {
options.data = true;
}
if (options.compat) {
options.useDepths = true;
}
export function precompile(input, options = {}, env) {
validateInput(input, options);

let ast = env.parse(input, options),
environment = new env.Compiler().compile(ast, options);
let environment = compileEnvironment(input, options, env);
return new env.JavaScriptCompiler().compile(environment, options);
}

export function compile(input, options = {}, env) {
if (input == null || (typeof input !== 'string' && input.type !== 'Program')) {
throw new Exception('You must pass a string or Handlebars AST to Handlebars.compile. You passed ' + input);
}

if (!('data' in options)) {
options.data = true;
}
if (options.compat) {
options.useDepths = true;
}
validateInput(input, options);

let compiled;

function compileInput() {
let ast = env.parse(input, options),
environment = new env.Compiler().compile(ast, options),
let environment = compileEnvironment(input, options, env),
templateSpec = new env.JavaScriptCompiler().compile(environment, options, undefined, true);
return env.template(templateSpec);
}
Expand All @@ -469,6 +448,27 @@ export function compile(input, options = {}, env) {
};
}

function validateInput(input, options) {
if (input == null || (typeof input !== 'string' && input.type !== 'Program')) {
throw new Exception('You must pass a string or Handlebars AST to Handlebars.compile. You passed ' + input);
}

if (options.trackIds || options.stringParams) {
throw new Exception('TrackIds and stringParams are no longer supported. See Github #1145');
}

if (!('data' in options)) {
options.data = true;
}
if (options.compat) {
options.useDepths = true;
}
}
function compileEnvironment(input, options, env) {
let ast = env.parse(input, options);
return new env.Compiler().compile(ast, options);
}

function argEquals(a, b) {
if (a === b) {
return true;
Expand Down
13 changes: 11 additions & 2 deletions spec/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,25 @@ describe('compiler', function() {
it('can pass through an empty string', function() {
equal(Handlebars.compile('')(), '');
});

it('throws on desupported options', function() {
shouldThrow(function() {
Handlebars.compile('Dudes', {trackIds: true});
}, Error, 'TrackIds and stringParams are no longer supported. See Github #1145');
shouldThrow(function() {
Handlebars.compile('Dudes', {stringParams: true});
}, Error, 'TrackIds and stringParams are no longer supported. See Github #1145');
});
});

describe('#precompile', function() {
it('should fail with invalid input', function() {
shouldThrow(function() {
Handlebars.precompile(null);
}, Error, 'You must pass a string or Handlebars AST to Handlebars.precompile. You passed null');
}, Error, 'You must pass a string or Handlebars AST to Handlebars.compile. You passed null');
shouldThrow(function() {
Handlebars.precompile({});
}, Error, 'You must pass a string or Handlebars AST to Handlebars.precompile. You passed [object Object]');
}, Error, 'You must pass a string or Handlebars AST to Handlebars.compile. You passed [object Object]');
});

it('can utilize AST instance', function() {
Expand Down

0 comments on commit 118836f

Please sign in to comment.