Skip to content

Commit 529550d

Browse files
committed
refactor($parse): do not pass scope,locals to interceptor fns
All internal use of interceptors are for things such as data conversion/normalizing, never reading state from the scope/locals. This is the intended use and makes interceptors more like filters (which receive only the input value + args, no scope/locals).
1 parent 828a275 commit 529550d

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

src/ng/parse.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1940,12 +1940,12 @@ function $ParseProvider() {
19401940

19411941
function regularInterceptedExpression(scope, locals, assign, inputs) {
19421942
var value = useInputs && inputs ? inputs[0] : parsedExpression(scope, locals, assign, inputs);
1943-
return interceptorFn(value, scope, locals);
1943+
return interceptorFn(value);
19441944
}
19451945

19461946
function oneTimeInterceptedExpression(scope, locals, assign, inputs) {
19471947
var value = useInputs && inputs ? inputs[0] : parsedExpression(scope, locals, assign, inputs);
1948-
var result = interceptorFn(value, scope, locals);
1948+
var result = interceptorFn(value);
19491949
// we only return the interceptor's result if the
19501950
// initial value is defined (for bind-once)
19511951
return isDone(value) ? result : value;

test/ng/parseSpec.js

+68
Original file line numberDiff line numberDiff line change
@@ -3232,6 +3232,74 @@ describe('parser', function() {
32323232
});
32333233

32343234
describe('interceptorFns', function() {
3235+
it('should only be passed the intercepted value', inject(function($parse) {
3236+
var args;
3237+
function interceptor(v) {
3238+
args = sliceArgs(arguments);
3239+
return v;
3240+
}
3241+
3242+
scope.$watch($parse('a', interceptor));
3243+
3244+
scope.a = 1;
3245+
scope.$digest();
3246+
expect(args).toEqual([1]);
3247+
}));
3248+
3249+
it('should only be passed the intercepted value when double-intercepted',
3250+
inject(function($parse) {
3251+
var args1;
3252+
function int1(v) {
3253+
args1 = sliceArgs(arguments);
3254+
return v + 2;
3255+
}
3256+
var args2;
3257+
function int2(v) {
3258+
args2 = sliceArgs(arguments);
3259+
return v + 4;
3260+
}
3261+
3262+
scope.$watch($parse($parse('a', int1), int2));
3263+
3264+
scope.a = 1;
3265+
scope.$digest();
3266+
expect(args1).toEqual([1]);
3267+
expect(args2).toEqual([3]);
3268+
}));
3269+
3270+
it('should support locals', inject(function($parse) {
3271+
var args;
3272+
function interceptor(v) {
3273+
args = sliceArgs(arguments);
3274+
return v + 4;
3275+
}
3276+
3277+
var exp = $parse('a + b', interceptor);
3278+
scope.a = 1;
3279+
3280+
expect(exp(scope, {b: 2})).toBe(7);
3281+
expect(args).toEqual([3]);
3282+
}));
3283+
3284+
it('should support locals when double-intercepted', inject(function($parse) {
3285+
var args1;
3286+
function int1(v) {
3287+
args1 = sliceArgs(arguments);
3288+
return v + 4;
3289+
}
3290+
var args2;
3291+
function int2(v) {
3292+
args2 = sliceArgs(arguments);
3293+
return v + 8;
3294+
}
3295+
3296+
var exp = $parse($parse('a + b', int1), int2);
3297+
3298+
scope.a = 1;
3299+
expect(exp(scope, {b: 2})).toBe(15);
3300+
expect(args1).toEqual([3]);
3301+
expect(args2).toEqual([7]);
3302+
}));
32353303

32363304
it('should always be invoked if they are flagged as having $stateful',
32373305
inject(function($parse) {

0 commit comments

Comments
 (0)