From 8d77b770fb7939d24f2a832eff88de3970d6e90b Mon Sep 17 00:00:00 2001 From: David Greenspan Date: Tue, 4 Mar 2014 15:40:23 -0800 Subject: [PATCH] Spacebars: Disallow non-initial `this` in paths Apparently `../this` used to work as a synonym for `..` in Handlebars, but in Spacebars it actually would look for a property named `this`, which caused confusing breaking changes in apps. --- packages/spacebars-compiler/spacebars_tests.js | 6 ++++++ packages/spacebars-compiler/templatetag.js | 10 +++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/spacebars-compiler/spacebars_tests.js b/packages/spacebars-compiler/spacebars_tests.js index 43d56b67e5..778cc1a7e6 100644 --- a/packages/spacebars-compiler/spacebars_tests.js +++ b/packages/spacebars-compiler/spacebars_tests.js @@ -121,6 +121,12 @@ Tinytest.add("spacebars - stache tags", function (test) { ['NUMBER', 2, 'y']]}); run('{{> foo x=1 y=2 z}}', "Can't have a non-keyword argument"); + + run('{{true.foo}}', "Can't use"); + run('{{foo.this}}', "Can only use"); + run('{{./this}}', "Can only use"); + run('{{../this}}', "Can only use"); + }); diff --git a/packages/spacebars-compiler/templatetag.js b/packages/spacebars-compiler/templatetag.js index 2b58dbd848..552944c4cc 100644 --- a/packages/spacebars-compiler/templatetag.js +++ b/packages/spacebars-compiler/templatetag.js @@ -141,9 +141,13 @@ TemplateTag.parse = function (scannerOrString) { segments.push(seg); } else { var id = scanIdentifier(! segments.length); - if (id === 'this' && ! segments.length) { - // initial `this` - segments.push('.'); + if (id === 'this') { + if (! segments.length) { + // initial `this` + segments.push('.'); + } else { + error("Can only use `this` at the beginning of a path.\nInstead of `foo.this` or `../this`, just write `foo` or `..`."); + } } else { segments.push(id); }