Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix strict_variables issues #674

Merged
merged 2 commits into from
Nov 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/twig.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,7 @@ module.exports = function (Twig) {
// options: {
// Compiler/parser options
//
// strictVariables: true/false
// strict_variables: true/false
// Should missing variable/keys emit an error message. If false, they default to null.
// }
// }
Expand Down
2 changes: 1 addition & 1 deletion src/twig.exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = function (Twig) {
'use strict';
const {id} = params;
const options = {
strictVariables: params.strictVariables || false,
strictVariables: params.strict_variables || false,
// TODO: turn autoscape on in the next major version
autoescape: (params.autoescape !== null && params.autoescape) || false,
allowInlineIncludes: params.allowInlineIncludes || false,
Expand Down
30 changes: 21 additions & 9 deletions src/twig.expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,10 @@ module.exports = function (Twig) {
// Get the variable from the context
return Twig.expression.resolveAsync.call(state, context[token.value], context)
.then(value => {
if (state.template.options.strictVariables && value === undefined) {
throw new Twig.Error('Variable "' + token.value + '" does not exist.');
}

stack.push(value);
});
}
Expand All @@ -869,14 +873,19 @@ module.exports = function (Twig) {
const object = stack.pop();
let value;

if (object && !Object.prototype.hasOwnProperty.call(object, key) && state.template.options.strictVariables) {
const keys = Object.keys(object);
if (keys.length > 0) {
throw new Twig.Error('Key "' + key + '" for object with keys "' + Object.keys(object).join(', ') + '" does not exist.');
} else {
throw new Twig.Error('Key "' + key + '" does not exist as the object is empty.');
}
}

return parseParams(state, token.params, context)
.then(params => {
if (object === null || object === undefined) {
if (state.template.options.strict_variables) {
throw new Twig.Error('Can\'t access a key ' + key + ' on an null or undefined object.');
} else {
value = undefined;
}
value = undefined;
} else {
const capitalize = function (value) {
return value.substr(0, 1).toUpperCase() + value.substr(1);
Expand Down Expand Up @@ -935,12 +944,15 @@ module.exports = function (Twig) {
.then(key => {
object = stack.pop();

if (object === null || object === undefined) {
if (state.template.options.strict_variables) {
throw new Twig.Error('Can\'t access a key ' + key + ' on an null or undefined object.');
if (object && !Object.prototype.hasOwnProperty.call(object, key) && state.template.options.strictVariables) {
const keys = Object.keys(object);
if (keys.length > 0) {
throw new Twig.Error('Key "' + key + '" for array with keys "' + keys.join(', ') + '" does not exist.');
} else {
return null;
throw new Twig.Error('Key "' + key + '" does not exist as the array is empty.');
}
} else if (object === null || object === undefined) {
return null;
}

// Get the variable from the context
Expand Down
66 changes: 65 additions & 1 deletion test/test.options.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,69 @@ describe('Twig.js Optional Functionality ->', function () {

output.should.equal('template with another template');
});
});

describe('should throw an error when `strict_variables` set to `true`', function () {
const variable = twig({
rethrow: true,
strict_variables: true,
data: '{{ test }}'
});

const object = twig({
rethrow: true,
strict_variables: true,
data: '{{ test.10 }}'
});

const array = twig({
rethrow: true,
strict_variables: true,
data: '{{ test[10] }}'
});

it('For undefined variables', function () {
try {
variable.render();
throw new Error('should have thrown an error.');
} catch (error) {
error.message.should.equal('Variable "test" does not exist.');
}
});

it('For empty objects', function () {
try {
object.render({test: {}});
throw new Error('should have thrown an error.');
} catch (error) {
error.message.should.equal('Key "10" does not exist as the object is empty.');
}
});

it('For undefined object keys', function () {
try {
object.render({test: {1: 'value', 2: 'value', 3: 'value'}});
throw new Error('should have thrown an error.');
} catch (error) {
error.message.should.equal('Key "10" for object with keys "1, 2, 3" does not exist.');
}
});

it('For empty arrays', function () {
try {
array.render({test: []});
throw new Error('should have thrown an error.');
} catch (error) {
error.message.should.equal('Key "10" does not exist as the array is empty.');
}
});

it('For undefined array keys', function () {
try {
array.render({test: [1, 2, 3]});
throw new Error('should have thrown an error.');
} catch (error) {
error.message.should.equal('Key "10" for array with keys "0, 1, 2" does not exist.');
}
});
});
});