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

Avoid class-name based contribution filtering #13103

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************

import { Command, CommandContribution, CommandRegistry, FilterContribution, ContributionFilterRegistry, bindContribution, Filter } from '@theia/core/lib/common';
import { Command, CommandContribution, CommandRegistry, ContributionFilterRegistry, FilterContribution, bindContribution } from '@theia/core/lib/common';
import { injectable, interfaces } from '@theia/core/shared/inversify';

export namespace SampleFilteredCommand {
Expand Down Expand Up @@ -58,8 +58,8 @@ export class SampleFilterAndCommandContribution implements FilterContribution, C
contrib => contrib.constructor !== this.constructor
]);
registry.addFilters('*', [
// filter a contribution based on its class name
filterClassName(name => name !== 'SampleFilteredCommandContribution')
// filter a contribution based on its class type
contrib => !(contrib instanceof SampleFilteredCommandContribution)
]);
}
}
Expand All @@ -69,12 +69,3 @@ export function bindSampleFilteredCommandContribution(bind: interfaces.Bind): vo
bind(SampleFilterAndCommandContribution).toSelf().inSingletonScope();
bindContribution(bind, SampleFilterAndCommandContribution, [CommandContribution, FilterContribution]);
}

function filterClassName(filter: Filter<string>): Filter<Object> {
return object => {
const className = object?.constructor?.name;
return className
? filter(className)
: false;
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ export const FilterContribution = Symbol('FilterContribution');
export interface FilterContribution {
/**
* Use the registry to register your contribution filters.
* * Note that filtering contributions based on their class (constructor) name is discouraged.
* Class names are minified in production builds and therefore not reliable.
* Use instance of checks or direct constructor comparison instead:
*
* ```ts
* registry.addFilters('*', [
* contrib => !(contrib instanceof SampleFilteredCommandContribution)
* ]);
* ```
*/
registerContributionFilters(registry: ContributionFilterRegistry): void;
}
Loading