Skip to content

Commit 3498741

Browse files
feat(mf2): Add duplicate-variant error (unicode-org/message-format-wg#853)
1 parent 856667c commit 3498741

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

packages/mf2-messageformat/src/data-model/validate.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ import { visit } from './visit.js';
2828
* - **Duplicate Option Name**:
2929
* The same _identifier_ appears as the name of more than one _option_ in the same _expression_.
3030
*
31+
* - **Duplicate Variant**:
32+
* The same list of _keys_ is used for more than one _variant_.
33+
*
3134
* @returns The sets of runtime `functions` and `variables` used by the message.
3235
* @beta
3336
*/
@@ -52,6 +55,7 @@ export function validate(
5255
const functions = new Set<string>();
5356
const localVars = new Set<string>();
5457
const variables = new Set<string>();
58+
const variants = new Set<string>();
5559

5660
let setArgAsDeclared = true;
5761
visit(msg, {
@@ -116,6 +120,11 @@ export function validate(
116120
variant(variant) {
117121
const { keys } = variant;
118122
if (keys.length !== selectorCount) onError('key-mismatch', variant);
123+
const strKeys = JSON.stringify(
124+
keys.map(key => (key.type === 'literal' ? key.value : 0))
125+
);
126+
if (variants.has(strKeys)) onError('duplicate-variant', variant);
127+
else variants.add(strKeys);
119128
missingFallback &&= keys.every(key => key.type === '*') ? null : variant;
120129
}
121130
});

packages/mf2-messageformat/src/errors.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export class MessageSyntaxError extends MessageError {
3232
| 'bad-input-expression'
3333
| 'duplicate-declaration'
3434
| 'duplicate-option'
35+
| 'duplicate-variant'
3536
| 'extra-content'
3637
| 'key-mismatch'
3738
| 'parse-error'
@@ -64,6 +65,7 @@ export class MessageDataModelError extends MessageSyntaxError {
6465
declare type:
6566
| 'duplicate-declaration'
6667
| 'duplicate-option'
68+
| 'duplicate-variant'
6769
| 'key-mismatch'
6870
| 'missing-fallback'
6971
| 'missing-selector-annotation';

test/mfwg-test-utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export function testName({ src, locale, params }: Test) {
4747
const dataModelErrors = [
4848
'duplicate-declaration',
4949
'duplicate-option-name',
50+
'duplicate-variant',
5051
'missing-fallback-variant',
5152
'missing-selector-annotation',
5253
'variant-key-mismatch'

0 commit comments

Comments
 (0)