Skip to content

Commit

Permalink
Skip write to 'name' property if not [[writable]]
Browse files Browse the repository at this point in the history
Resolves: sinonjs#2203
  • Loading branch information
sjoseph7 committed Oct 18, 2020
1 parent 1aa75b0 commit 9c18996
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/sinon/util/core/extend.js
Expand Up @@ -73,8 +73,8 @@ function extendCommon(target, sources, doCopy) {
return target;
}

/** Public: Extend target in place with all (own) properties from sources in-order. Thus, last source will
* override properties in previous sources.
/** Public: Extend target in place with all (own) properties, except 'name' when [[writable]] is false,
* from sources in-order. Thus, last source will override properties in previous sources.
*
* @arg {Object} target - The Object to extend
* @arg {Object[]} sources - Objects to copy properties from.
Expand All @@ -85,7 +85,18 @@ module.exports = function extend(target /*, sources */) {
var sources = slice(arguments, 1);

return extendCommon(target, sources, function copyValue(dest, source, prop) {
dest[prop] = source[prop];
var destOwnPropertyDescriptor = Object.getOwnPropertyDescriptor(dest, prop);
var sourceOwnPropertyDescriptor = Object.getOwnPropertyDescriptor(source, prop);

// If the property is 'name' but is not [[writable]], skip it
if (prop !== "name" || destOwnPropertyDescriptor.writable) {
Object.defineProperty(dest, prop, {
configurable: sourceOwnPropertyDescriptor.configurable,
enumerable: sourceOwnPropertyDescriptor.enumerable,
writable: sourceOwnPropertyDescriptor.writable,
value: sourceOwnPropertyDescriptor.value
});
}
});
};

Expand Down

0 comments on commit 9c18996

Please sign in to comment.