Skip to content

Commit 1d4b41a

Browse files
committed
Prevent explicit first argument deep copy
1 parent dac95fd commit 1d4b41a

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

src/core/core.controller.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@ defaults._set('global', {
3535
responsiveAnimationDuration: 0
3636
});
3737

38-
function mergeScaleConfig(/* objects ... */) {
39-
return helpers.merge(helpers.clone(arguments[0]), [].slice.call(arguments, 1), {
38+
/**
39+
* Recursively merge the given config objects as the `scales` options by
40+
* incorporating scale defaults in `xAxes` and `yAxes` array items, then
41+
* returns a deep copy of the result, thus doesn't alter inputs.
42+
*/
43+
function mergeScaleConfig(/* config objects ... */) {
44+
return helpers.merge({}, Array.prototype.slice.call(arguments), {
4045
merger: function(key, target, source, options) {
4146
if (key === 'xAxes' || key === 'yAxes') {
4247
var slen = source[key].length;
@@ -70,8 +75,13 @@ function mergeScaleConfig(/* objects ... */) {
7075
});
7176
}
7277

73-
function mergeConfig(/* objects ... */) {
74-
return helpers.merge(helpers.clone(arguments[0]), [].slice.call(arguments, 1), {
78+
/**
79+
* Recursively merge the given config objects as the root options by handling
80+
* default scale options for the `scales` and `scale` properties, then returns
81+
* a deep copy of the result, thus doesn't alter inputs.
82+
*/
83+
function mergeConfig(/* config objects ... */) {
84+
return helpers.merge({}, Array.prototype.slice.call(arguments), {
7585
merger: function(key, target, source, options) {
7686
var tval = target[key] || {};
7787
var sval = source[key];

test/specs/core.controller.tests.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,36 @@ describe('Chart', function() {
271271
_jasmineCheckE: 'e2'
272272
}));
273273
});
274+
275+
it('should not alter defaults when merging config', function() {
276+
var chart = acquireChart({
277+
type: 'line',
278+
options: {
279+
_jasmineCheck: 42,
280+
scales: {
281+
xAxes: [{
282+
id: 'foo',
283+
type: 'linear',
284+
_jasmineCheck: 42,
285+
}],
286+
yAxes: [{
287+
id: 'bar',
288+
type: 'category',
289+
_jasmineCheck: 42,
290+
}]
291+
}
292+
}
293+
});
294+
295+
expect(chart.options._jasmineCheck).toBeDefined();
296+
expect(chart.scales.foo.options._jasmineCheck).toBeDefined();
297+
expect(chart.scales.bar.options._jasmineCheck).toBeDefined();
298+
299+
expect(Chart.defaults.line._jasmineCheck).not.toBeDefined();
300+
expect(Chart.defaults.global._jasmineCheck).not.toBeDefined();
301+
expect(Chart.scaleService.defaults.linear._jasmineCheck).not.toBeDefined();
302+
expect(Chart.scaleService.defaults.category._jasmineCheck).not.toBeDefined();
303+
});
274304
});
275305

276306
describe('config.options.responsive: false', function() {

0 commit comments

Comments
 (0)