Skip to content

Commit

Permalink
Add description support for record type messages (#62)
Browse files Browse the repository at this point in the history
* Add [@intl.description] annotation

Fmt

* Bump version to 0.9.1

* Update intl.description matcher comment

* Update description
  • Loading branch information
ixzzd authored Nov 14, 2020
1 parent 48d791c commit 800723f
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 6 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ module Msg = {
};
```

You also can pass descriptions to the records with:
```reason
let foo = [@intl.description "Hello description"] {id: "message.hello", defaultMessage: "Hello"};
```

## Message Definition (bs-react-intl 1.x)

Formatted messages may be defined in your source files in one of the following ways:
Expand Down
2 changes: 1 addition & 1 deletion bin/Version.re
Original file line number Diff line number Diff line change
@@ -1 +1 @@
let version = "0.9.0";
let version = "0.9.1";
24 changes: 22 additions & 2 deletions lib/ExtractionIterator.re
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ let extractMessageFromLabels = (callback, labels) => {
Message.fromStringMap(map) |> Option.iter(callback);
};

let extractMessageFromRecord = (callback, fields) => {
let extractMessageFromRecord = (~description=?, callback, fields) => {
let map =
fields
|> List.fold_left(
Expand All @@ -35,7 +35,7 @@ let extractMessageFromRecord = (callback, fields) => {
StringMap.empty,
);

Message.fromStringMap(map) |> Option.iter(callback);
Message.fromStringMap(~description?, map) |> Option.iter(callback);
};

let extractMessagesFromRecords = (callback, records) =>
Expand Down Expand Up @@ -70,6 +70,26 @@ let extractMessagesFromValueBindings = (callback, valueBindings: list(value_bind
valueBindings
|> List.iter(valueBinding =>
switch (valueBinding) {
// Match with [@intl.description "i am description"] let foo = { ... };
| {
pvb_pat: {ppat_desc: Ppat_var(_)},
pvb_expr: {
pexp_desc: Pexp_record(fields, None),
pexp_attributes: [
{
attr_name: {txt: "intl.description"},
attr_payload:
PStr([
{
pstr_desc: Pstr_eval({pexp_desc: Pexp_constant(Pconst_string(description, _))}, _),
pstr_loc: _,
},
]),
},
],
},
} =>
extractMessageFromRecord(~description, callback, fields)
| {pvb_pat: {ppat_desc: Ppat_var(_)}, pvb_expr: {pexp_desc: Pexp_record(fields, None)}} =>
extractMessageFromRecord(callback, fields)
| _ => ()
Expand Down
4 changes: 2 additions & 2 deletions lib/Message.re
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ type t = {

let compare = (a, b) => compare(a.id, b.id);

let fromStringMap = map => {
let fromStringMap = (~description=?, map) => {
let id = map |> StringMap.find_opt("id");
let defaultMessage = map |> StringMap.find_opt("defaultMessage");
let description = map |> StringMap.find_opt("description");
let description = description |> Option.is_none ? map |> StringMap.find_opt("description") : description;
switch (id, defaultMessage) {
| (Some(id), Some(defaultMessage)) => Some({id, defaultMessage, description})
| _ => None
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bs-react-intl-extractor",
"version": "0.9.0",
"version": "0.9.1",
"description": "Message extractor for bs-react-intl",
"author": "Christoph Knittel <christoph.knittel@cca.io>",
"license": "MIT",
Expand Down
5 changes: 5 additions & 0 deletions test/__snapshots__/Extract.a27e9fd5.0.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ Extract › full
\"description\": \"Description for message 1.7\"
},
{ \"id\": \"test1.msg1.8\", \"defaultMessage\": \"This is message 1.8\" },
{
\"id\": \"test1.msg1.9\",
\"defaultMessage\": \"This is message 1.9\",
\"description\": \"Description for message 1.9\"
},
{ \"id\": \"test1.msg2.1\", \"defaultMessage\": \"This is message 2.1\" },
{
\"id\": \"test1.msg2.2\",
Expand Down
8 changes: 8 additions & 0 deletions testData/test1/Test_1_1.re
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,11 @@ module Msg2 = {

let ignored2 = {id: "test1.ignored1.2", defaultMessage: "This message is ignored"};
};

module Msg3 = {
open ReactIntl;

[@intl.messages];

let msg19 = [@intl.description "Description for message 1.9"] {id: "test1.msg1.9", defaultMessage: "This is message 1.9"};
};

0 comments on commit 800723f

Please sign in to comment.