From 800723f8018dbbf9d2b81e7c6addc227e7a1709f Mon Sep 17 00:00:00 2001 From: Egor Chemokhonenko <1germes@gmail.com> Date: Sat, 14 Nov 2020 14:23:30 +0200 Subject: [PATCH] Add description support for record type messages (#62) * Add [@intl.description] annotation Fmt * Bump version to 0.9.1 * Update intl.description matcher comment * Update description --- README.md | 5 ++++ bin/Version.re | 2 +- lib/ExtractionIterator.re | 24 +++++++++++++++++-- lib/Message.re | 4 ++-- package.json | 2 +- .../__snapshots__/Extract.a27e9fd5.0.snapshot | 5 ++++ testData/test1/Test_1_1.re | 8 +++++++ 7 files changed, 44 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index c17b6ce..d37f3dd 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/bin/Version.re b/bin/Version.re index 72e826e..b3ed4a2 100644 --- a/bin/Version.re +++ b/bin/Version.re @@ -1 +1 @@ -let version = "0.9.0"; +let version = "0.9.1"; diff --git a/lib/ExtractionIterator.re b/lib/ExtractionIterator.re index f9a847b..ea07712 100644 --- a/lib/ExtractionIterator.re +++ b/lib/ExtractionIterator.re @@ -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( @@ -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) => @@ -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) | _ => () diff --git a/lib/Message.re b/lib/Message.re index 318a06e..e53ba7d 100644 --- a/lib/Message.re +++ b/lib/Message.re @@ -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 diff --git a/package.json b/package.json index 0eb1474..6bb89ff 100644 --- a/package.json +++ b/package.json @@ -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 ", "license": "MIT", diff --git a/test/__snapshots__/Extract.a27e9fd5.0.snapshot b/test/__snapshots__/Extract.a27e9fd5.0.snapshot index 3e5e145..7a3c93d 100644 --- a/test/__snapshots__/Extract.a27e9fd5.0.snapshot +++ b/test/__snapshots__/Extract.a27e9fd5.0.snapshot @@ -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\", diff --git a/testData/test1/Test_1_1.re b/testData/test1/Test_1_1.re index 118c2ee..4394a0c 100644 --- a/testData/test1/Test_1_1.re +++ b/testData/test1/Test_1_1.re @@ -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"}; +};