This repository has been archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
550 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
export * from './secondary-exports.ts' | ||
export { plugin as icuMessages } from './plugin/index.ts' | ||
|
||
export { | ||
plugin as icuMessages, | ||
type Options as PluginOptions, | ||
} from './plugin/index.ts' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { | ||
type MessageFormatElement, | ||
createLiteralElement, | ||
type ParserOptions, | ||
} from '@formatjs/icu-messageformat-parser' | ||
|
||
type ParseErrorHandlerResult = MessageFormatElement[] | undefined | void | ||
|
||
/** | ||
* A function that handles parsing error and either throws another error, or | ||
* returns a fallback string or parse result. | ||
* | ||
* @param context Context containing the error, message ID and other relevant | ||
* properties. | ||
* @returns The fallback value or nothing. | ||
*/ | ||
export type ParseErrorHandler = ( | ||
context: ParseErrorContext, | ||
) => ParseErrorHandlerResult | ||
|
||
export const builtinStrategies = { | ||
/** @returns The original message as a literal. */ | ||
'use-message-as-literal'(ctx) { | ||
return [createLiteralElement(ctx.message)] | ||
}, | ||
/** @returns The message ID as a literal. */ | ||
'use-id-as-literal'(ctx) { | ||
return [createLiteralElement(ctx.messageId)] | ||
}, | ||
/** @returns Empty literal. */ | ||
'use-empty-literal'() { | ||
return [createLiteralElement('')] | ||
}, | ||
/** @returns `undefined`, which skips the string. */ | ||
skip() { | ||
return undefined | ||
}, | ||
} satisfies Record<string, ParseErrorHandler> | ||
|
||
Object.setPrototypeOf(builtinStrategies, null) | ||
|
||
type ParseErrorHandlingStrategy = keyof typeof builtinStrategies | ||
|
||
export type ParseErrorHandlingOption = | ||
| ParseErrorHandler | ||
| ParseErrorHandlingStrategy | ||
|
||
/** | ||
* Resolve error handler function. | ||
* | ||
* @param option Either an error handler to return back or the name of the | ||
* built-in handling strategy. | ||
* @returns Resolved error handler. | ||
* @throws {Error} If called with unknown built-in handling strategy name. | ||
*/ | ||
export function resolveParseErrorHandler(option: ParseErrorHandlingOption) { | ||
if (typeof option === 'function') return option | ||
|
||
if (Object.hasOwn(builtinStrategies, option)) return builtinStrategies[option] | ||
|
||
throw new Error(`Cannot resolve built-in strategy with name "${option}"`) | ||
} | ||
|
||
interface ParseErrorContext { | ||
/** ID of the module that is being parsed. */ | ||
get moduleId(): string | ||
|
||
/** ID of the message that cannot be parsed. */ | ||
get messageId(): string | ||
|
||
/** Message that cannot be parsed. */ | ||
get message(): string | ||
|
||
/** Error that occurred during the parsing. */ | ||
get error(): unknown | ||
|
||
/** Parser options that were used to parse the message. */ | ||
get parserOptions(): ParserOptions | undefined | ||
|
||
/** | ||
* Call one of the built-in error handling strategies. | ||
* | ||
* @param name Name of the error handling strategy. | ||
* @returns Result for the strategy. | ||
*/ | ||
useBuiltinStrategy(name: ParseErrorHandlingStrategy): ParseErrorHandlerResult | ||
} | ||
|
||
/** | ||
* Creates a new context. | ||
* | ||
* @param info Information required to create a context. | ||
* @returns Newly created context object. | ||
*/ | ||
export function createParseErrorContext( | ||
info: Pick< | ||
ParseErrorContext, | ||
'error' | 'moduleId' | 'message' | 'messageId' | 'parserOptions' | ||
>, | ||
) { | ||
const ctx = { | ||
...info, | ||
useBuiltinStrategy(name: ParseErrorHandlingStrategy) { | ||
return resolveParseErrorHandler(name)(ctx) | ||
}, | ||
} | ||
|
||
return Object.freeze(ctx) satisfies ParseErrorContext | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,13 @@ | ||
export { createOptionsResolver, AnyMessage } from './options.ts' | ||
export type { MessagesParserOptionsValue } from './options.ts' | ||
export { | ||
createOptionsResolver, | ||
AnyMessage, | ||
type MessagesParserOptionsValue, | ||
type ParserOptionsResolver, | ||
} from './options.ts' | ||
export { defaultOptionsResolver } from './default-options-resolver.ts' | ||
export { | ||
createParseErrorContext, | ||
resolveParseErrorHandler, | ||
type ParseErrorHandler, | ||
type ParseErrorHandlingOption, | ||
} from './error-handling.ts' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
import { plugin as basePlugin } from '../plugin/index.ts' | ||
import { plugin as basePlugin, type Options } from '../plugin/index.ts' | ||
|
||
export * from '../secondary-exports.ts' | ||
|
||
export type PluginOptions = Options<any> | ||
|
||
export const icuMessages = basePlugin.webpack |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.