Skip to content

Commit

Permalink
Merge pull request #13913 from courajs/assert-reserved-named-arguments
Browse files Browse the repository at this point in the history
Disallow paths beginning with @ in templates
  • Loading branch information
chadhietala committed Jul 27, 2016
2 parents c2fafcf + f015b6e commit 798ba8a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { assert } from 'ember-metal/debug';
import calculateLocationDisplay from 'ember-template-compiler/system/calculate-location-display';

export default function AssertReservedNamedArguments(options) {
this.syntax = null;
this.options = options;
}

AssertReservedNamedArguments.prototype.transform = function AssertReservedNamedArguments_transform(ast) {
let moduleName = this.options.moduleName;

this.syntax.traverse(ast, {
PathExpression: function(node) {
if (node.original[0] === '@') {
assert(assertMessage(moduleName, node));
}
}
});

return ast;
};

function assertMessage(moduleName, node) {
let path = node.original;
let source = calculateLocationDisplay(moduleName, node.loc);

return `'${path}' is not a valid path. ${source}`;
}
8 changes: 5 additions & 3 deletions packages/ember-template-compiler/lib/plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ import TransformItemClass from 'ember-template-compiler/plugins/transform-item-c
import TransformAngleBracketComponents from 'ember-template-compiler/plugins/transform-angle-bracket-components';
import TransformInputOnToOnEvent from 'ember-template-compiler/plugins/transform-input-on-to-onEvent';
import TransformTopLevelComponents from 'ember-template-compiler/plugins/transform-top-level-components';
import DeprecateRenderModel from 'ember-template-compiler/plugins/deprecate-render-model';
import TransformInlineLinkTo from 'ember-template-compiler/plugins/transform-inline-link-to';
import TransformOldClassBindingSyntax from 'ember-template-compiler/plugins/transform-old-class-binding-syntax';
import DeprecateRenderModel from 'ember-template-compiler/plugins/deprecate-render-model';
import AssertReservedNamedArguments from 'ember-template-compiler/plugins/assert-reserved-named-arguments';

export default Object.freeze([
TransformOldBindingSyntax,
TransformItemClass,
TransformAngleBracketComponents,
TransformInputOnToOnEvent,
TransformTopLevelComponents,
DeprecateRenderModel,
TransformInlineLinkTo,
TransformOldClassBindingSyntax
TransformOldClassBindingSyntax,
DeprecateRenderModel,
AssertReservedNamedArguments
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { compile } from '../utils/helpers';

QUnit.module('ember-template-compiler: assert-reserved-named-arguments');

QUnit.test('Paths beginning with @ are not valid', function() {
expect(3);

expectAssertion(() => {
compile('{{@foo}}', {
moduleName: 'baz/foo-bar'
});
}, `'@foo' is not a valid path. ('baz/foo-bar' @ L1:C2) `);

expectAssertion(() => {
compile('{{#if @foo}}Yup{{/if}}', {
moduleName: 'baz/foo-bar'
});
}, `'@foo' is not a valid path. ('baz/foo-bar' @ L1:C6) `);

expectAssertion(() => {
compile('{{input type=(if @foo "bar" "baz")}}', {
moduleName: 'baz/foo-bar'
});
}, `'@foo' is not a valid path. ('baz/foo-bar' @ L1:C17) `);
});

0 comments on commit 798ba8a

Please sign in to comment.