Skip to content
This repository was archived by the owner on May 4, 2020. It is now read-only.

Commit 30b9de9

Browse files
author
Long Ho
committed
feat(babel-plugin-react-intl): add filename as param to overrideIdFn
fix #495 close #496
1 parent 843d82b commit 30b9de9

File tree

5 files changed

+21
-14
lines changed

5 files changed

+21
-14
lines changed

packages/babel-plugin-react-intl/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ function createMessageDescriptor(
182182
function evaluateMessageDescriptor(
183183
descriptorPath: MessageDescriptorPath,
184184
isJSXSource = false,
185+
filename: string,
185186
overrideIdFn?: OptionsSchema['overrideIdFn']
186187
) {
187188
let id = getMessageDescriptorValue(descriptorPath.id);
@@ -191,7 +192,7 @@ function evaluateMessageDescriptor(
191192
const description = getMessageDescriptorValue(descriptorPath.description);
192193

193194
if (overrideIdFn) {
194-
id = overrideIdFn(id, defaultMessage, description);
195+
id = overrideIdFn(id, defaultMessage, description, filename);
195196
}
196197
const descriptor: MessageDescriptor = {
197198
id,
@@ -433,6 +434,7 @@ export default declare((api: any, options: OptionsSchema) => {
433434
const descriptor = evaluateMessageDescriptor(
434435
descriptorPath,
435436
true,
437+
filename,
436438
overrideIdFn
437439
);
438440

@@ -537,6 +539,7 @@ export default declare((api: any, options: OptionsSchema) => {
537539
const descriptor = evaluateMessageDescriptor(
538540
descriptorPath,
539541
false,
542+
filename,
540543
overrideIdFn
541544
);
542545
storeMessage(descriptor, messageDescriptor, opts, filename, messages);

packages/babel-plugin-react-intl/src/options.schema.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"messagesDir": {"type": "string"},
77
"overrideIdFn": {
88
"instanceof": "Function",
9-
"tsType": "(id: string, defaultMessage: string, descriptor: string) => string"
9+
"tsType": "(id: string, defaultMessage: string, descriptor: string, filePath: string) => string"
1010
},
1111
"removeDefaultMessage": {"type": "boolean"},
1212
"extractFromFormatMessageCall": {"type": "boolean"},

packages/babel-plugin-react-intl/src/options.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export interface OptionsSchema {
99
moduleSourceName?: string;
1010
extractSourceLocation?: boolean;
1111
messagesDir?: string;
12-
overrideIdFn?: (id: string, defaultMessage: string, descriptor: string) => string;
12+
overrideIdFn?: (id: string, defaultMessage: string, descriptor: string, filePath: string) => string;
1313
removeDefaultMessage?: boolean;
1414
extractFromFormatMessageCall?: boolean;
1515
additionalComponentNames?: string[];

packages/babel-plugin-react-intl/test/__snapshots__/index.test.ts.snap

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -485,25 +485,25 @@ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj;
485485
486486
const msgs = (0, _reactIntl.defineMessages)({
487487
header: {
488-
\\"id\\": \\"HELLO.foo.bar.baz.12.string\\",
488+
\\"id\\": \\"actual.js.foo.bar.baz.12.string\\",
489489
\\"defaultMessage\\": \\"Hello World!\\"
490490
},
491491
content: {
492-
\\"id\\": \\"HELLO.foo.bar.biff.12.object\\",
492+
\\"id\\": \\"actual.js.foo.bar.biff.12.object\\",
493493
\\"defaultMessage\\": \\"Hello Nurse!\\"
494494
}
495495
});
496496
497497
class Foo extends _react.Component {
498498
render() {
499499
return _react.default.createElement(\\"div\\", null, _react.default.createElement(\\"h1\\", null, _react.default.createElement(_reactIntl.FormattedMessage, msgs.header)), _react.default.createElement(\\"p\\", null, _react.default.createElement(_reactIntl.FormattedMessage, msgs.content)), _react.default.createElement(_reactIntl.FormattedMessage, {
500-
id: \\"HELLO.foo.bar.zoo.12.object\\",
500+
id: \\"actual.js.foo.bar.zoo.12.object\\",
501501
defaultMessage: \\"Hello World!\\"
502502
}), _react.default.createElement(_reactIntl.FormattedMessage, {
503503
id: \\"HELLO..5.object\\",
504504
defaultMessage: \\"NO ID\\"
505505
}), _react.default.createElement(_reactIntl.FormattedHTMLMessage, {
506-
id: \\"HELLO.foo.bar.delta.21.string\\",
506+
id: \\"actual.js.foo.bar.delta.21.string\\",
507507
defaultMessage: \\"<h1>Hello World!</h1>\\"
508508
}));
509509
}
@@ -518,23 +518,23 @@ Array [
518518
Object {
519519
"defaultMessage": "Hello World!",
520520
"description": "The default message",
521-
"id": "HELLO.foo.bar.baz.12.string",
521+
"id": "actual.js.foo.bar.baz.12.string",
522522
},
523523
Object {
524524
"defaultMessage": "Hello Nurse!",
525525
"description": Object {
526526
"metadata": "Additional metadata content.",
527527
"text": "Something for the translator.",
528528
},
529-
"id": "HELLO.foo.bar.biff.12.object",
529+
"id": "actual.js.foo.bar.biff.12.object",
530530
},
531531
Object {
532532
"defaultMessage": "Hello World!",
533533
"description": Object {
534534
"metadata": "Additional metadata content.",
535535
"text": "Something for the translator. Another description",
536536
},
537-
"id": "HELLO.foo.bar.zoo.12.object",
537+
"id": "actual.js.foo.bar.zoo.12.object",
538538
},
539539
Object {
540540
"defaultMessage": "NO ID",
@@ -547,7 +547,7 @@ Array [
547547
Object {
548548
"defaultMessage": "<h1>Hello World!</h1>",
549549
"description": "The default message.",
550-
"id": "HELLO.foo.bar.delta.21.string",
550+
"id": "actual.js.foo.bar.delta.21.string",
551551
},
552552
]
553553
`;

packages/babel-plugin-react-intl/test/index.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {join} from 'path';
1+
import {join, basename} from 'path';
22
import * as fs from 'fs';
33
import {transformFileSync} from '@babel/core';
44
import plugin from '../src';
@@ -69,9 +69,13 @@ describe('options', () => {
6969
overrideIdFn: (
7070
id: string,
7171
defaultMessage: string,
72-
description: string
72+
description: string,
73+
filePath: string
7374
) => {
74-
return `HELLO.${id}.${defaultMessage.length}.${typeof description}`;
75+
const filename = basename(filePath);
76+
return `${filename}.${id}.${
77+
defaultMessage.length
78+
}.${typeof description}`;
7579
},
7680
})!.code;
7781

0 commit comments

Comments
 (0)