@formatjs/ts-transformer 4.3.1 → 4.3.3

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@formatjs/ts-transformer",
3
3
  "description": "TS Compiler transformer for formatjs",
4
- "version": "4.3.1",
4
+ "version": "4.3.3",
5
5
  "license": "MIT",
6
6
  "author": "Long Ho <holevietlong@gmail.com>",
7
7
  "type": "module",
@@ -12,7 +12,7 @@
12
12
  "json-stable-stringify": "^1.3.0",
13
13
  "tslib": "^2.8.1",
14
14
  "typescript": "^5.6.0",
15
- "@formatjs/icu-messageformat-parser": "3.5.0"
15
+ "@formatjs/icu-messageformat-parser": "3.5.1"
16
16
  },
17
17
  "devDependencies": {
18
18
  "ts-jest": "^29"
@@ -75,6 +75,16 @@ export interface Opts {
75
75
  * Whether to hoist selectors & flatten sentences
76
76
  */
77
77
  flatten?: boolean;
78
+ /**
79
+ * Whether to throw on errors extracting messages.
80
+ * When false, invalid messages are skipped with a warning.
81
+ * Defaults to true.
82
+ */
83
+ throws?: boolean;
84
+ /**
85
+ * Callback for reporting errors when throws is false.
86
+ */
87
+ onMsgError?: (filePath: string, error: Error) => void;
78
88
  }
79
89
  export declare function transformWithTs(ts: TypeScript, opts: Opts): typescript.TransformerFactory<typescript.SourceFile>;
80
90
  export declare function transform(opts: Opts): typescript.TransformerFactory<typescript.SourceFile>;
package/src/transform.js CHANGED
@@ -99,7 +99,19 @@ function evaluateStringConcat(ts, node) {
99
99
  }
100
100
  return ["", false];
101
101
  }
102
- function extractMessageDescriptor(ts, node, { overrideIdFn, extractSourceLocation, preserveWhitespace, flatten }, sf) {
102
+ function extractMessageDescriptor(ts, node, { overrideIdFn, extractSourceLocation, preserveWhitespace, flatten, throws = true, onMsgError }, sf) {
103
+ let extractionError = null;
104
+ // Helper to handle errors based on throws option
105
+ function handleError(errorMsg) {
106
+ const error = new Error(errorMsg);
107
+ if (throws) {
108
+ throw error;
109
+ }
110
+ extractionError = error;
111
+ if (onMsgError) {
112
+ onMsgError(sf.fileName, error);
113
+ }
114
+ }
103
115
  let properties = undefined;
104
116
  if (ts.isObjectLiteralExpression(node)) {
105
117
  properties = node.properties;
@@ -147,7 +159,8 @@ function extractMessageDescriptor(ts, node, { overrideIdFn, extractSourceLocatio
147
159
  }
148
160
  const { template } = initializer;
149
161
  if (!ts.isNoSubstitutionTemplateLiteral(template)) {
150
- throw new Error("Tagged template expression must be no substitution");
162
+ handleError("[FormatJS] Tagged template expression must be no substitution");
163
+ return;
151
164
  }
152
165
  switch (name.text) {
153
166
  case "id":
@@ -197,7 +210,8 @@ function extractMessageDescriptor(ts, node, { overrideIdFn, extractSourceLocatio
197
210
  }
198
211
  const { expression: { template } } = initializer;
199
212
  if (!ts.isNoSubstitutionTemplateLiteral(template)) {
200
- throw new Error("Tagged template expression must be no substitution");
213
+ handleError("[FormatJS] Tagged template expression must be no substitution");
214
+ return;
201
215
  }
202
216
  switch (name.text) {
203
217
  case "id":
@@ -227,10 +241,12 @@ function extractMessageDescriptor(ts, node, { overrideIdFn, extractSourceLocatio
227
241
  }
228
242
  } else if (MESSAGE_DESC_KEYS.includes(name.text) && name.text !== "description") {
229
243
  // Non-static expression for defaultMessage or id
230
- throw new Error(`[FormatJS] \`${name.text}\` must be a string literal or statically evaluable expression to be extracted.`);
244
+ handleError(`[FormatJS] \`${name.text}\` must be a string literal or statically evaluable expression to be extracted.`);
245
+ return;
231
246
  }
232
247
  } else if (MESSAGE_DESC_KEYS.includes(name.text) && name.text !== "description") {
233
- throw new Error(`[FormatJS] \`${name.text}\` must be a string literal to be extracted.`);
248
+ handleError(`[FormatJS] \`${name.text}\` must be a string literal to be extracted.`);
249
+ return;
234
250
  }
235
251
  } else if (ts.isBinaryExpression(initializer)) {
236
252
  const [result, isStatic] = evaluateStringConcat(ts, initializer);
@@ -248,15 +264,21 @@ function extractMessageDescriptor(ts, node, { overrideIdFn, extractSourceLocatio
248
264
  }
249
265
  } else if (MESSAGE_DESC_KEYS.includes(name.text) && name.text !== "description") {
250
266
  // Non-static expression for defaultMessage or id
251
- throw new Error(`[FormatJS] \`${name.text}\` must be a string literal or statically evaluable expression to be extracted.`);
267
+ handleError(`[FormatJS] \`${name.text}\` must be a string literal or statically evaluable expression to be extracted.`);
268
+ return;
252
269
  }
253
270
  } else if (ts.isObjectLiteralExpression(initializer) && name.text === "description") {
254
271
  msg.description = objectLiteralExpressionToObj(ts, initializer);
255
272
  } else if (MESSAGE_DESC_KEYS.includes(name.text) && name.text !== "description") {
256
- throw new Error(`[FormatJS] \`${name.text}\` must be a string literal to be extracted.`);
273
+ handleError(`[FormatJS] \`${name.text}\` must be a string literal to be extracted.`);
274
+ return;
257
275
  }
258
276
  }
259
277
  });
278
+ // If we had an extraction error (and throws is false), skip this message
279
+ if (extractionError) {
280
+ return;
281
+ }
260
282
  // We extracted nothing
261
283
  if (!msg.defaultMessage && !msg.id) {
262
284
  return;