Skip to content

Commit

Permalink
Release v3.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonDegraeve committed Sep 28, 2020
1 parent 0edc161 commit 1828919
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
2 changes: 1 addition & 1 deletion esy.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "res-react-intl",
"version": "3.0.2",
"version": "3.0.3",
"description": "Reason/OCaml PPX generating ReactIntl `id` from `defaultMessage`",
"author": "Simon Degraeve <simon.degraeve@gmail.com>",
"license": "MIT",
Expand Down
42 changes: 32 additions & 10 deletions lib/Mapper.re
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ let printExpression = (expr: expression) =>
Printast.expression(0, Format.str_formatter, expr);

let replaceIdFromLabels = labels => {
let id = ref("");
let id = ref(`None);

labels
|> List.fold_left(
(acc, assoc) =>
Expand All @@ -23,14 +24,20 @@ let replaceIdFromLabels = labels => {
{pexp_desc: Pexp_constant(Pconst_string(value, _)), _},
)
when key == "id" =>
id.contents = value;
id.contents = `Id(value);

acc;
| (
Labelled(key),
{pexp_desc: Pexp_constant(Pconst_string(value, _)), _},
) as label
when key == "defaultMessage" =>
id.contents = id.contents == "" ? value : id.contents;
id.contents = (
switch (id.contents) {
| `None => `Message(value)
| value => value
}
);
List.append(acc, [label]);
| label => List.append(acc, [label])
},
Expand All @@ -41,8 +48,13 @@ let replaceIdFromLabels = labels => {
[
(
Labelled("id"),
id.contents
|> generateId
(
switch (id.contents) {
| `Message(value) => value |> generateId
| `Id(value) => value
| `None => ""
}
)
|> Ast_helper.Const.string
|> Ast_helper.Exp.constant,
),
Expand All @@ -53,7 +65,7 @@ let replaceIdFromLabels = labels => {

let replaceIdFromRecord =
(fields: list((Asttypes.loc(Longident.t), expression))) => {
let id = ref("");
let id = ref(`None);

fields
|> List.fold_left(
Expand All @@ -64,14 +76,19 @@ let replaceIdFromRecord =
{pexp_desc: Pexp_constant(Pconst_string(value, _)), _},
)
when key == "id" =>
id.contents = value;
id.contents = `Id(value);
acc;
| (
{txt: Lident(key), _},
{pexp_desc: Pexp_constant(Pconst_string(value, _)), _},
) as field
when key == "defaultMessage" =>
id.contents = id.contents == "" ? value : id.contents;
id.contents = (
switch (id.contents) {
| `None => `Message(value)
| value => value
}
);
List.append(acc, [field]);
| field => List.append(acc, [field])
},
Expand All @@ -80,8 +97,13 @@ let replaceIdFromRecord =
|> List.append([
(
{txt: Lident("id"), loc: Ast_helper.default_loc.contents},
id.contents
|> generateId
(
switch (id.contents) {
| `Message(value) => value |> generateId
| `Id(value) => value
| `None => ""
}
)
|> Ast_helper.Const.string
|> Ast_helper.Exp.constant,
),
Expand Down

0 comments on commit 1828919

Please sign in to comment.