Skip to content

Commit 7fc244a

Browse files
committed
fix: throw duplicate error on extraction
1 parent 0693089 commit 7fc244a

3 files changed

Lines changed: 28 additions & 10 deletions

File tree

packages/babel-preset/intl-preset.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,30 @@ const path = require('path');
33

44
const { interpolateName } = require('@formatjs/ts-transformer');
55
const intlPlugin = require('babel-plugin-formatjs').default;
6+
const stringify = require('fast-json-stable-stringify');
7+
8+
function mergeDuplicates(messages) {
9+
const seen = new Map();
10+
11+
return messages.filter((message) => {
12+
const other = seen.get(message.id);
13+
14+
seen.set(message.id, message);
15+
16+
if (!other) return true;
17+
18+
if (
19+
stringify(message.description) !== stringify(other.description) ||
20+
message.defaultMessage !== other.defaultMessage
21+
) {
22+
throw new Error(
23+
`Duplicate message id: "${message.id}", but the \`description\` and/or \`defaultMessage\` are different.`,
24+
);
25+
}
26+
// if they are the same, filter this one out
27+
return false;
28+
});
29+
}
630

731
module.exports = function intlPreset(_, options = {}) {
832
const {
@@ -56,6 +80,8 @@ module.exports = function intlPreset(_, options = {}) {
5680

5781
mkdirSync(path.join(messagesDir, dir), { recursive: true });
5882

83+
messages = mergeDuplicates(messages);
84+
5985
writeFileSync(
6086
path.join(messagesDir, dir, `${name}.json`),
6187
JSON.stringify(messages, null, 2),

packages/babel-preset/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"babel-plugin-formatjs": "^10.3.25",
3838
"babel-plugin-polyfill-corejs3": "^0.5.3",
3939
"browserslist": "^4.21.3",
40+
"fast-json-stable-stringify": "^2.1.0",
4041
"lodash": "^4.17.21"
4142
},
4243
"devDependencies": {

yarn.lock

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,6 @@
2121
text-table "^0.2.0"
2222
yargs "^17.1.1"
2323

24-
"@4c/jest-preset@^1.8.0":
25-
version "1.8.0"
26-
resolved "https://registry.yarnpkg.com/@4c/jest-preset/-/jest-preset-1.8.0.tgz#79e1fa5dfefa642808c6d65ab7908ea4e9394ec6"
27-
integrity sha512-qBrBrbR/YiOleNJErwaMqCP+cw4YGqXF3R+EIgLg/La28sf9fuhCaYjN9OrUBLjKwF6HkdMfPrd7M0iSV9NPHA==
28-
dependencies:
29-
babel-jest "^28.1.3"
30-
find-up "^5.0.0"
31-
find-yarn-workspace-root "^2.0.0"
32-
3324
"@ampproject/remapping@^2.1.0":
3425
version "2.2.0"
3526
resolved "https://registry.yarnpkg.com/@ampproject/remapping/-/remapping-2.2.0.tgz#56c133824780de3174aed5ab6834f3026790154d"
@@ -4626,7 +4617,7 @@ fast-glob@^3.2.9:
46264617
merge2 "^1.3.0"
46274618
micromatch "^4.0.4"
46284619

4629-
fast-json-stable-stringify@^2.0.0:
4620+
fast-json-stable-stringify@^2.0.0, fast-json-stable-stringify@^2.1.0:
46304621
version "2.1.0"
46314622
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz#874bf69c6f404c2b5d99c481341399fd55892633"
46324623
integrity sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==

0 commit comments

Comments
 (0)