diff --git a/packages/icu-messageformat-parser/index.ts b/packages/icu-messageformat-parser/index.ts index 32acb9fdae..b896ee8750 100644 --- a/packages/icu-messageformat-parser/index.ts +++ b/packages/icu-messageformat-parser/index.ts @@ -54,6 +54,7 @@ export function parse(message: string, opts: ParserOptions = {}) { } return result.val } +export {ParserOptions} export * from './types' // only for testing export const _Parser = Parser diff --git a/packages/intl-messageformat/src/core.ts b/packages/intl-messageformat/src/core.ts index 25223e1f6e..514e629f7a 100644 --- a/packages/intl-messageformat/src/core.ts +++ b/packages/intl-messageformat/src/core.ts @@ -4,7 +4,11 @@ Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. */ -import {parse, MessageFormatElement} from '@formatjs/icu-messageformat-parser' +import { + parse, + MessageFormatElement, + ParserOptions, +} from '@formatjs/icu-messageformat-parser' import memoize, {Cache, strategies} from '@formatjs/fast-memoize' import { FormatterCache, @@ -53,15 +57,8 @@ function mergeConfigs( ) } -export interface Options { +export interface Options extends Omit { formatters?: Formatters - /** - * Whether to treat HTML/XML tags as string literal - * instead of parsing them as tag token. - * When this is false we only allow simple tags without - * any attributes - */ - ignoreTag?: boolean } function createFastMemoizeCache( @@ -133,9 +130,10 @@ export class IntlMessageFormat { 'IntlMessageFormat.__parse must be set to process `message` of type `string`' ) } + const {formatters, ...parseOpts} = opts || {} // Parse string messages into an AST. this.ast = IntlMessageFormat.__parse(message, { - ignoreTag: opts?.ignoreTag, + ...parseOpts, locale: this.resolvedLocale, }) } else { diff --git a/packages/intl-messageformat/tests/index.test.ts b/packages/intl-messageformat/tests/index.test.ts index cce916dc0a..64780b39b6 100644 --- a/packages/intl-messageformat/tests/index.test.ts +++ b/packages/intl-messageformat/tests/index.test.ts @@ -469,6 +469,24 @@ describe('IntlMessageFormat', function () { }) }) + describe('select message without other clause', function () { + const msg = '{variable, select, a {A} b {B} c {C}}' + + it('should throw by default', function () { + expect(() => { + new IntlMessageFormat(msg, 'en') + }).toThrow(/MISSING_OTHER_CLAUSE/) + }) + + it('should not throw when requiresOtherClause is false', function () { + expect(() => { + new IntlMessageFormat(msg, 'en', undefined, { + requiresOtherClause: false, + }) + }).not.toThrow() + }) + }) + describe('selectordinal arguments', function () { const msg = 'This is my {year, selectordinal, one{#st} two{#nd} few{#rd} other{#th}} birthday.'