@discord/intl 0.12.0 → 0.13.0-canary.45435b1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/format.d.ts CHANGED
@@ -20,12 +20,12 @@ import { AstNode } from '@discord/intl-ast';
20
20
  import { Formatters } from 'intl-messageformat';
21
21
  import type { FormatConfig } from './format-config';
22
22
  import type { RichTextTagNames } from './types';
23
- export declare abstract class FormatBuilder<Result> {
23
+ export declare abstract class FormatBuilder<Result, ObjectType = Result extends object ? Result : never> {
24
24
  abstract pushRichTextTag(tag: RichTextTagNames, children: Result[], control: Result[]): void;
25
25
  abstract pushLiteralText(text: string): void;
26
- abstract pushObject(value: object): void;
26
+ abstract pushObject(value: ObjectType): void;
27
27
  abstract finish(): Result[];
28
28
  }
29
29
  export type FormatBuilderConstructor<Result> = new () => FormatBuilder<Result>;
30
- export declare function bindFormatValuesWithBuilder<T, Builder extends FormatBuilder<T>>(builder: Builder, nodes: AstNode[], locales: string | string[], formatters: Formatters, formatConfig: FormatConfig, values?: Record<string, string | object>, currentPluralValue?: number, originalMessage?: string): void;
30
+ export declare function bindFormatValuesWithBuilder<T, ObjectType, Builder extends FormatBuilder<T, ObjectType>>(builder: Builder, nodes: AstNode[], locales: string | string[], formatters: Formatters, formatConfig: FormatConfig, values?: Record<string, string | object>, currentPluralValue?: number, originalMessage?: string): void;
31
31
  export declare function bindFormatValues<Result>(Builder: FormatBuilderConstructor<Result>, nodes: string | AstNode[], locales: string | string[], formatters: Formatters, formatConfig: FormatConfig, values?: Record<string, string | object>, currentPluralValue?: number): Result[];
package/dist/format.js CHANGED
@@ -67,6 +67,13 @@ function bindFormatValuesWithBuilder(builder, nodes, locales, formatters, format
67
67
  const value = values[variableName];
68
68
  switch (nodeType) {
69
69
  case intl_ast_1.FormatJsNodeType.Argument:
70
+ // `function` here captures ReactNodes, which can be functions that
71
+ // return elements. Basically everything else fits in the `object`
72
+ // type, and the generic type requires that an explicit ObjectType
73
+ // be specified for cases where it's not. That means there's an
74
+ // escape hatch that _might_ break this, but it would have to be
75
+ // intentionally set by internal code to function that way, so we'll
76
+ // operate on a promise that it won't for now.
70
77
  if (typeof value === 'object' || typeof value === 'function') {
71
78
  builder.pushObject(value);
72
79
  }
@@ -50,10 +50,7 @@ interface RichTextLinkNode extends RichTextNodeBase<RichTextNode[]> {
50
50
  type: RichTextNodeType.Link;
51
51
  target: string;
52
52
  }
53
- interface RichTextObjectNode extends RichTextNodeBase<object> {
54
- type: RichTextNodeType.Object;
55
- }
56
- export type RichTextNode = RichTextTextNode | RichTextStrongNode | RichTextEmphasisNode | RichTextStrikethroughNode | RichTextCodeNode | RichTextParagraphNode | RichTextLinkNode | RichTextObjectNode;
53
+ export type RichTextNode = RichTextTextNode | RichTextStrongNode | RichTextEmphasisNode | RichTextStrikethroughNode | RichTextCodeNode | RichTextParagraphNode | RichTextLinkNode;
57
54
  /**
58
55
  * Format the given message in the current locale with the provided values.
59
56
  * The returned values is _always_ an Array of parts, even if the message is
@@ -65,7 +65,7 @@ class AstBuilder extends format_1.FormatBuilder {
65
65
  }
66
66
  }
67
67
  pushObject(value) {
68
- this.result.push({ type: RichTextNodeType.Object, content: value });
68
+ this.result.push(value);
69
69
  }
70
70
  finish() {
71
71
  return this.result;
@@ -41,8 +41,6 @@ function createReactBuilder(richTextElements) {
41
41
  }
42
42
  }
43
43
  pushObject(value) {
44
- // @ts-expect-error this is technically invalid, but we'll just assume that if a format
45
- // returns an object, it'll be acting as some form of ReactNode.
46
44
  this.result.push(value);
47
45
  }
48
46
  finish() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@discord/intl",
3
- "version": "0.12.0",
3
+ "version": "0.13.0-canary.45435b1",
4
4
  "license": "MIT",
5
5
  "description": "Client runtime for managing messages and translations in a React project.",
6
6
  "main": "./dist/index.js",
@@ -23,7 +23,7 @@
23
23
  "@formatjs/intl": "^2.10.1",
24
24
  "@intrnl/xxhash64": "^0.1.2",
25
25
  "intl-messageformat": "^10.5.11",
26
- "@discord/intl-ast": "0.12.0"
26
+ "@discord/intl-ast": "0.13.0"
27
27
  },
28
28
  "devDependencies": {
29
29
  "@swc/cli": "^0.3.12",
package/src/format.ts CHANGED
@@ -36,16 +36,20 @@ function isRichTextTag(name: string) {
36
36
  return name[0] === '$';
37
37
  }
38
38
 
39
- export abstract class FormatBuilder<Result> {
39
+ export abstract class FormatBuilder<Result, ObjectType = Result extends object ? Result : never> {
40
40
  abstract pushRichTextTag(tag: RichTextTagNames, children: Result[], control: Result[]): void;
41
41
  abstract pushLiteralText(text: string): void;
42
- abstract pushObject(value: object): void;
42
+ abstract pushObject(value: ObjectType): void;
43
43
  abstract finish(): Result[];
44
44
  }
45
45
 
46
46
  export type FormatBuilderConstructor<Result> = new () => FormatBuilder<Result>;
47
47
 
48
- export function bindFormatValuesWithBuilder<T, Builder extends FormatBuilder<T>>(
48
+ export function bindFormatValuesWithBuilder<
49
+ T,
50
+ ObjectType,
51
+ Builder extends FormatBuilder<T, ObjectType>,
52
+ >(
49
53
  builder: Builder,
50
54
  nodes: AstNode[],
51
55
  locales: string | string[],
@@ -89,8 +93,15 @@ export function bindFormatValuesWithBuilder<T, Builder extends FormatBuilder<T>>
89
93
  const value = values[variableName];
90
94
  switch (nodeType) {
91
95
  case FormatJsNodeType.Argument:
96
+ // `function` here captures ReactNodes, which can be functions that
97
+ // return elements. Basically everything else fits in the `object`
98
+ // type, and the generic type requires that an explicit ObjectType
99
+ // be specified for cases where it's not. That means there's an
100
+ // escape hatch that _might_ break this, but it would have to be
101
+ // intentionally set by internal code to function that way, so we'll
102
+ // operate on a promise that it won't for now.
92
103
  if (typeof value === 'object' || typeof value === 'function') {
93
- builder.pushObject(value);
104
+ builder.pushObject(value as ObjectType);
94
105
  } else {
95
106
  // Taken from FormatJS: non-objects (strings, numbers, and falsy
96
107
  // values) all get cast to strings immediately as literal nodes.
@@ -63,9 +63,6 @@ interface RichTextLinkNode extends RichTextNodeBase<RichTextNode[]> {
63
63
  type: RichTextNodeType.Link;
64
64
  target: string;
65
65
  }
66
- interface RichTextObjectNode extends RichTextNodeBase<object> {
67
- type: RichTextNodeType.Object;
68
- }
69
66
 
70
67
  export type RichTextNode =
71
68
  | RichTextTextNode
@@ -74,8 +71,7 @@ export type RichTextNode =
74
71
  | RichTextStrikethroughNode
75
72
  | RichTextCodeNode
76
73
  | RichTextParagraphNode
77
- | RichTextLinkNode
78
- | RichTextObjectNode;
74
+ | RichTextLinkNode;
79
75
 
80
76
  const AST_RICH_TEXT_ELEMENTS: RichTextFormattingMap<AstFunctionTypes['hook']> = {
81
77
  $b: (content) => ({ type: RichTextNodeType.Strong, content }),
@@ -114,8 +110,8 @@ class AstBuilder extends FormatBuilder<RichTextNode> {
114
110
  }
115
111
  }
116
112
 
117
- pushObject(value: object) {
118
- this.result.push({ type: RichTextNodeType.Object, content: value });
113
+ pushObject(value: RichTextNode) {
114
+ this.result.push(value);
119
115
  }
120
116
 
121
117
  finish(): RichTextNode[] {
@@ -66,9 +66,7 @@ function createReactBuilder(richTextElements: RichTextFormattingMap<ReactFunctio
66
66
  }
67
67
  }
68
68
 
69
- pushObject(value: object) {
70
- // @ts-expect-error this is technically invalid, but we'll just assume that if a format
71
- // returns an object, it'll be acting as some form of ReactNode.
69
+ pushObject(value: React.ReactNode) {
72
70
  this.result.push(value);
73
71
  }
74
72