@discord/intl 0.7.1 → 0.8.0-rc.0

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.
@@ -34,7 +34,13 @@ function createReactBuilder(richTextElements) {
34
34
  this.result.push(richTextElements[tag](children, `${this._nodeKey++}`));
35
35
  }
36
36
  pushLiteralText(text) {
37
- this.result.push(text);
37
+ const last = this.result[this.result.length - 1];
38
+ if (typeof last === 'string') {
39
+ this.result[this.result.length - 1] += text;
40
+ }
41
+ else {
42
+ this.result.push(text);
43
+ }
38
44
  }
39
45
  pushObject(value) {
40
46
  // @ts-expect-error this is technically invalid, but we'll just assume that if a format returns
@@ -71,17 +71,15 @@ class IntlManager {
71
71
  const resolved = message(this.currentLocale);
72
72
  if (resolved == null || resolved.ast.length === 0)
73
73
  return '';
74
- const value = resolved.ast[0];
75
- if (typeof value !== 'string') {
76
- throw new Error('Attempted to call `string` formatting on a non-literal message: ' +
77
- JSON.stringify(resolved));
74
+ let result = '';
75
+ for (const element of resolved.ast) {
76
+ if (typeof element !== 'string') {
77
+ throw new Error('Attempted to call `string` formatting on a non-literal message: ' +
78
+ JSON.stringify(resolved));
79
+ }
80
+ result += element;
78
81
  }
79
- // TODO: Figure out how to make this typing exact. This currently relies on
80
- // the generic typing being sound enough to know that the message can only
81
- // contain a single static text node and no placeholders or rich text.
82
- // As it stands, this is _technically_ typed as string | undefined, even though
83
- // TypeScript doesn't actually complain about it.
84
- return value;
82
+ return result;
85
83
  }
86
84
  /**
87
85
  * Return a raw string representing the syntax of the original message, as
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@discord/intl",
3
- "version": "0.7.1",
3
+ "version": "0.8.0-rc.0",
4
4
  "license": "MIT",
5
5
  "description": "Client runtime for managing messages and translations in a React project.",
6
6
  "main": "./dist/index.js",
@@ -19,7 +19,7 @@
19
19
  "@formatjs/intl": "^2.10.1",
20
20
  "@intrnl/xxhash64": "^0.1.2",
21
21
  "intl-messageformat": "^10.5.11",
22
- "@discord/intl-ast": "0.7.1"
22
+ "@discord/intl-ast": "0.8.0-rc.0"
23
23
  },
24
24
  "devDependencies": {
25
25
  "@swc/cli": "^0.3.12",
@@ -56,7 +56,12 @@ function createReactBuilder(richTextElements: RichTextFormattingMap<ReactFunctio
56
56
  }
57
57
 
58
58
  pushLiteralText(text: string) {
59
- this.result.push(text);
59
+ const last = this.result[this.result.length - 1];
60
+ if (typeof last === 'string') {
61
+ this.result[this.result.length - 1] += text;
62
+ } else {
63
+ this.result.push(text);
64
+ }
60
65
  }
61
66
 
62
67
  pushObject(value: object) {
@@ -120,20 +120,17 @@ export class IntlManager {
120
120
  const resolved = message(this.currentLocale);
121
121
  if (resolved == null || resolved.ast.length === 0) return '';
122
122
 
123
- const value = resolved.ast[0] as LiteralNode;
124
- if (typeof value !== 'string') {
125
- throw new Error(
126
- 'Attempted to call `string` formatting on a non-literal message: ' +
127
- JSON.stringify(resolved),
128
- );
123
+ let result = '';
124
+ for (const element of resolved.ast) {
125
+ if (typeof element !== 'string') {
126
+ throw new Error(
127
+ 'Attempted to call `string` formatting on a non-literal message: ' +
128
+ JSON.stringify(resolved),
129
+ );
130
+ }
131
+ result += element;
129
132
  }
130
-
131
- // TODO: Figure out how to make this typing exact. This currently relies on
132
- // the generic typing being sound enough to know that the message can only
133
- // contain a single static text node and no placeholders or rich text.
134
- // As it stands, this is _technically_ typed as string | undefined, even though
135
- // TypeScript doesn't actually complain about it.
136
- return value;
133
+ return result;
137
134
  }
138
135
 
139
136
  /**