typir
Version:
General purpose type checking library
68 lines • 3.79 kB
TypeScript
/******************************************************************************
* Copyright 2025 TypeFox GmbH
* This program and the accompanying materials are made available under the
* terms of the MIT License, which is available in the project root.
******************************************************************************/
import { TypeGraphListener } from '../graph/type-graph.js';
import { Type } from '../graph/type-node.js';
import { TypirSpecifics, TypirServices } from '../typir.js';
export interface RuleOptions {
/**
* If a rule is associated with a language key, the rule will be executed only for language nodes, which have this language key,
* in order to improve the runtime performance.
* In case of multiple language keys, the rule will be applied to all language nodes having ones of these language keys.
* Rules without a language key ('undefined') are executed for all language nodes.
*/
languageKey: string | string[] | undefined;
/**
* An optional type, if the new rule is dedicated for exactly this type.
* If the given type is removed from the type system, this rule will be automatically removed as well (for all language keys).
* In case of multiple types, this rule will be removed, after all types are removed.
* In case of 'undefined', the rule will never be automatically removed.
*/
boundToType: Type | Type[] | undefined;
}
interface InternalRuleOptions {
languageKeyUndefined: boolean;
languageKeys: string[];
boundToTypes: Type[];
}
export interface RuleCollectorListener<RuleType> {
onAddedRule(rule: RuleType, diffOptions: RuleOptions): void;
onRemovedRule(rule: RuleType, diffOptions: RuleOptions): void;
}
export declare class RuleRegistry<RuleType, Specifics extends TypirSpecifics> implements TypeGraphListener {
/**
* language node type --> rules
* Improves the look-up of related rules, when doing type for a concrete language node.
* All rules are registered at least once in this map, since rules without dedicated language key are registered to 'undefined'. */
protected readonly languageTypeToRules: Map<string | undefined, RuleType[]>;
/**
* type identifier --> -> rules
* Improves the look-up for rules which are bound to types, when these types are removed.
* Only rules which are bound to at least one type in this map, types bound to no types are missing in this map. */
protected readonly typirTypeToRules: Map<string, RuleType[]>;
/**
* rule --> its collected options
* Contains the current set of all options for an rule. */
protected readonly ruleToOptions: Map<RuleType, InternalRuleOptions>;
/** Collects all unique rules, lazily managed. */
protected readonly uniqueRules: Set<RuleType>;
protected readonly listeners: Array<RuleCollectorListener<RuleType>>;
constructor(services: TypirServices<Specifics>);
getRulesByLanguageKey(languageKey: string | undefined): RuleType[];
/** Unique set of all registered rules. */
getUniqueRules(): Set<RuleType>;
isEmpty(): boolean;
getNumberUniqueRules(): number;
protected getRuleOptions(options?: Partial<RuleOptions>): RuleOptions;
addRule(rule: RuleType, givenOptions?: Partial<RuleOptions>): void;
removeRule(rule: RuleType, optionsToRemove?: Partial<RuleOptions>): void;
protected deregisterRuleForLanguageKey(rule: RuleType, languageKey: string | undefined): boolean;
protected getBoundToTypeKey(boundToType?: Type): string;
onRemovedType(type: Type, _key: string): void;
addListener(listener: RuleCollectorListener<RuleType>): void;
removeListener(listener: RuleCollectorListener<RuleType>): void;
}
export {};
//# sourceMappingURL=rule-registration.d.ts.map