2022-10-03 21:19:54 +00:00
|
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import type { MessageFormatElement } from '@formatjs/icu-messageformat-parser';
|
|
|
|
import { Location } from '@formatjs/icu-messageformat-parser';
|
|
|
|
import type { Visitor } from './traverse';
|
|
|
|
import { traverse } from './traverse';
|
|
|
|
|
|
|
|
export type Element = MessageFormatElement;
|
|
|
|
export { Location };
|
|
|
|
|
|
|
|
export type Context = {
|
|
|
|
messageId: string;
|
2023-04-04 19:05:50 +00:00
|
|
|
report(
|
|
|
|
message: string,
|
|
|
|
location: Location | void,
|
|
|
|
locationOffset?: number
|
|
|
|
): void;
|
2022-10-03 21:19:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export type RuleFactory = {
|
|
|
|
(context: Context): Visitor;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type Rule = {
|
|
|
|
id: string;
|
|
|
|
run(elements: Array<MessageFormatElement>, context: Context): void;
|
|
|
|
};
|
|
|
|
|
|
|
|
export function rule(id: string, ruleFactory: RuleFactory): Rule {
|
|
|
|
return {
|
|
|
|
id,
|
|
|
|
run(elements, context) {
|
2023-04-04 19:05:50 +00:00
|
|
|
traverse(null, elements, ruleFactory(context));
|
2022-10-03 21:19:54 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|