From 2d7abff3ef2062c379cb1ef7d73c378a67e7507d Mon Sep 17 00:00:00 2001 From: kurkle Date: Tue, 7 Dec 2021 20:56:23 +0200 Subject: [PATCH 1/4] Pass object from array as value to _fallback --- src/helpers/helpers.config.js | 28 ++++++++++----------- test/specs/helpers.config.tests.js | 40 ++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 14 deletions(-) diff --git a/src/helpers/helpers.config.js b/src/helpers/helpers.config.js index 4d27f9e5b02..93766ea354a 100644 --- a/src/helpers/helpers.config.js +++ b/src/helpers/helpers.config.js @@ -251,12 +251,12 @@ function resolveFallback(fallback, prop, value) { const getScope = (key, parent) => key === true ? parent : typeof key === 'string' ? resolveObjectKey(parent, key) : undefined; -function addScopes(set, parentScopes, key, parentFallback) { +function addScopes(set, parentScopes, key, parentFallback, value) { for (const parent of parentScopes) { const scope = getScope(key, parent); - if (scope) { + if (isObject(scope)) { set.add(scope); - const fallback = resolveFallback(scope._fallback, key, scope); + const fallback = resolveFallback(scope._fallback, key, value); if (defined(fallback) && fallback !== key && fallback !== parentFallback) { // When we reach the descriptor that defines a new _fallback, return that. // The fallback will resume to that new scope. @@ -275,14 +275,21 @@ function createSubResolver(parentScopes, resolver, prop, value) { const rootScopes = resolver._rootScopes; const fallback = resolveFallback(resolver._fallback, prop, value); const allScopes = [...parentScopes, ...rootScopes]; - const set = new Set(); - set.add(value); - let key = addScopesFromKey(set, allScopes, prop, fallback || prop); + const set = new Set([value]); + + function addScopesFromKey(key, fallbackKey, item) { + while (key) { + key = addScopes(set, allScopes, key, fallbackKey, item); + } + return key; + } + + let key = addScopesFromKey(prop, fallback || prop, value); if (key === null) { return false; } if (defined(fallback) && fallback !== prop) { - key = addScopesFromKey(set, allScopes, fallback, key); + key = addScopesFromKey(fallback, key, value); if (key === null) { return false; } @@ -291,13 +298,6 @@ function createSubResolver(parentScopes, resolver, prop, value) { () => subGetTarget(resolver, prop, value)); } -function addScopesFromKey(set, allScopes, key, fallback) { - while (key) { - key = addScopes(set, allScopes, key, fallback); - } - return key; -} - function subGetTarget(resolver, prop, value) { const parent = resolver._getTarget(); if (!(prop in parent)) { diff --git a/test/specs/helpers.config.tests.js b/test/specs/helpers.config.tests.js index 58e295b580b..9ec2749a3f8 100644 --- a/test/specs/helpers.config.tests.js +++ b/test/specs/helpers.config.tests.js @@ -652,6 +652,46 @@ describe('Chart.helpers.config', function() { }); }); + it('should call _fallback with proper value from array when descriptor is object', function() { + const spy = jasmine.createSpy('fallback'); + const descriptors = { + items: { + _fallback: spy + } + }; + const options = { + items: [{test: true}] + }; + const resolver = _attachContext(_createResolver([options, descriptors]), {dymmy: true}); + const item0 = resolver.items[0]; + expect(item0.test).toEqual(true); + expect(spy).toHaveBeenCalledWith('items', options.items[0]); + }); + + it('should call _fallback with proper value from array when descriptor and defaults are objects', function() { + const spy = jasmine.createSpy('fallback'); + const descriptors = { + items: { + _fallback: spy + } + }; + const defaults = { + items: { + type: 'defaultType' + } + }; + const options = { + items: [{test: true}] + }; + const resolver = _createResolver([options, defaults, descriptors]); + // console.warn(resolver.items[0]); + const opts = _attachContext(resolver, {dymmy: true}); + const item0 = opts.items[0]; + console.warn(opts._proxy._scopes); + expect(item0.test).toEqual(true); + expect(spy).toHaveBeenCalledWith('items', options.items[0]); + }); + it('should support overriding options', function() { const options = { fn1: ctx => ctx.index, From 7b7703c0556ad7f178114e4a178f14fecc3987f2 Mon Sep 17 00:00:00 2001 From: kurkle Date: Tue, 7 Dec 2021 21:14:23 +0200 Subject: [PATCH 2/4] cleanup --- test/specs/helpers.config.tests.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/specs/helpers.config.tests.js b/test/specs/helpers.config.tests.js index 9ec2749a3f8..151bad76126 100644 --- a/test/specs/helpers.config.tests.js +++ b/test/specs/helpers.config.tests.js @@ -662,8 +662,9 @@ describe('Chart.helpers.config', function() { const options = { items: [{test: true}] }; - const resolver = _attachContext(_createResolver([options, descriptors]), {dymmy: true}); - const item0 = resolver.items[0]; + const resolver = _createResolver([options, descriptors]); + const opts = _attachContext(resolver, {dymmy: true}); + const item0 = opts.items[0]; expect(item0.test).toEqual(true); expect(spy).toHaveBeenCalledWith('items', options.items[0]); }); @@ -684,7 +685,6 @@ describe('Chart.helpers.config', function() { items: [{test: true}] }; const resolver = _createResolver([options, defaults, descriptors]); - // console.warn(resolver.items[0]); const opts = _attachContext(resolver, {dymmy: true}); const item0 = opts.items[0]; console.warn(opts._proxy._scopes); From a4b6b63217641f02d2101fb76b840d1e68896530 Mon Sep 17 00:00:00 2001 From: kurkle Date: Tue, 7 Dec 2021 21:31:55 +0200 Subject: [PATCH 3/4] reduce changes --- src/helpers/helpers.config.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/helpers/helpers.config.js b/src/helpers/helpers.config.js index 93766ea354a..0f7000e3039 100644 --- a/src/helpers/helpers.config.js +++ b/src/helpers/helpers.config.js @@ -254,7 +254,7 @@ const getScope = (key, parent) => key === true ? parent function addScopes(set, parentScopes, key, parentFallback, value) { for (const parent of parentScopes) { const scope = getScope(key, parent); - if (isObject(scope)) { + if (scope) { set.add(scope); const fallback = resolveFallback(scope._fallback, key, value); if (defined(fallback) && fallback !== key && fallback !== parentFallback) { @@ -277,19 +277,12 @@ function createSubResolver(parentScopes, resolver, prop, value) { const allScopes = [...parentScopes, ...rootScopes]; const set = new Set([value]); - function addScopesFromKey(key, fallbackKey, item) { - while (key) { - key = addScopes(set, allScopes, key, fallbackKey, item); - } - return key; - } - - let key = addScopesFromKey(prop, fallback || prop, value); + let key = addScopesFromKey(set, allScopes, prop, fallback || prop, value); if (key === null) { return false; } if (defined(fallback) && fallback !== prop) { - key = addScopesFromKey(fallback, key, value); + key = addScopesFromKey(set, allScopes, fallback, key, value); if (key === null) { return false; } @@ -298,6 +291,13 @@ function createSubResolver(parentScopes, resolver, prop, value) { () => subGetTarget(resolver, prop, value)); } +function addScopesFromKey(set, allScopes, key, fallbackKey, item) { + while (key) { + key = addScopes(set, allScopes, key, fallbackKey, item); + } + return key; +} + function subGetTarget(resolver, prop, value) { const parent = resolver._getTarget(); if (!(prop in parent)) { From 7c0421dc7c25ae414714e3ddc6e65d375bc449d8 Mon Sep 17 00:00:00 2001 From: kurkle Date: Tue, 7 Dec 2021 21:34:13 +0200 Subject: [PATCH 4/4] reduce even more changes --- src/helpers/helpers.config.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/helpers/helpers.config.js b/src/helpers/helpers.config.js index 0f7000e3039..154d42a4969 100644 --- a/src/helpers/helpers.config.js +++ b/src/helpers/helpers.config.js @@ -275,8 +275,8 @@ function createSubResolver(parentScopes, resolver, prop, value) { const rootScopes = resolver._rootScopes; const fallback = resolveFallback(resolver._fallback, prop, value); const allScopes = [...parentScopes, ...rootScopes]; - const set = new Set([value]); - + const set = new Set(); + set.add(value); let key = addScopesFromKey(set, allScopes, prop, fallback || prop, value); if (key === null) { return false; @@ -291,9 +291,9 @@ function createSubResolver(parentScopes, resolver, prop, value) { () => subGetTarget(resolver, prop, value)); } -function addScopesFromKey(set, allScopes, key, fallbackKey, item) { +function addScopesFromKey(set, allScopes, key, fallback, item) { while (key) { - key = addScopes(set, allScopes, key, fallbackKey, item); + key = addScopes(set, allScopes, key, fallback, item); } return key; }