Skip to content

Commit 8bd9ad8

Browse files
longlhoLong Ho
authored and
Long Ho
committed
feat: add defineMessage macro to tag single message
1 parent 50b7941 commit 8bd9ad8

File tree

7 files changed

+56
-53
lines changed

7 files changed

+56
-53
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ _(If you're looking for React Intl v1, you can find it [here][v1-docs].)_
3636

3737
There are several [runnable examples][examples] in this Git repo, but here's a Hello World one:
3838

39-
```js
39+
```tsx
4040
import React, {Component} from 'react';
4141
import ReactDOM from 'react-dom';
4242
import {IntlProvider, FormattedMessage} from 'react-intl';

docs/Getting-Started.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ If you need to support older browsers, we recommend you do the following:
6262
2. Polyfill `Intl.DateTimeFormat` with https://github.com/formatjs/date-time-format-timezone
6363
3. If you're supporting browsers that do not have [Intl.PluralRules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/PluralRules) (e.g IE11 & Safari 12-), include this [polyfill](https://www.npmjs.com/package/@formatjs/intl-pluralrules) in your build.
6464

65-
```js
65+
```tsx
6666
if (!Intl.PluralRules) {
6767
require('@formatjs/intl-pluralrules/polyfill');
6868
require('@formatjs/intl-pluralrules/dist/locale-data/de'); // Add locale data for de
@@ -71,7 +71,7 @@ if (!Intl.PluralRules) {
7171

7272
4. If you're supporting browsers that do not have [Intl.RelativeTimeFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RelativeTimeFormat) (e.g IE11, Edge, Safari 12-), include this [polyfill](https://www.npmjs.com/package/@formatjs/intl-relativetimeformat) in your build along with individual CLDR data for each locale you support.
7373

74-
```js
74+
```tsx
7575
if (!Intl.RelativeTimeFormat) {
7676
require('@formatjs/intl-relativetimeformat/polyfill');
7777
require('@formatjs/intl-relativetimeformat/dist/locale-data/de'); // Add locale data for de
@@ -81,7 +81,7 @@ if (!Intl.RelativeTimeFormat) {
8181
5. If you need `Intl.DisplayNames`, include this [polyfill][displaynames-polyfill] in your build along
8282
with individual CLDR data for each locale you support.
8383

84-
```js
84+
```tsx
8585
if (!Intl.DisplayNames) {
8686
require('@formatjs/intl-displaynames/polyfill');
8787
require('@formatjs/intl-displaynames/dist/locale-data/de'); // Add locale data for de

docs/Testing-with-React-Intl.md

+32-32
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ The following examples will assume `mocha`, `expect`, and `expect-jsx` test fram
6767

6868
#### <ShortDate> (Basic)
6969

70-
```js
70+
```tsx
7171
import React from 'react';
7272
import {FormattedDate} from 'react-intl';
7373

@@ -85,7 +85,7 @@ export default ShortDate;
8585

8686
Testing the `<ShortDate>` example component is no different than testing any other basic component in your app using React's shallow rendering:
8787

88-
```js
88+
```tsx
8989
import expect from 'expect';
9090
import expectJSX from 'expect-jsx';
9191
import React from 'react';
@@ -110,7 +110,7 @@ describe('<ShortDate>', function() {
110110

111111
#### <RelativeDate> (Advanced, Uses `injectIntl()`)
112112

113-
```js
113+
```tsx
114114
import React, {Component} from 'react';
115115
import {injectIntl, FormattedRelative} from 'react-intl';
116116

@@ -131,7 +131,7 @@ Shallow rendering only tests one level deep and we want to test the rendering of
131131

132132
Under the hood, `injectIntl()` passes `context.intl` which was created from the `<IntlProvider>` in the component's ancestry to `props.intl`. What we need to do is simulate this for our shallow rendering test:
133133

134-
```js
134+
```tsx
135135
import expect from 'expect';
136136
import expectJSX from 'expect-jsx';
137137
import React from 'react';
@@ -173,7 +173,7 @@ describe('<RelativeDate>', function() {
173173

174174
If you use the DOM in your tests, you need to supply the `IntlProvider` context to your components using composition:
175175

176-
```js
176+
```tsx
177177
let element = ReactTestUtils.renderIntoDocument(
178178
<IntlProvider>
179179
<MyComponent />
@@ -185,7 +185,7 @@ However this means that the `element` reference is now pointing to the `IntlProv
185185

186186
In your component, remember to add `{withRef: true}` when calling `injectIntl()`:
187187

188-
```js
188+
```tsx
189189
class MyComponent extends React.Component {
190190
...
191191
myClassFn() { ... }
@@ -195,7 +195,7 @@ export default injectIntl(MyComponent, {withRef: true});
195195

196196
In your test, add a "ref" to extract the reference to your tested component:
197197

198-
```js
198+
```tsx
199199
let element;
200200
let root = ReactTestUtils.renderIntoDocument(
201201
<IntlProvider>
@@ -206,15 +206,15 @@ let root = ReactTestUtils.renderIntoDocument(
206206

207207
You can now access the wrapped component instance from `element` like this:
208208

209-
```js
209+
```tsx
210210
element.myClassFn();
211211
```
212212

213213
### Helper function
214214

215215
Since you will have to do this in all your unit tests, you should probably wrap that setup in a `render` function like this:
216216

217-
```js
217+
```tsx
218218
function renderWithIntl(element) {
219219
let instance;
220220

@@ -232,7 +232,7 @@ function renderWithIntl(element) {
232232

233233
You can now use this in your tests like this:
234234

235-
```js
235+
```tsx
236236
let element = renderWithIntl(<MyElement>);
237237
element.myClassFn();
238238
```
@@ -287,7 +287,7 @@ export function shallowWithIntl(node: React.ReactElement) {
287287

288288
Create a file with the above helper in e.g. `helpers/intl-enzyme-test-helper.js` and `import` the methods you need in your tests.
289289

290-
```js
290+
```tsx
291291
// intl-enzyme-test-helper.js
292292

293293
import {mountWithIntl} from 'helpers/intl-enzyme-test-helper.js';
@@ -310,7 +310,7 @@ Snapshot testing is a new feature of Jest that automatically generates text snap
310310

311311
#### Helper function
312312

313-
```js
313+
```tsx
314314
import React from 'react';
315315
import renderer from 'react-test-renderer';
316316
import {IntlProvider} from 'react-intl';
@@ -324,7 +324,7 @@ export default createComponentWithIntl;
324324

325325
#### Usage
326326

327-
```js
327+
```tsx
328328
import React from 'react';
329329
import createComponentWithIntl from '../../utils/createComponentWithIntl';
330330
import AppMain from '../AppMain';
@@ -350,7 +350,7 @@ You can find runnable example [here](https://github.com/formatjs/react-intl/tree
350350

351351
Jest will automatically mock react-intl, so no any extra implementation is needed, tests should work as is:
352352

353-
```js
353+
```tsx
354354
import React from 'react';
355355
import {shallow} from 'enzyme';
356356
import AppMain from '../AppMain';
@@ -384,30 +384,30 @@ Our custom `render` function can look like this:
384384

385385
```jsx
386386
// test-utils.js
387-
import React from 'react'
388-
import { render as rtlRender } from '@testing-library/react'
389-
import { IntlProvider } from 'react-intl'
387+
import React from 'react';
388+
import {render as rtlRender} from '@testing-library/react';
389+
import {IntlProvider} from 'react-intl';
390390

391-
function render(ui, { locale = 'pt', ...renderOptions } = {}) {
392-
function Wrapper({ children }) {
393-
return <IntlProvider locale={locale}>{children}</IntlProvider>
391+
function render(ui, {locale = 'pt', ...renderOptions} = {}) {
392+
function Wrapper({children}) {
393+
return <IntlProvider locale={locale}>{children}</IntlProvider>;
394394
}
395-
return rtlRender(ui, { wrapper: Wrapper, ...renderOptions })
395+
return rtlRender(ui, {wrapper: Wrapper, ...renderOptions});
396396
}
397397

398398
// re-export everything
399-
export * from '@testing-library/react'
399+
export * from '@testing-library/react';
400400

401401
// override render method
402-
export { render }
402+
export {render};
403403
```
404404

405405
```jsx
406-
import React from 'react'
407-
import '@testing-library/jest-dom/extend-expect'
406+
import React from 'react';
407+
import '@testing-library/jest-dom/extend-expect';
408408
// We're importing from our own created test-utils and not RTL's
409-
import { render, screen } from '../test-utils.js'
410-
import { FormattedDate } from 'react-intl'
409+
import {render, screen} from '../test-utils.js';
410+
import {FormattedDate} from 'react-intl';
411411

412412
const FormatDateView = () => {
413413
return (
@@ -420,11 +420,11 @@ const FormatDateView = () => {
420420
year="numeric"
421421
/>
422422
</div>
423-
)
424-
}
423+
);
424+
};
425425

426426
test('it should render FormattedDate and have a formated pt date', () => {
427-
render(<FormatDateView />)
428-
expect(screen.getByTestId('date-display')).toHaveTextContent('11/03/2019')
429-
})
427+
render(<FormatDateView />);
428+
expect(screen.getByTestId('date-display')).toHaveTextContent('11/03/2019');
429+
});
430430
```

docs/Upgrade-Guide-2.x.md

+15-15
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ This assumes a locale data `<script>` is added based on the request; e.g., for F
3939

4040
**Using `<script src="react-intl/dist/react-intl.js>`:**
4141

42-
```js
42+
```tsx
4343
if ('ReactIntl' in window && 'ReactIntlLocaleData' in window) {
4444
Object.keys(ReactIntlLocaleData).forEach(lang => {
4545
ReactIntl.addLocaleData(ReactIntlLocaleData[lang]);
@@ -49,7 +49,7 @@ if ('ReactIntl' in window && 'ReactIntlLocaleData' in window) {
4949

5050
**Using Browserify/Webpack to Load React Intl:**
5151

52-
```js
52+
```tsx
5353
import {addLocaleData} from 'react-intl';
5454

5555
if ('ReactIntlLocaleData' in window) {
@@ -69,7 +69,7 @@ if ('ReactIntlLocaleData' in window) {
6969

7070
In React Intl v1, you would add the `IntlMixin` to your root component; e.g., `<App>`. Remove the `IntlMixin` and instead wrap your root component with [`<IntlProvider>`](Components#intlprovider):
7171

72-
```js
72+
```tsx
7373
import ReactDOM from 'react-dom';
7474
import {IntlProvider} from 'react-intl';
7575

@@ -89,7 +89,7 @@ The `IntlMixin` also provided the imperative API for custom components to use th
8989

9090
Here's an example of a custom `<RelativeTime>` stateless component which uses `injectIntl()` and the imperative [`formatDate()`](API.md#formatdate) API:
9191

92-
```js
92+
```tsx
9393
import React from 'react';
9494
import {injectIntl, FormattedRelative} from 'react-intl';
9595

@@ -141,7 +141,7 @@ React Intl v2 no longer supports nested `messages` objects, instead the collecti
141141

142142
Apps using a nested `messages` object structure could use the following function to flatten their object according to React Intl v1's semantics:
143143

144-
```js
144+
```tsx
145145
function flattenMessages(nestedMessages, prefix = '') {
146146
return Object.keys(nestedMessages).reduce((messages, key) => {
147147
let value = nestedMessages[key];
@@ -170,13 +170,13 @@ All calls to `getIntlMessage()` need to be replaced with a Message Descriptor.
170170

171171
**Replace:**
172172

173-
```js
173+
```tsx
174174
this.getIntlMessage('some.message.id');
175175
```
176176

177177
**With:**
178178

179-
```js
179+
```tsx
180180
{
181181
id: 'some.message.id';
182182
}
@@ -188,7 +188,7 @@ A typical pattern when calling `formatMessage()` is to nest a call to `getIntlMe
188188

189189
**1.0:**
190190

191-
```js
191+
```tsx
192192
let message = this.formatMessage(
193193
this.getIntlMessage('some.message.id'),
194194
values
@@ -197,7 +197,7 @@ let message = this.formatMessage(
197197

198198
**2.0:**
199199

200-
```js
200+
```tsx
201201
let message = this.props.intl.formatMessage({id: 'some.message.id'}, values);
202202
```
203203

@@ -213,13 +213,13 @@ The following example shows up to update a `<FormattedMessage>` instance to use
213213

214214
**1.0:**
215215

216-
```js
216+
```tsx
217217
<FormattedMessage message={this.getIntlMessage('greeting')} name="Eric" />
218218
```
219219

220220
**2.0:**
221221

222-
```js
222+
```tsx
223223
<FormattedMessage id="greeting" values={{name: 'Eric'}} />
224224
```
225225

@@ -233,13 +233,13 @@ A new feature has been added to [`<FormattedRelative>`](Components#formattedrela
233233

234234
**1.0:**
235235

236-
```js
236+
```tsx
237237
<FormattedRelative value={date} now={otherDate} />
238238
```
239239

240240
**2.0:**
241241

242-
```js
242+
```tsx
243243
<FormattedRelative value={date} initialNow={otherDate} />
244244
```
245245

@@ -251,13 +251,13 @@ The signature of the `formatRelative()` function has been aligned with the other
251251

252252
**1.0:**
253253

254-
```js
254+
```tsx
255255
let relative = this.formatRelative(date, {units: 'hour'}, {now: otherDate});
256256
```
257257

258258
**2.0:**
259259

260-
```js
260+
```tsx
261261
let relative = this.props.intl.formatRelative(date, {
262262
units: 'hour',
263263
now: otherDate,

docs/Upgrade-Guide.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ If you previously were using `addLocaleData` to support older browsers, we recom
152152
1. If you're supporting browsers that do not have [Intl.PluralRules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/PluralRules) (e.g IE11 & Safari 12-), include this [polyfill](https://www.npmjs.com/package/@formatjs/intl-pluralrules) in your build.
153153
2. If you're supporting browsers that do not have [Intl.RelativeTimeFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RelativeTimeFormat) (e.g IE11, Edge, Safari 13-), include this [polyfill](https://www.npmjs.com/package/@formatjs/intl-relativetimeformat) in your build along with individual CLDR data for each locale you support.
154154

155-
```js
155+
```tsx
156156
if (!Intl.PluralRules) {
157157
require('@formatjs/intl-pluralrules/polyfill');
158158
require('@formatjs/intl-pluralrules/dist/locale-data/de'); // Add locale data for de

examples/Hooks.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const messages = {
3737
* BEFORE using the `useIntl()` hook,
3838
* as shown in the `App` component below
3939
*
40-
* ```js
40+
* ```tsx
4141
* interface Props {}
4242
*
4343
* const Wrapper: React.FC<Props> = (props) => {

src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ export * from './types';
88
export function defineMessages<T, U extends Record<string, T>>(msgs: U): U {
99
return msgs;
1010
}
11+
export function defineMessage<T>(msg: T): T {
12+
return msg;
13+
}
1114
import {
1215
createFormattedComponent,
1316
createFormattedDateTimePartsComponent,

0 commit comments

Comments
 (0)