Skip to content

Commit

Permalink
fix(@formatjs/cli): remove @formatjs/ecma402-abstract, use native TS …
Browse files Browse the repository at this point in the history
…Intl typdefs
  • Loading branch information
longlho committed Nov 18, 2024
1 parent 78865eb commit 3fed5f4
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 35 deletions.
1 change: 0 additions & 1 deletion packages/intl/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ npm_package(
SRCS = glob(["src/**/*.ts*"]) + ["index.ts"]

SRC_DEPS = [
":node_modules/@formatjs/ecma402-abstract",
":node_modules/@formatjs/fast-memoize",
":node_modules/@formatjs/icu-messageformat-parser",
":node_modules/intl-messageformat",
Expand Down
1 change: 0 additions & 1 deletion packages/intl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
"module": "lib/index.js",
"sideEffects": false,
"dependencies": {
"@formatjs/ecma402-abstract": "workspace:*",
"@formatjs/fast-memoize": "workspace:*",
"@formatjs/icu-messageformat-parser": "workspace:*",
"intl-messageformat": "workspace:*",
Expand Down
2 changes: 0 additions & 2 deletions packages/intl/src/create-intl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,7 @@ export function createIntl<T = string>(
resolvedConfig,
formatters.getPluralRules
),
// @ts-expect-error TODO: will get to this later
formatMessage: formatMessage.bind(null, resolvedConfig, formatters),
// @ts-expect-error TODO: will get to this later
$t: formatMessage.bind(null, resolvedConfig, formatters),
formatList: formatList.bind(null, resolvedConfig, formatters.getListFormat),
formatListToParts: formatListToParts.bind(
Expand Down
7 changes: 3 additions & 4 deletions packages/intl/src/dateTime.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {Formatters, IntlFormatters, CustomFormats, OnErrorFn} from './types'
import {CustomFormats, Formatters, IntlFormatters, OnErrorFn} from './types'

import {filterProps, getNamedFormat} from './utils'
import {IntlFormatError} from './error'
import {DateTimeFormat} from '@formatjs/ecma402-abstract'
import {filterProps, getNamedFormat} from './utils'

const DATE_TIME_FORMAT_OPTIONS: Array<keyof Intl.DateTimeFormatOptions> = [
'formatMatcher',
Expand Down Expand Up @@ -43,7 +42,7 @@ export function getFormatter(
type: 'date' | 'time',
getDateTimeFormat: Formatters['getDateTimeFormat'],
options: Parameters<IntlFormatters['formatDate']>[1] = {}
): DateTimeFormat {
): Intl.DateTimeFormat {
const {format} = options
const defaults = {
...(timeZone && {timeZone}),
Expand Down
3 changes: 1 addition & 2 deletions packages/intl/src/message.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import {invariant} from '@formatjs/ecma402-abstract'

import {CustomFormats, Formatters, MessageDescriptor, OnErrorFn} from './types'

import {MessageFormatElement, TYPE} from '@formatjs/icu-messageformat-parser'
Expand All @@ -11,6 +9,7 @@ import {
PrimitiveType,
} from 'intl-messageformat'
import {MessageFormatError, MissingTranslationError} from './error'
import {invariant} from './utils'

function setTimeZoneInOptions(
opts: Record<string, Intl.DateTimeFormatOptions>,
Expand Down
11 changes: 7 additions & 4 deletions packages/intl/src/number.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import {NumberFormatOptions} from '@formatjs/ecma402-abstract'
import {IntlFormatError} from './error'
import {CustomFormats, Formatters, IntlFormatters, OnErrorFn} from './types'
import {filterProps, getNamedFormat} from './utils'

const NUMBER_FORMAT_OPTIONS: Array<keyof NumberFormatOptions> = [
const NUMBER_FORMAT_OPTIONS: Array<keyof Intl.NumberFormatOptions> = [
'style',
'currency',
'unit',
Expand All @@ -27,9 +26,13 @@ const NUMBER_FORMAT_OPTIONS: Array<keyof NumberFormatOptions> = [
'numberingSystem',

// ES2023 NumberFormat
// @ts-expect-error: TypeScript doesn't know about this yet
'trailingZeroDisplay',
// @ts-expect-error: TypeScript doesn't know about this yet
'roundingPriority',
// @ts-expect-error: TypeScript doesn't know about this yet
'roundingIncrement',
// @ts-expect-error: TypeScript doesn't know about this yet
'roundingMode',
]

Expand All @@ -50,12 +53,12 @@ export function getFormatter(
const {format} = options
const defaults = ((format &&
getNamedFormat(formats!, 'number', format, onError)) ||
{}) as NumberFormatOptions
{}) as Intl.NumberFormatOptions
const filteredOptions = filterProps(
options,
NUMBER_FORMAT_OPTIONS,
defaults
) as NumberFormatOptions
) as Intl.NumberFormatOptions

return getNumberFormat(locale, filteredOptions)
}
Expand Down
9 changes: 4 additions & 5 deletions packages/intl/src/plural.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {ErrorCode, FormatError} from 'intl-messageformat'
import {IntlFormatError} from './error'
import {Formatters, IntlFormatters, OnErrorFn} from './types'
import {filterProps} from './utils'
import {IntlFormatError} from './error'
import {ErrorCode, FormatError} from 'intl-messageformat'
import {LDMLPluralRule} from '@formatjs/ecma402-abstract'

const PLURAL_FORMAT_OPTIONS: Array<keyof Intl.PluralRulesOptions> = ['type']

Expand All @@ -17,7 +16,7 @@ export function formatPlural(
getPluralRules: Formatters['getPluralRules'],
value: Parameters<IntlFormatters['formatPlural']>[0],
options: Parameters<IntlFormatters['formatPlural']>[1] = {}
): LDMLPluralRule {
): Intl.LDMLPluralRule {
if (!Intl.PluralRules) {
onError(
new FormatError(
Expand All @@ -36,7 +35,7 @@ Try polyfilling it using "@formatjs/intl-pluralrules"
try {
return getPluralRules(locale, filteredOptions).select(
value
) as LDMLPluralRule
) as Intl.LDMLPluralRule
} catch (e) {
onError(new IntlFormatError('Error formatting plural.', locale, e))
}
Expand Down
16 changes: 9 additions & 7 deletions packages/intl/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {DateTimeFormat, NumberFormatOptions} from '@formatjs/ecma402-abstract'
import {MessageFormatElement} from '@formatjs/icu-messageformat-parser'

import {
Expand Down Expand Up @@ -93,7 +92,10 @@ export type FormatDateOptions = Omit<
'localeMatcher'
> &
CustomFormatConfig<'date'>
export type FormatNumberOptions = Omit<NumberFormatOptions, 'localeMatcher'> &
export type FormatNumberOptions = Omit<
Intl.NumberFormatOptions,
'localeMatcher'
> &
CustomFormatConfig<'number'>
export type FormatRelativeTimeOptions = Omit<
Intl.RelativeTimeFormatOptions,
Expand All @@ -120,8 +122,8 @@ export type FormatDisplayNameOptions = Omit<
export interface IntlFormatters<TBase = unknown> {
formatDateTimeRange(
this: void,
from: Parameters<DateTimeFormat['formatRange']>[0],
to: Parameters<DateTimeFormat['formatRange']>[1],
from: Parameters<Intl.DateTimeFormat['formatRange']>[0],
to: Parameters<Intl.DateTimeFormat['formatRange']>[1],
opts?: FormatDateOptions
): string
formatDate(
Expand Down Expand Up @@ -215,11 +217,11 @@ export interface Formatters {
getDateTimeFormat(
this: void,
...args: ConstructorParameters<typeof Intl.DateTimeFormat>
): DateTimeFormat
): Intl.DateTimeFormat
getNumberFormat(
this: void,
locales?: string | string[],
opts?: NumberFormatOptions
opts?: Intl.NumberFormatOptions
): Intl.NumberFormat
getMessageFormat(
this: void,
Expand Down Expand Up @@ -250,7 +252,7 @@ export interface IntlShape<T = string>
}

export interface IntlCache {
dateTime: Record<string, DateTimeFormat>
dateTime: Record<string, Intl.DateTimeFormat>
number: Record<string, Intl.NumberFormat>
message: Record<string, IntlMessageFormat>
relativeTime: Record<string, Intl.RelativeTimeFormat>
Expand Down
23 changes: 16 additions & 7 deletions packages/intl/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import {Cache, memoize, strategies} from '@formatjs/fast-memoize'
import {IntlMessageFormat} from 'intl-messageformat'
import {UnsupportedFormatterError} from './error'
import {
IntlCache,
CustomFormats,
Formatters,
IntlCache,
OnErrorFn,
OnWarnFn,
ResolvedIntlConfig,
} from './types'
import {IntlMessageFormat} from 'intl-messageformat'
import {memoize, Cache, strategies} from '@formatjs/fast-memoize'
import {UnsupportedFormatterError} from './error'
import {DateTimeFormat, NumberFormatOptions} from '@formatjs/ecma402-abstract'

export function invariant(
condition: boolean,
message: string,
Err: any = Error
): asserts condition {
if (!condition) {
throw new Err(message)
}
}

export function filterProps<T extends Record<string, any>, K extends string>(
props: T,
Expand Down Expand Up @@ -108,7 +117,7 @@ export function createFormatters(
const ListFormat = (Intl as any).ListFormat
const DisplayNames = (Intl as any).DisplayNames
const getDateTimeFormat = memoize(
(...args) => new Intl.DateTimeFormat(...args) as DateTimeFormat,
(...args) => new Intl.DateTimeFormat(...args),
{
cache: createFastMemoizeCache(cache.dateTime),
strategy: strategies.variadic,
Expand Down Expand Up @@ -165,7 +174,7 @@ export function getNamedFormat<T extends keyof CustomFormats>(
name: string,
onError: OnErrorFn
):
| NumberFormatOptions
| Intl.NumberFormatOptions
| Intl.DateTimeFormatOptions
| Intl.RelativeTimeFormatOptions
| undefined {
Expand Down
3 changes: 1 addition & 2 deletions packages/intl/tests/formatTime.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable @typescript-eslint/camelcase */
import {DateTimeFormat} from '@formatjs/ecma402-abstract'
import {formatTime as formatTimeFn} from '../src/dateTime'
import {Formatters, IntlConfig, IntlFormatters} from '../src/types'

Expand All @@ -10,7 +9,7 @@ describe('format API', () => {

let getDateTimeFormat: Formatters['getDateTimeFormat'] = (
...args: ConstructorParameters<typeof Intl.DateTimeFormat>
) => new Intl.DateTimeFormat(...args) as DateTimeFormat
) => new Intl.DateTimeFormat(...args)
beforeEach(() => {
config = {
locale: 'en',
Expand Down

0 comments on commit 3fed5f4

Please sign in to comment.