@formatjs/ts-transformer 4.3.2 → 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 +1 -1
- package/src/transform.d.ts +10 -0
- package/src/transform.js +29 -7
package/package.json
CHANGED
package/src/transform.d.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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;
|