|
1 | 1 | open Ast_iterator;
|
2 | 2 |
|
| 3 | +open Asttypes; |
| 4 | + |
3 | 5 | open Parsetree;
|
4 | 6 |
|
5 |
| -let getLabelString = (key, labels) => |
6 |
| - switch (labels |> List.assoc_opt(Asttypes.Labelled(key))) { |
7 |
| - | Some({pexp_desc: Pexp_constant(Pconst_string(text, _))}) => Some(text) |
8 |
| - | _ => None |
9 |
| - }; |
10 |
| - |
11 |
| -let getMessage = expr => |
12 |
| - switch (expr) { |
13 |
| - | { |
14 |
| - pexp_desc: |
15 |
| - Pexp_apply( |
16 |
| - { |
17 |
| - pexp_desc: Pexp_ident({txt: Ldot(Ldot(Lident("ReactIntl"), "FormattedMessage"), "createElement"), loc}), |
18 |
| - }, |
19 |
| - labels, |
20 |
| - ), |
21 |
| - } => |
22 |
| - let id = labels |> getLabelString("id"); |
23 |
| - let defaultMessage = labels |> getLabelString("defaultMessage"); |
24 |
| - let description = labels |> getLabelString("description"); |
25 |
| - switch (id, defaultMessage) { |
26 |
| - | (Some(id), Some(defaultMessage)) => Some({Message.id, defaultMessage, description}) |
27 |
| - | _ => None |
28 |
| - }; |
29 |
| - | _ => None |
30 |
| - }; |
| 7 | +open Longident; |
| 8 | + |
| 9 | +module StringMap = Map.Make(String); |
| 10 | + |
| 11 | +let extractMessageFromLabels = labels => { |
| 12 | + let map = ref(StringMap.empty); |
| 13 | + labels |
| 14 | + |> List.iter(assoc => |
| 15 | + switch (assoc) { |
| 16 | + | (Asttypes.Labelled(key), {pexp_desc: Pexp_constant(Pconst_string(value, _))}) => |
| 17 | + map := map^ |> StringMap.add(key, value) |
| 18 | + | _ => () |
| 19 | + } |
| 20 | + ); |
| 21 | + Message.fromStringMap(map^); |
| 22 | +}; |
| 23 | + |
| 24 | +let extractMessageFromRecord = fields => { |
| 25 | + let map = ref(StringMap.empty); |
| 26 | + fields |
| 27 | + |> List.iter(field => |
| 28 | + switch (field) { |
| 29 | + | ({txt: Lident(key)}, {pexp_desc: Pexp_constant(Pconst_string(value, _))}) => |
| 30 | + map := map^ |> StringMap.add(key, value) |
| 31 | + | _ => () |
| 32 | + } |
| 33 | + ); |
| 34 | + Message.fromStringMap(map^); |
| 35 | +}; |
| 36 | + |
| 37 | +let extractMessagesFromRecords = (callback, records) => |
| 38 | + records |
| 39 | + |> List.iter(field => |
| 40 | + switch (field) { |
| 41 | + | ( |
| 42 | + {txt: Lident(s)}, |
| 43 | + { |
| 44 | + pexp_desc: |
| 45 | + Pexp_extension(( |
| 46 | + {txt: "bs.obj"}, |
| 47 | + PStr([{pstr_desc: Pstr_eval({pexp_desc: Pexp_record(fields, _)}, _), pstr_loc: _}]), |
| 48 | + )), |
| 49 | + }, |
| 50 | + ) => |
| 51 | + switch (extractMessageFromRecord(fields)) { |
| 52 | + | Some(message) => callback(message) |
| 53 | + | _ => () |
| 54 | + } |
| 55 | + | _ => () |
| 56 | + } |
| 57 | + ); |
31 | 58 |
|
32 | 59 | let getIterator = callback => {
|
33 | 60 | ...default_iterator,
|
34 | 61 | expr: (iterator, expr) => {
|
35 |
| - switch (getMessage(expr)) { |
36 |
| - | Some(message) => callback(message) |
37 |
| - | None => () |
| 62 | + switch (expr) { |
| 63 | + /* Match ReactIntl.FormattedMessage.createElement */ |
| 64 | + | { |
| 65 | + pexp_desc: |
| 66 | + Pexp_apply( |
| 67 | + { |
| 68 | + pexp_desc: Pexp_ident({txt: Ldot(Ldot(Lident("ReactIntl"), "FormattedMessage"), "createElement"), _}), |
| 69 | + }, |
| 70 | + labels, |
| 71 | + ), |
| 72 | + } => |
| 73 | + switch (extractMessageFromLabels(labels)) { |
| 74 | + | Some(message) => callback(message) |
| 75 | + | _ => () |
| 76 | + } |
| 77 | + /* Match ReactIntl.defineMessages */ |
| 78 | + | { |
| 79 | + pexp_desc: |
| 80 | + Pexp_apply( |
| 81 | + {pexp_desc: Pexp_ident({txt: Ldot(Lident("ReactIntl"), "defineMessages"), _})}, |
| 82 | + [ |
| 83 | + ( |
| 84 | + Asttypes.Nolabel, |
| 85 | + { |
| 86 | + pexp_desc: |
| 87 | + Pexp_extension(( |
| 88 | + {txt: "bs.obj"}, |
| 89 | + PStr([{pstr_desc: Pstr_eval({pexp_desc: Pexp_record(fields, _)}, _), pstr_loc: _}]), |
| 90 | + )), |
| 91 | + }, |
| 92 | + ), |
| 93 | + ], |
| 94 | + ), |
| 95 | + } => |
| 96 | + extractMessagesFromRecords(callback, fields) |
| 97 | + | _ => () |
38 | 98 | };
|
39 | 99 | default_iterator.expr(iterator, expr);
|
40 | 100 | },
|
|
0 commit comments