Navigation Menu

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

extend is not handling property descriptors with accessors #2387

Closed
marcospont opened this issue Jul 8, 2021 · 3 comments
Closed

extend is not handling property descriptors with accessors #2387

marcospont opened this issue Jul 8, 2021 · 3 comments

Comments

@marcospont
Copy link

marcospont commented Jul 8, 2021

The 'extend' function from 'util/core' is not handling property descriptors using accessors properly.

The issue was introduced by the following commit: f981192

The below test can be used to duplicate the issue. The object 'obj' has a 'pub' property that is defined using a getter and a setter. Once 'extend' is called, the property descriptor is recreated using 'value' and ignoring the accessors.

import extend from 'sinon/lib/sinon/util/core/extend';

describe('extend', () => {
	it('must preserve accessors', () => {
		const obj = {
			_priv: 1
		};

		Object.defineProperty(obj, 'pub', {
			configurable: true,
			get: () => obj._priv,
			set: value => {
				obj._priv = value;
			}
		});

		const newObj = {};

		extend(newObj, obj);

		expect(newObj['pub']).toBe(1);
	});
});
@fatso83
Copy link
Contributor

fatso83 commented Jul 9, 2021

Good find!

@sauravazad
Copy link
Contributor

There is an inherent mistake in the implementation as it does not distinguishes between Data property and Accessor property

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#properties

  • Data property
    Associates a key with a value, and has the following attributes:

Attributes of a data property

Attribute Type Description Default value
[[Value]] Any JavaScript type The value retrieved by a get access of the property. undefined
[[Writable]] Boolean If false, the property's [[Value]] cannot be changed. false
[[Enumerable]] Boolean If true, the property will be enumerated in for...in loops.See also Enumerability and ownership of properties. false
[[Configurable]] Boolean If false, the property cannot be deleted, cannot be changed to an accessor property, and attributes other than [[Value]] and [[Writable]] cannot be changed. false
  • Accessor property
    Associates a key with one of two accessor functions (get and set) to retrieve or store a value, and has the following attributes:

Attributes of an accessor property

Attribute Type Description Default value
[[Get]] Function object or undefined The function is called with an empty argument list and retrieves the property value whenever a get access to the value is performed.See also get. undefined
[[Set]] Function object or undefined The function is called with an argument that contains the assigned value and is executed whenever a specified property is attempted to be changed.See also set. undefined
[[Enumerable]] Boolean If true, the property will be enumerated in for...in loops. false
[[Configurable]] Boolean If false, the property can't be deleted and can't be changed to a data property. false

I have raised a PR to fix the behavior #2391.

@mroderick
Copy link
Member

This has been fixed by #2391 and published to the npm registry as sinon@11.1.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants