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 2 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
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
});
}
sjoseph7 marked this conversation as resolved.
Show resolved Hide resolved
});
};

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

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

it("does not attempt to write to 'name' property if it is not [[writable]]", 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);
});
sjoseph7 marked this conversation as resolved.
Show resolved Hide resolved
});