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

Plugin options default scriptable/indexable=false #8497

Merged
merged 6 commits into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/core/core.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/core.plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}
21 changes: 12 additions & 9 deletions src/helpers/helpers.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
/**
Expand Down Expand Up @@ -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
};
Expand Down Expand Up @@ -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;
}
Expand All @@ -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];
Expand All @@ -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;
Expand Down
31 changes: 31 additions & 0 deletions test/specs/core.plugin.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
});
});
});
});