From aefb68bb389246bdd1281b5600d524ad93cd4443 Mon Sep 17 00:00:00 2001 From: Long Ho Date: Fri, 27 Sep 2019 11:11:42 -0400 Subject: [PATCH] fix: remove custom unescaping of static message Since ICU message format uses apostrophe escaping, our manual unescape becomes very troublesome. There seems to be no significant perf degradation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before: ```
x 2,161,669 ops/sec ±7.87% (71 runs sampled) 100 x
x 16,464 ops/sec ±0.71% (92 runs sampled) 100 x x 2,421 ops/sec ±1.26% (91 runs sampled) 100 x x 903 ops/sec ±1.40% (91 runs sampled) 100 x x 340 ops/sec ±11.89% (82 runs sampled) 100 x with placeholder x 91.85 ops/sec ±5.76% (67 runs sampled) 100 x with placeholder in AST form x 223 ops/sec ±3.57% (79 runs sampled) 100 x with placeholder, cached x 505 ops/sec ±5.36% (72 runs sampled) 100 x with placeholder, cached in AST form x 419 ops/sec ±6.04% (80 runs sampled) ``` After: ```
x 2,998,677 ops/sec ±0.57% (90 runs sampled) 100 x
x 15,512 ops/sec ±3.10% (87 runs sampled) 100 x x 2,456 ops/sec ±1.51% (90 runs sampled) 100 x x 942 ops/sec ±0.99% (90 runs sampled) 100 x x 358 ops/sec ±3.59% (82 runs sampled) 100 x with placeholder x 103 ops/sec ±3.83% (75 runs sampled) 100 x with placeholder in AST form x 237 ops/sec ±1.51% (82 runs sampled) 100 x with placeholder, cached x 673 ops/sec ±2.15% (87 runs sampled) 100 x with placeholder, cached in AST form x 485 ops/sec ±1.77% (86 runs sampled) ``` --- src/formatters/message.ts | 24 ------------------------ test/unit/format.ts | 21 --------------------- 2 files changed, 45 deletions(-) diff --git a/src/formatters/message.ts b/src/formatters/message.ts index 4b8f32fa82..b8bf1d48c8 100644 --- a/src/formatters/message.ts +++ b/src/formatters/message.ts @@ -15,17 +15,8 @@ const invariant: typeof invariant_ = (invariant_ as any).default || invariant_; import {Formatters, IntlConfig, MessageDescriptor} from '../types'; import {createError, escape} from '../utils'; -import {LiteralElement, TYPE} from 'intl-messageformat-parser'; import {FormatXMLElementFn, PrimitiveType} from 'intl-messageformat'; -/** - * Escape a raw msg when we run in prod mode - * https://github.com/formatjs/formatjs/blob/master/packages/intl-messageformat-parser/src/parser.pegjs#L155 - */ -function escapeUnformattedMessage(msg: string): string { - return msg.replace(/'\{(.*?)\}'/g, `{$1}`); -} - export function formatMessage( { locale, @@ -77,21 +68,6 @@ export function formatMessage( invariant(id, '[React Intl] An `id` must be provided to format a message.'); const message = messages && messages[id]; - const hasValues = Object.keys(values).length > 0; - - // Avoid expensive message formatting for simple messages without values. In - // development messages will always be formatted in case of missing values. - if (!hasValues && process.env.NODE_ENV === 'production') { - const val = message || defaultMessage || id; - if (typeof val === 'string') { - return escapeUnformattedMessage(val); - } - invariant( - val.length === 1 && val[0].type === TYPE.literal, - 'Message has placeholders but no values was provided' - ); - return (val[0] as LiteralElement).value; - } let formattedMessageParts: Array = []; diff --git a/test/unit/format.ts b/test/unit/format.ts index 9031970f24..e919f82d9b 100644 --- a/test/unit/format.ts +++ b/test/unit/format.ts @@ -722,11 +722,6 @@ describe('format API', () => { expect(formatMessage({id: 'ast_simple'})).toBe(mf.format()); }); - it('should throw if format AST message w/o values', () => { - process.env.NODE_ENV = 'production'; - expect(() => formatMessage({id: 'ast_var'})).toThrow(); - }); - it('formats messages with placeholders', () => { const {locale, messages} = config; const mf = new IntlMessageFormat(messages.with_arg, locale); @@ -757,22 +752,6 @@ describe('format API', () => { ); }); - it('avoids formatting when no values and in production', () => { - const {messages} = config; - - process.env.NODE_ENV = 'production'; - expect(formatMessage({id: 'no_args'})).toBe(messages.no_args); - expect(state.getMessageFormat).toHaveBeenCalledTimes(0); - - const values = {foo: 'foo'}; - expect(formatMessage({id: 'no_args'}, values)).toBe(messages.no_args); - expect(state.getMessageFormat).toHaveBeenCalledTimes(1); - - process.env.NODE_ENV = 'development'; - expect(formatMessage({id: 'no_args'})).toBe(messages.no_args); - expect(state.getMessageFormat).toHaveBeenCalledTimes(2); - }); - describe('fallbacks', () => { it('formats message with missing named formats', () => { const {locale, messages} = config;