From: Jukka Kurkela Date: Tue, 23 Feb 2021 15:54:42 +0000 (+0200) Subject: Plugin options default scriptable/indexable=false (#8497) X-Git-Tag: v3.0.0-beta.12~21 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4c960fb138b75f5664533943014448a3b137aec6;p=thirdparty%2FChart.js.git Plugin options default scriptable/indexable=false (#8497) * Plugin options default scriptable/indexable=false * Update test * Inherti desciptors * Remove unreachable code * remove unintentional change * remove double default --- diff --git a/src/core/core.config.js b/src/core/core.config.js index d2fa83cb0..ef703865c 100644 --- a/src/core/core.config.js +++ b/src/core/core.config.js @@ -314,11 +314,12 @@ export default class Config { * @param {object[]} scopes * @param {object} [context] * @param {string[]} [prefixes] + * @param {{scriptable: boolean, indexable: boolean}} [descriptorDefaults] */ - createResolver(scopes, context, prefixes = ['']) { + createResolver(scopes, context, prefixes = [''], descriptorDefaults) { const {resolver} = getResolver(this._resolverCache, scopes, prefixes); return isObject(context) - ? _attachContext(resolver, isFunction(context) ? context() : context) + ? _attachContext(resolver, context, undefined, descriptorDefaults) : resolver; } } diff --git a/src/core/core.plugins.js b/src/core/core.plugins.js index 5655ba3a9..62cb543bc 100644 --- a/src/core/core.plugins.js +++ b/src/core/core.plugins.js @@ -166,5 +166,5 @@ function createDescriptors(chart, plugins, options, all) { function pluginOpts(config, plugin, opts, context) { const keys = config.pluginScopeKeys(plugin); const scopes = config.getOptionScopes(opts, keys); - return config.createResolver(scopes, context); + return config.createResolver(scopes, context, [''], {scriptable: false, indexable: false}); } diff --git a/src/helpers/helpers.config.js b/src/helpers/helpers.config.js index 3af16a66a..fb60c81c4 100644 --- a/src/helpers/helpers.config.js +++ b/src/helpers/helpers.config.js @@ -74,18 +74,19 @@ export function _createResolver(scopes, prefixes = [''], rootScopes = scopes, fa * @param {object} proxy - The Proxy returned by `_createResolver` * @param {object} context - Context object for scriptable/indexable options * @param {object} [subProxy] - The proxy provided for scriptable options + * @param {{scriptable: boolean, indexable: boolean}} [descriptorDefaults] - Defaults for descriptors * @private */ -export function _attachContext(proxy, context, subProxy) { +export function _attachContext(proxy, context, subProxy, descriptorDefaults) { const cache = { _cacheable: false, _proxy: proxy, _context: context, _subProxy: subProxy, _stack: new Set(), - _descriptors: _descriptors(proxy), - setContext: (ctx) => _attachContext(proxy, ctx, subProxy), - override: (scope) => _attachContext(proxy.override(scope), context, subProxy) + _descriptors: _descriptors(proxy, descriptorDefaults), + setContext: (ctx) => _attachContext(proxy, ctx, subProxy, descriptorDefaults), + override: (scope) => _attachContext(proxy.override(scope), context, subProxy, descriptorDefaults) }; return new Proxy(cache, { /** @@ -138,9 +139,11 @@ export function _attachContext(proxy, context, subProxy) { /** * @private */ -export function _descriptors(proxy) { - const {_scriptable = true, _indexable = true} = proxy; +export function _descriptors(proxy, defaults = {scriptable: true, indexable: true}) { + const {_scriptable = defaults.scriptable, _indexable = defaults.indexable} = proxy; return { + scriptable: _scriptable, + indexable: _indexable, isScriptable: isFunction(_scriptable) ? _scriptable : () => _scriptable, isIndexable: isFunction(_indexable) ? _indexable : () => _indexable }; @@ -177,7 +180,7 @@ function _resolveWithContext(target, prop, receiver) { } if (needsSubResolver(prop, value)) { // if the resolved value is an object, crate a sub resolver for it - value = _attachContext(value, _context, _subProxy && _subProxy[prop]); + value = _attachContext(value, _context, _subProxy && _subProxy[prop], descriptors); } return value; } @@ -199,7 +202,7 @@ function _resolveScriptable(prop, value, target, receiver) { } function _resolveArray(prop, value, target, isIndexable) { - const {_proxy, _context, _subProxy} = target; + const {_proxy, _context, _subProxy, _descriptors: descriptors} = target; if (defined(_context.index) && isIndexable(prop)) { value = value[_context.index % value.length]; @@ -210,7 +213,7 @@ function _resolveArray(prop, value, target, isIndexable) { value = []; for (const item of arr) { const resolver = createSubResolver(scopes, _proxy, prop, item); - value.push(_attachContext(resolver, _context, _subProxy && _subProxy[prop])); + value.push(_attachContext(resolver, _context, _subProxy && _subProxy[prop], descriptors)); } } return value; diff --git a/test/specs/core.plugin.tests.js b/test/specs/core.plugin.tests.js index 9a70f10a7..7168ce928 100644 --- a/test/specs/core.plugin.tests.js +++ b/test/specs/core.plugin.tests.js @@ -361,5 +361,36 @@ describe('Chart.plugins', function() { // The plugin on the chart should only be started once expect(results).toEqual([1]); }); + + it('should default to false for _scriptable, _indexable', function(done) { + const plugin = { + id: 'test', + start: function(chart, args, opts) { + expect(opts.fun).toEqual(jasmine.any(Function)); + expect(opts.fun()).toEqual('test'); + expect(opts.arr).toEqual([1, 2, 3]); + + expect(opts.sub.subfun).toEqual(jasmine.any(Function)); + expect(opts.sub.subfun()).toEqual('subtest'); + expect(opts.sub.subarr).toEqual([3, 2, 1]); + done(); + } + }; + window.acquireChart({ + options: { + plugins: { + test: { + fun: () => 'test', + arr: [1, 2, 3], + sub: { + subfun: () => 'subtest', + subarr: [3, 2, 1], + } + } + } + }, + plugins: [plugin] + }); + }); }); });