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

Commit 634967a

Browse files
author
Long Ho
committed
chore(intl-messageformat): prettier
1 parent a038e8b commit 634967a

File tree

5 files changed

+87
-50
lines changed

5 files changed

+87
-50
lines changed

packages/intl-messageformat-parser/test/tag.test.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,23 @@ test('escaped multiple tags', () => {
2323
});
2424

2525
test('escaped multiple tags with placeholder', () => {
26-
expect(pegParse("I '<'3 cats. '<a>foo</a>' '<b>'{bar}'</b>'")).toMatchSnapshot();
26+
expect(
27+
pegParse("I '<'3 cats. '<a>foo</a>' '<b>'{bar}'</b>'")
28+
).toMatchSnapshot();
2729
});
2830

2931
test('mismatched tag', function() {
3032
expect(() => pegParse('this is <a>mismatch</b>')).toThrowError(/Mismatch/);
3133
});
3234

33-
test('nested tag', function () {
34-
expect(pegParse('this is <a>nested <b>{placeholder}</b></a>')).toMatchSnapshot()
35-
})
35+
test('nested tag', function() {
36+
expect(
37+
pegParse('this is <a>nested <b>{placeholder}</b></a>')
38+
).toMatchSnapshot();
39+
});
3640

37-
test('self-closing tag', function () {
38-
expect(pegParse('this is <br/> <a>nested <b>{placeholder}</b></a>')).toMatchSnapshot()
39-
})
41+
test('self-closing tag', function() {
42+
expect(
43+
pegParse('this is <br/> <a>nested <b>{placeholder}</b></a>')
44+
).toMatchSnapshot();
45+
});

packages/intl-messageformat/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,23 +177,23 @@ We support embedded XML tag in the message, e.g `this is a <b>strong</b> tag`. T
177177
1. Any attributes on the HTML tag are also ignored.
178178
2. Self-closing tags are treated as string literal and not supported, please use regular ICU placeholder like `{placeholder}`.
179179
3. All tags specified must have corresponding values and will throw
180-
error if it's missing, e.g: `new IntlMessageFormat("a<b>strong</b>").format({ b: (...chunks) => <strong>chunks</strong> })`.
180+
error if it's missing, e.g: `new IntlMessageFormat("a<b>strong</b>").format({ b: (...chunks) => <strong>chunks</strong> })`.
181181
4. XML/HTML tags are escaped using apostrophe just like other ICU constructs. In order to escape you can do things like:
182+
182183
```tsx
183-
new IntlMessageFormat("I '<'3 cats").format() // "I <3 cats"
184-
new IntlMessageFormat("raw '<b>HTML</b>'").format() // "raw <b>HTML</b>"
185-
new IntlMessageFormat("raw '<b>HTML</b>' with '<a>'{placeholder}'</a>'").format({placeholder: 'some word'}) // "raw <b>HTML</b> with <a>some word</a>"
184+
new IntlMessageFormat("I '<'3 cats").format(); // "I <3 cats"
185+
new IntlMessageFormat("raw '<b>HTML</b>'").format(); // "raw <b>HTML</b>"
186+
new IntlMessageFormat(
187+
"raw '<b>HTML</b>' with '<a>'{placeholder}'</a>'"
188+
).format({placeholder: 'some word'}); // "raw <b>HTML</b> with <a>some word</a>"
186189
```
187190

188-
189191
### `getAst` Method
190192

191193
Return the underlying AST for the compiled message
192194

193195
#### Caveats
194196

