Skip to content

Commit

Permalink
Fix #2203: skip writing 'name' property if not writable (#2304)
Browse files Browse the repository at this point in the history
* Add test for #2203
* Skip writing 'name' property if not writable
  • Loading branch information
sjoseph7 committed Oct 28, 2020
1 parent d01f74b commit f981192
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
18 changes: 15 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,19 @@ 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 (prop === "name" && !destOwnPropertyDescriptor.writable) {
return;
}

Object.defineProperty(dest, prop, {
configurable: sourceOwnPropertyDescriptor.configurable,
enumerable: sourceOwnPropertyDescriptor.enumerable,
writable: sourceOwnPropertyDescriptor.writable,
value: sourceOwnPropertyDescriptor.value
});
});
};

Expand Down
28 changes: 28 additions & 0 deletions test/extend-test.js
Expand Up @@ -77,4 +77,32 @@ describe("extend", function() {

assert.equals(result, expected);
});

context("when 'name' property is not writable", function() {
it("does not attempt to write to the property", function() {
var object1 = { prop1: null };

Object.defineProperty(object1, "name", {
configurable: false,
enumerable: true,
value: "not-writable",
writable: false
});

var object2 = {
prop2: "hey",
name: "write-attempt"
};

var result = extend(object1, object2);

var expected = {
prop1: null,
prop2: "hey",
name: "not-writable"
};

assert.equals(result, expected);
});
});
});

0 comments on commit f981192

Please sign in to comment.