Skip to content

Commit 18e25d5

Browse files
authored
Report an error when attempting to override an existing message or term. (#177)
1 parent a061484 commit 18e25d5

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

fluent/src/context.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,16 @@ export class MessageContext {
113113
if (id.startsWith("-")) {
114114
// Identifiers starting with a dash (-) define terms. Terms are private
115115
// and cannot be retrieved from MessageContext.
116+
if (this._terms.has(id)) {
117+
errors.push(`Attempt to override an existing term: "${id}"`);
118+
continue;
119+
}
116120
this._terms.set(id, entries[id]);
117121
} else {
122+
if (this._messages.has(id)) {
123+
errors.push(`Attempt to override an existing message: "${id}"`);
124+
continue;
125+
}
118126
this._messages.set(id, entries[id]);
119127
}
120128
}

fluent/test/context_test.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,19 @@ suite('Context', function() {
5353
});
5454

5555

56-
test('overwrites existing messages if the ids are the same', function() {
57-
ctx.addMessages(ftl`
56+
test('does not overwrite existing messages if the ids are the same', function() {
57+
const errors = ctx.addMessages(ftl`
5858
foo = New Foo
5959
`);
6060

61+
// Attempt to overwrite error reported
62+
assert.equal(errors.length, 1);
63+
6164
assert.equal(ctx._messages.size, 2);
6265

6366
const msg = ctx.getMessage('foo');
6467
const val = ctx.format(msg, args, errs);
65-
assert.equal(val, 'New Foo');
68+
assert.equal(val, 'Foo');
6669
assert.equal(errs.length, 0);
6770
});
6871
});

0 commit comments

Comments
 (0)