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

Commit a6e9174

Browse files
thorn0lgalfaso
authored andcommitted
refactor($parse): remove unnecessary check
Closes: #13588
1 parent dec8a0e commit a6e9174

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

src/ng/parse.js

+13-14
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,22 @@ function ensureSafeMemberName(name, fullExpression) {
4848
return name;
4949
}
5050

51-
function getStringValue(name, fullExpression) {
52-
// From the JavaScript docs:
51+
function getStringValue(name) {
5352
// Property names must be strings. This means that non-string objects cannot be used
5453
// as keys in an object. Any non-string object, including a number, is typecasted
5554
// into a string via the toString method.
55+
// -- MDN, https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Property_accessors#Property_names
5656
//
57-
// So, to ensure that we are checking the same `name` that JavaScript would use,
58-
// we cast it to a string, if possible.
59-
// Doing `name + ''` can cause a repl error if the result to `toString` is not a string,
60-
// this is, this will handle objects that misbehave.
61-
name = name + '';
62-
if (!isString(name)) {
63-
throw $parseMinErr('iseccst',
64-
'Cannot convert object to primitive value! '
65-
+ 'Expression: {0}', fullExpression);
66-
}
67-
return name;
57+
// So, to ensure that we are checking the same `name` that JavaScript would use, we cast it
58+
// to a string. It's not always possible. If `name` is an object and its `toString` method is
59+
// 'broken' (doesn't return a string, isn't a function, etc.), an error will be thrown:
60+
//
61+
// TypeError: Cannot convert object to primitive value
62+
//
63+
// For performance reasons, we don't catch this error here and allow it to propagate up the call
64+
// stack. Note that you'll get the same error in JavaScript if you try to access a property using
65+
// such a 'broken' object as a key.
66+
return name + '';
6867
}
6968

7069
function ensureSafeObject(obj, fullExpression) {
@@ -1231,7 +1230,7 @@ ASTCompiler.prototype = {
12311230
},
12321231

12331232
getStringValue: function(item) {
1234-
this.assign(item, 'getStringValue(' + item + ',text)');
1233+
this.assign(item, 'getStringValue(' + item + ')');
12351234
},
12361235

12371236
ensureSafeAssignContext: function(item) {

test/ng/parseSpec.js

+13
Original file line numberDiff line numberDiff line change
@@ -2201,6 +2201,19 @@ describe('parser', function() {
22012201
expect(scope.$eval('true || false || run()')).toBe(true);
22022202
});
22032203

2204+
it('should throw TypeError on using a \'broken\' object as a key to access a property', function() {
2205+
scope.object = {};
2206+
forEach([
2207+
{ toString: 2 },
2208+
{ toString: null },
2209+
{ toString: function() { return {}; } }
2210+
], function(brokenObject) {
2211+
scope.brokenObject = brokenObject;
2212+
expect(function() {
2213+
scope.$eval('object[brokenObject]');
2214+
}).toThrow();
2215+
});
2216+
});
22042217

22052218
it('should support method calls on primitive types', function() {
22062219
scope.empty = '';

0 commit comments

Comments
 (0)