Skip to content

Commit

Permalink
Fix plugins: rename Locale type param to LocaleDoc to avoid confusion
Browse files Browse the repository at this point in the history
  • Loading branch information
Raiondesu committed Dec 19, 2023
1 parent 941de83 commit b812294
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 24 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export interface PluginRecord<
* tracks all plugins included throughout the project to simplify type-checking
*/
export interface PluginRegistry<
Locale extends Record<string, any> = Record<string, any>,
Key extends LocaleKey<Locale> = LocaleKey<Locale>,
LocaleDoc extends Record<string, any> = Record<string, any>,
Key extends LocaleKey<LocaleDoc> = LocaleKey<LocaleDoc>,
PluginInfo = unknown,
ContextualPlugins extends Record<keyof PluginRegistry, Plugin> = Record<string, Plugin>
> {
Expand Down
24 changes: 12 additions & 12 deletions packages/plugins/arrays/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import { LocaleKey } from 'intl-schematic';
import { createPlugin } from 'intl-schematic/plugins';

declare module 'intl-schematic/plugins' {
export interface PluginRegistry<Locale, Key, PluginInfo, ContextualPlugins> {
export interface PluginRegistry<LocaleDoc, Key, PluginInfo, ContextualPlugins> {
ArraysPlugin: {
// Any attempt to externalize these types leads to a rat race between a type simplifier and a type inferer
args: [
references: keyof Exclude<Locale[Key][number], string> extends infer Keys
? Keys extends LocaleKey<Locale>
references: keyof Exclude<LocaleDoc[Key][number], string> extends infer Keys
? Keys extends LocaleKey<LocaleDoc>
? {
[key in Keys]:
GetPluginNameFromContext<Locale, key, ContextualPlugins> extends infer PluginName
GetPluginNameFromContext<LocaleDoc, key, ContextualPlugins> extends infer PluginName
? PluginName extends keyof PluginRegistry
? PluginRegistry<Locale, key,
? PluginRegistry<LocaleDoc, key,
ContextualPlugins[PluginName]['info'],
ContextualPlugins
>[PluginName] extends PluginRecord<infer Args>
Expand All @@ -23,10 +23,10 @@ declare module 'intl-schematic/plugins' {
} : {
// In case references for the key weren't recognized,
// iterate show user all locale keys' parameter types as options
[key in LocaleKey<Locale>]?:
GetPluginNameFromContext<Locale, key, ContextualPlugins> extends infer PluginName
[key in LocaleKey<LocaleDoc>]?:
GetPluginNameFromContext<LocaleDoc, key, ContextualPlugins> extends infer PluginName
? PluginName extends keyof PluginRegistry
? PluginRegistry<Locale, key,
? PluginRegistry<LocaleDoc, key,
ContextualPlugins[PluginName]['info'],
ContextualPlugins
>[PluginName] extends PluginRecord<infer Args>
Expand All @@ -42,17 +42,17 @@ declare module 'intl-schematic/plugins' {
// Extracts referenced keys from arrays and detects their processors and signatures
// to display them to the user
signature: {
[key in keyof Exclude<Locale[Key][number], string> & LocaleKey<Locale>]:
GetPluginNameFromContext<Locale, key, ContextualPlugins> extends infer PluginName
[key in keyof Exclude<LocaleDoc[Key][number], string> & LocaleKey<LocaleDoc>]:
GetPluginNameFromContext<LocaleDoc, key, ContextualPlugins> extends infer PluginName
? PluginName extends keyof PluginRegistry
? PluginRegistry<Locale, key,
? PluginRegistry<LocaleDoc, key,
ContextualPlugins[PluginName]['info'],
ContextualPlugins
>[PluginName] extends PluginRecord<any, any, infer Signature>
? [PluginName, Signature]
: PluginName
: PluginName
: Locale[key];
: LocaleDoc[key];
};
};
}
Expand Down
4 changes: 2 additions & 2 deletions packages/plugins/functions/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { createPlugin } from 'intl-schematic/plugins';

declare module 'intl-schematic/plugins' {
export interface PluginRegistry<Locale, Key> {
export interface PluginRegistry<LocaleDoc, Key> {
Functions: {
args: Locale[Key] extends (...args: infer Args) => string ? Args : [];
args: LocaleDoc[Key] extends (...args: infer Args) => string ? Args : [];
};
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/plugins/nested/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { createPlugin } from 'intl-schematic/plugins';

declare module 'intl-schematic/plugins' {
export interface PluginRegistry<Locale, Key> {
export interface PluginRegistry<LocaleDoc, Key> {
NestedKeys: {
args: Locale[Key] extends string ? [] : Leaves<Locale[Key]>;
args: LocaleDoc[Key] extends string ? [] : Leaves<LocaleDoc[Key]>;
};
}
}
Expand Down
14 changes: 8 additions & 6 deletions packages/plugins/processors/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@ import type {} from '@intl-schematic/plugin-locale';
import { InputObject, ParameterObject, Processor, Processors, getLocalizedProcessors } from './plugin-core';

declare module 'intl-schematic/plugins' {
export interface PluginRegistry<Locale, Key, PluginInfo> {
export interface PluginRegistry<LocaleDoc, Key, PluginInfo> {
ProcessorsPlugin: {
// Any attempt to externalize these types leads to a rat race between a type simplifier and a type inferer
args: PluginInfo extends Processors
? 'processor' extends keyof Locale[Key]
? 'processor' extends keyof LocaleDoc[Key]
// legacy format
? { [key in keyof PluginInfo]: PluginInfo[key] extends Processor<infer Input, infer Param>
? [input: Input, parameter?: Param] : never;
}[Extract<keyof PluginInfo, keyof Locale[Key]['processor']>]
}[Extract<keyof PluginInfo, keyof LocaleDoc[Key]['processor']>]
// new format
: { [key in keyof PluginInfo]: PluginInfo[key] extends Processor<infer Input, infer Param>
? [input: Input, parameter?: Param] : never;
}[Extract<keyof PluginInfo, keyof Locale[Key]>]
}[Extract<keyof PluginInfo, keyof LocaleDoc[Key]>]
: [input?: unknown, parameter?: unknown];

info: Processors;

// Extracts a processor and a parameter from a translation key to display
signature: Locale[Key];
signature: LocaleDoc[Key];
};
}
}
Expand All @@ -47,7 +47,9 @@ export const ProcessorsPlugin = <P extends Processors>(processors: P) => {
const localizedProcessorsByLocale: Record<string, Record<string, ReturnType<Processor>>> = {};

return createPlugin('ProcessorsPlugin',
function isParametrized(value: unknown): value is ParametrizedTranslationRecord<Extract<keyof P, string>> | LegacyParametrizedTranslationRecord {
function isParametrized(value: unknown):
value is ParametrizedTranslationRecord<Extract<keyof P, string>> | LegacyParametrizedTranslationRecord
{
if (typeof value !== 'object' || value == null) {
return false;
}
Expand Down

0 comments on commit b812294

Please sign in to comment.