Skip to content

Commit

Permalink
Ensure concatenated properties are frozen.
Browse files Browse the repository at this point in the history
Mutating objects on the prototype (which includes concatenated
properties) is not good. This changes the functionality of concatenated
properties to freeze the resulting array while in debug mode, to make it
clear that mutating them without slicing first is not good.
  • Loading branch information
Robert Jackson committed Sep 30, 2016
1 parent 09140df commit 0224f81
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions packages/ember-metal/lib/mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,20 +143,32 @@ function giveMethodSuper(obj, key, method, values, descs) {

function applyConcatenatedProperties(obj, key, value, values) {
let baseValue = values[key] || obj[key];
let ret;

if (baseValue) {
if ('function' === typeof baseValue.concat) {
if (value === null || value === undefined) {
return baseValue;
ret = baseValue;
} else {
return baseValue.concat(value);
ret = baseValue.concat(value);
}
} else {
return makeArray(baseValue).concat(value);
ret = makeArray(baseValue).concat(value);
}
} else {
return makeArray(value);
ret = makeArray(value);
}

runInDebug(() => {
// it is possible to use concatenatedProperties with strings (which cannot be frozen)
// only freeze objects...
if (typeof ret === 'object' && ret !== null) {
// prevent mutating `concatenatedProperties` array after it is applied
Object.freeze(ret);
}
});

return ret;
}

function applyMergedProperties(obj, key, value, values) {
Expand Down

0 comments on commit 0224f81

Please sign in to comment.