Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip write to 'name' property if not [[writable]] #2304

Merged
merged 5 commits into from Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
});
});
});