Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

feat($parse): provide a mechanism to access the locals object #13454

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions docs/content/guide/expression.ngdoc
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ This restriction is intentional. It prevents accidental access to the global sta
Instead use services like `$window` and `$location` in functions called from expressions. Such services
provide mockable access to globals.

It is possible to access the context object using the identifier `this` and the locals object using the
identifier `$locals`.

<example module="expressionExample">
<file name="index.html">
<div class="example2" ng-controller="ExampleController">
Expand Down
16 changes: 15 additions & 1 deletion src/ng/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ AST.ArrayExpression = 'ArrayExpression';
AST.Property = 'Property';
AST.ObjectExpression = 'ObjectExpression';
AST.ThisExpression = 'ThisExpression';
AST.LocalsExpression = 'LocalsExpression';

// Internal use only
AST.NGValueParameter = 'NGValueParameter';
Expand Down Expand Up @@ -625,7 +626,8 @@ AST.prototype = {
'false': { type: AST.Literal, value: false },
'null': { type: AST.Literal, value: null },
'undefined': {type: AST.Literal, value: undefined },
'this': {type: AST.ThisExpression }
'this': {type: AST.ThisExpression },
'$locals': {type: AST.LocalsExpression }
}
};

Expand Down Expand Up @@ -745,6 +747,10 @@ function findConstantAndWatchExpressions(ast, $filter) {
ast.constant = false;
ast.toWatch = [];
break;
case AST.LocalsExpression:
ast.constant = false;
ast.toWatch = [];
break;
}
}

Expand Down Expand Up @@ -1114,6 +1120,10 @@ ASTCompiler.prototype = {
this.assign(intoId, 's');
recursionFn('s');
break;
case AST.LocalsExpression:
this.assign(intoId, 'l');
recursionFn('l');
break;
case AST.NGValueParameter:
this.assign(intoId, 'v');
recursionFn('v');
Expand Down Expand Up @@ -1441,6 +1451,10 @@ ASTInterpreter.prototype = {
return function(scope) {
return context ? {value: scope} : scope;
};
case AST.LocalsExpression:
return function(scope, locals) {
return context ? {value: locals} : locals;
};
case AST.NGValueParameter:
return function(scope, locals, assign, inputs) {
return context ? {value: assign} : assign;
Expand Down
43 changes: 41 additions & 2 deletions test/ng/parseSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,23 @@ describe('parser', function() {
});


it('should not confuse `this`, `undefined`, `true`, `false`, `null` when used as identfiers', function() {
forEach(['this', 'undefined', 'true', 'false', 'null'], function(identifier) {
it('should understand the `$locals` expression', function() {
expect(createAst('$locals')).toEqual(
{
type: 'Program',
body: [
{
type: 'ExpressionStatement',
expression: { type: 'LocalsExpression' }
}
]
}
);
});


it('should not confuse `this`, `$locals`, `undefined`, `true`, `false`, `null` when used as identfiers', function() {
forEach(['this', '$locals', 'undefined', 'true', 'false', 'null'], function(identifier) {
expect(createAst('foo.' + identifier)).toEqual(
{
type: 'Program',
Expand Down Expand Up @@ -3589,6 +3604,30 @@ describe('parser', function() {
$rootScope.null = {a: 42};
expect($rootScope.$eval('this.null.a')).toBe(42);
}));

it('should allow accessing $locals', inject(function($rootScope) {
$rootScope.foo = 'foo';
$rootScope.bar = 'bar';
$rootScope.$locals = 'foo';
var locals = {foo: 42};
expect($rootScope.$eval('$locals')).toBeUndefined();
expect($rootScope.$eval('$locals.foo')).toBeUndefined();
expect($rootScope.$eval('this.$locals')).toBe('foo');
expect(function() {
$rootScope.$eval('$locals = {}');
}).toThrow();
expect(function() {
$rootScope.$eval('$locals.bar = 23');
}).toThrow();
expect($rootScope.$eval('$locals', locals)).toBe(locals);
expect($rootScope.$eval('$locals.foo', locals)).toBe(42);
expect($rootScope.$eval('this.$locals', locals)).toBe('foo');
expect(function() {
$rootScope.$eval('$locals = {}', locals);
}).toThrow();
expect($rootScope.$eval('$locals.bar = 23', locals)).toEqual(23);
expect(locals.bar).toBe(23);
}));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have a test with this.$local.

});
});
});
Expand Down