Skip to content

Commit

Permalink
fix: throw duplicate error on extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Aug 29, 2022
1 parent 0693089 commit 7fc244a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
26 changes: 26 additions & 0 deletions packages/babel-preset/intl-preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,30 @@ const path = require('path');

const { interpolateName } = require('@formatjs/ts-transformer');
const intlPlugin = require('babel-plugin-formatjs').default;
const stringify = require('fast-json-stable-stringify');

function mergeDuplicates(messages) {
const seen = new Map();

return messages.filter((message) => {
const other = seen.get(message.id);

seen.set(message.id, message);

if (!other) return true;

if (
stringify(message.description) !== stringify(other.description) ||
message.defaultMessage !== other.defaultMessage
) {
throw new Error(
`Duplicate message id: "${message.id}", but the \`description\` and/or \`defaultMessage\` are different.`,
);
}
// if they are the same, filter this one out
return false;
});
}

module.exports = function intlPreset(_, options = {}) {
const {
Expand Down Expand Up @@ -56,6 +80,8 @@ module.exports = function intlPreset(_, options = {}) {

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

messages = mergeDuplicates(messages);

writeFileSync(
path.join(messagesDir, dir, `${name}.json`),
JSON.stringify(messages, null, 2),
Expand Down
1 change: 1 addition & 0 deletions packages/babel-preset/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"babel-plugin-formatjs": "^10.3.25",
"babel-plugin-polyfill-corejs3": "^0.5.3",
"browserslist": "^4.21.3",
"fast-json-stable-stringify": "^2.1.0",
"lodash": "^4.17.21"
},
"devDependencies": {
Expand Down
11 changes: 1 addition & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,6 @@
text-table "^0.2.0"
yargs "^17.1.1"

"@4c/jest-preset@^1.8.0":
version "1.8.0"
resolved "https://registry.yarnpkg.com/@4c/jest-preset/-/jest-preset-1.8.0.tgz#79e1fa5dfefa642808c6d65ab7908ea4e9394ec6"
integrity sha512-qBrBrbR/YiOleNJErwaMqCP+cw4YGqXF3R+EIgLg/La28sf9fuhCaYjN9OrUBLjKwF6HkdMfPrd7M0iSV9NPHA==
dependencies:
babel-jest "^28.1.3"
find-up "^5.0.0"
find-yarn-workspace-root "^2.0.0"

"@ampproject/remapping@^2.1.0":
version "2.2.0"
resolved "https://registry.yarnpkg.com/@ampproject/remapping/-/remapping-2.2.0.tgz#56c133824780de3174aed5ab6834f3026790154d"
Expand Down Expand Up @@ -4626,7 +4617,7 @@ fast-glob@^3.2.9:
merge2 "^1.3.0"
micromatch "^4.0.4"

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

0 comments on commit 7fc244a

Please sign in to comment.