195-
196-
197197
- List of self-closing tags is defined [here](https://html.spec.whatwg.org/multipage/syntax.html#void-elements).
198198

199199
If you don't have `DOMParser` available in your environment (e.g. Node.js), you need to add a polyfill. One of the packages that provides such a polyfill is [jsdom](https://www.npmjs.com/package/jsdom).

packages/intl-messageformat/src/core.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,27 +121,35 @@ export class IntlMessageFormat {
121121
(opts && opts.formatters) || createDefaultFormatters(this.formatterCache);
122122
}
123123

124-
format = <T = void>(values?: Record<string, PrimitiveType | T | FormatXMLElementFn<T>>) => {
125-
const parts = this.formatToParts(values)
124+
format = <T = void>(
125+
values?: Record<string, PrimitiveType | T | FormatXMLElementFn<T>>
126+
) => {
127+
const parts = this.formatToParts(values);
126128
// Hot path for straight simple msg translations
127129
if (parts.length === 1) {
128130
return parts[0].value;
129131
}
130132
const result = parts.reduce((all, part) => {
131-
if (!all.length || part.type !== PART_TYPE.literal || typeof all[all.length - 1] !== 'string') {
132-
all.push(part.value)
133+
if (
134+
!all.length ||
135+
part.type !== PART_TYPE.literal ||
136+
typeof all[all.length - 1] !== 'string'
137+
) {
138+
all.push(part.value);
133139
} else {
134-
all[all.length - 1] += part.value
140+
all[all.length - 1] += part.value;
135141
}
136-
return all
142+
return all;
137143
}, [] as Array<string | T>);
138-
144+
139145
if (result.length <= 1) {
140-
return result[0] || ''
146+
return result[0] || '';
141147
}
142-
return result
143-
}
144-
formatToParts = <T>(values?: Record<string, PrimitiveType | T | FormatXMLElementFn<T>>): MessageFormatPart<T>[] =>
148+
return result;
149+
};
150+
formatToParts = <T>(
151+
values?: Record<string, PrimitiveType | T | FormatXMLElementFn<T>>
152+
): MessageFormatPart<T>[] =>
145153
formatToParts(
146154
this.ast,
147155
this.locales,

packages/intl-messageformat/src/formatters.ts

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ class FormatError extends Error {
6666
}
6767
}
6868

69-
function mergeLiteral<T>(parts: MessageFormatPart<T>[]): MessageFormatPart<T>[] {
69+
function mergeLiteral<T>(
70+
parts: MessageFormatPart<T>[]
71+
): MessageFormatPart<T>[] {
7072
if (parts.length < 2) {
7173
return parts;
7274
}
@@ -85,8 +87,10 @@ function mergeLiteral<T>(parts: MessageFormatPart<T>[]): MessageFormatPart<T>[]
8587
}, [] as MessageFormatPart<T>[]);
8688
}
8789

88-
function isFormatXMLElementFn<T> (el: PrimitiveType | T | FormatXMLElementFn<T>): el is FormatXMLElementFn<T> {
89-
return typeof el === 'function'
90+
function isFormatXMLElementFn<T>(
91+
el: PrimitiveType | T | FormatXMLElementFn<T>
92+
): el is FormatXMLElementFn<T> {
93+
return typeof el === 'function';
9094
}
9195

9296
// TODO(skeleton): add skeleton support
@@ -149,7 +153,7 @@ export function formatToParts<T>(
149153
: '';
150154
}
151155
result.push({
152-
type: typeof value === 'string' ? PART_TYPE.literal : PART_TYPE.object,
156+
type: typeof value === 'string' ? PART_TYPE.literal : PART_TYPE.object,
153157
value,
154158
} as ObjectPart<T>);
155159
continue;
@@ -200,22 +204,33 @@ export function formatToParts<T>(
200204
continue;
201205
}
202206
if (isTagElement(el)) {
203-
const {children, value} = el
204-
const formatFn = values[value]
207+
const {children, value} = el;
208+
const formatFn = values[value];
205209
if (!isFormatXMLElementFn<T>(formatFn)) {
206-
throw new TypeError(`Value for "${value}" must be a function`)
210+
throw new TypeError(`Value for "${value}" must be a function`);
207211
}
208-
const parts = formatToParts<T>(children, locales, formatters, formats, values)
209-
let chunks = formatFn(...parts.map(p => p.value))
212+
const parts = formatToParts<T>(
213+
children,
214+
locales,
215+
formatters,
216+
formats,
217+
values
218+
);
219+
let chunks = formatFn(...parts.map(p => p.value));
210220
if (!Array.isArray(chunks)) {
211-
chunks = [chunks]
221+
chunks = [chunks];
212222
}
213-
result.push(...chunks.map((c): MessageFormatPart<T> => {
214-
return {
215-
type: typeof c === 'string' ? PART_TYPE.literal : PART_TYPE.object,
216-
value: c
217-
} as MessageFormatPart<T>
218-
}))
223+
result.push(
224+
...chunks.map(
225+
(c): MessageFormatPart<T> => {
226+
return {
227+
type:
228+
typeof c === 'string' ? PART_TYPE.literal : PART_TYPE.object,
229+
value: c,
230+
} as MessageFormatPart<T>;
231+
}
232+
)
233+
);
219234
}
220235
if (isSelectElement(el)) {
221236
const opt = el.options[value as string] || el.options.other;
@@ -267,4 +282,6 @@ Try polyfilling it using "@formatjs/intl-pluralrules"
267282
return mergeLiteral(result);
268283
}
269284

270-
export type FormatXMLElementFn<T> = (...args: Array<string | T>) => string | Array<string | T>;
285+
export type FormatXMLElementFn<T> = (
286+
...args: Array<string | T>
287+
) => string | Array<string | T>;

packages/intl-messageformat/tests/index.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -601,11 +601,7 @@ describe('IntlMessageFormat', function() {
601601
b: (...chunks) => ['<b>', ...chunks, '</b>'],
602602
i: c => ({val: `$$${c}$$`}),
603603
})
604-
).to.deep.equal([
605-
'hello <b>world',
606-
{val: '$$!$$'},
607-
' <br/> </b>',
608-
]);
604+
).to.deep.equal(['hello <b>world', {val: '$$!$$'}, ' <br/> </b>']);
609605
});
610606
it('simple message w/ placeholder and no tag', function() {
611607
const mf = new IntlMessageFormat('hello {placeholder} {var2}', 'en');
@@ -650,7 +646,12 @@ describe('IntlMessageFormat', function() {
650646
placeholder: '>',
651647
a: str => ({str}),
652648
})
653-
).to.deep.equal(['&lt; hello ', {str: 'world'}, ' <asd> &lt;&gt; ', {str: '>'}]);
649+
).to.deep.equal([
650+
'&lt; hello ',
651+
{str: 'world'},
652+
' <asd> &lt;&gt; ',
653+
{str: '>'},
654+
]);
654655
});
655656
it('select message w/ placeholder & >', function() {
656657
const mf = new IntlMessageFormat(
@@ -665,7 +666,12 @@ describe('IntlMessageFormat', function() {
665666
placeholder: '>',
666667
a: str => ({str}),
667668
})
668-
).to.deep.equal(['&lt; hello ', {str: 'world'}, ' <asd> &lt;&gt; ', {str: '>'}]);
669+
).to.deep.equal([
670+
'&lt; hello ',
671+
{str: 'world'},
672+
' <asd> &lt;&gt; ',
673+
{str: '>'},
674+
]);
669675
expect(
670676
mf.format({
671677
gender: 'female',
@@ -675,7 +681,7 @@ describe('IntlMessageFormat', function() {
675681
});
676682
it('should allow escaping tag as legacy HTML', function() {
677683
const mf = new IntlMessageFormat(
678-
'hello \'<b>world</b>\' \'<a>\'{placeholder}\'</a>\'',
684+
"hello '<b>world</b>' '<a>'{placeholder}'</a>'",
679685
'en'
680686
);
681687
expect(

0 commit comments

Comments
 (0)