Skip to content
This repository was archived by the owner on Jun 8, 2019. It is now read-only.

Commit 8311849

Browse files
committed
Add extractSourceLocation option
This option will provide metadata in the Message Description for the file and location in the file where the message was defined. When enabled and running it on [this file][1] you'll get this result: ```json [ { "id": "app.title", "defaultMessage": "React Intl Translations Example", "file": "src/client/components/app.js", "start": { "line": 23, "column": 20 }, "end": { "line": 26, "column": 22 } }, { "id": "app.locales_menu_heading", "defaultMessage": "Locales:", "file": "src/client/components/app.js", "start": { "line": 32, "column": 20 }, "end": { "line": 35, "column": 22 } } ] ``` Fixes #71 [1]: https://github.com/yahoo/react-intl/blob/25f3f64d86ac86281157117b5bdb9fa30d1d615f/examples/translations/src/client/components/app.js
1 parent e491de9 commit 8311849

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/index.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export default function () {
107107
}
108108

109109
function storeMessage({id, description, defaultMessage}, path, state) {
110-
const {opts, reactIntl} = state;
110+
const {file, opts, reactIntl} = state;
111111

112112
if (!(id && defaultMessage)) {
113113
throw path.buildCodeFrameError(
@@ -134,7 +134,15 @@ export default function () {
134134
);
135135
}
136136

137-
reactIntl.messages.set(id, {id, description, defaultMessage});
137+
let loc;
138+
if (opts.extractSourceLocation) {
139+
loc = {
140+
file: p.relative(process.cwd(), file.opts.filename),
141+
...path.node.loc,
142+
};
143+
}
144+
145+
reactIntl.messages.set(id, {id, description, defaultMessage, ...loc});
138146
}
139147

140148
function referencesImport(path, mod, importedNames) {
@@ -257,7 +265,7 @@ export default function () {
257265

258266
// Evaluate the Message Descriptor values, then store it.
259267
descriptor = evaluateMessageDescriptor(descriptor);
260-
storeMessage(descriptor, path, state);
268+
storeMessage(descriptor, messageObj, state);
261269
}
262270

263271
if (referencesImport(callee, moduleSourceName, FUNCTION_NAMES)) {

0 commit comments

Comments
 (0)