Skip to content

Commit

Permalink
Avoid class-name based contribution filtering
Browse files Browse the repository at this point in the history
Update example to avoid class name based contribution filtering.
Class names are minified in production which breaks the contribution filter.

Use instance of check instead. 
Also mention this in the `FilterContribution` JS-Doc
  • Loading branch information
tortmayr committed Nov 27, 2023
1 parent 6004188 commit 4aba24a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
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;
}

0 comments on commit 4aba24a

Please sign in to comment.