@formatjs/ts-transformer 3.8.2 → 3.9.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.
- package/package.json +1 -1
- package/src/transform.d.ts +1 -1
- package/src/transform.d.ts.map +1 -1
- package/src/transform.js +53 -18
- package/src/types.d.ts +1 -1
- package/src/types.d.ts.map +1 -1
package/package.json
CHANGED
package/src/transform.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import * as typescript from 'typescript';
|
|
|
2
2
|
import { MessageDescriptor } from './types';
|
|
3
3
|
export declare type Extractor = (filePath: string, msgs: MessageDescriptor[]) => void;
|
|
4
4
|
export declare type MetaExtractor = (filePath: string, meta: Record<string, string>) => void;
|
|
5
|
-
export declare type InterpolateNameFn = (id?:
|
|
5
|
+
export declare type InterpolateNameFn = (id?: MessageDescriptor['id'], defaultMessage?: MessageDescriptor['defaultMessage'], description?: MessageDescriptor['description'], filePath?: string) => string;
|
|
6
6
|
declare type TypeScript = typeof typescript;
|
|
7
7
|
export interface Opts {
|
|
8
8
|
/**
|
package/src/transform.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transform.d.ts","sourceRoot":"","sources":["../../../../../../packages/ts-transformer/src/transform.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,UAAU,MAAM,YAAY,CAAA;AACxC,OAAO,EAAC,iBAAiB,EAAC,MAAM,SAAS,CAAA;AAIzC,oBAAY,SAAS,GAAG,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,iBAAiB,EAAE,KAAK,IAAI,CAAA;AAC7E,oBAAY,aAAa,GAAG,CAC1B,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,KACzB,IAAI,CAAA;AAET,oBAAY,iBAAiB,GAAG,CAC9B,EAAE,CAAC,EAAE,
|
|
1
|
+
{"version":3,"file":"transform.d.ts","sourceRoot":"","sources":["../../../../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AgB,eAAe,CAAC,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,IAAI,wDA6BzD;AAED,wBAAgB,SAAS,CAAC,IAAI,EAAE,IAAI,wDAEnC"}
|
package/src/transform.js
CHANGED
|
@@ -54,6 +54,33 @@ function objToTSNode(factory, obj) {
|
|
|
54
54
|
function messageASTToTSNode(factory, ast) {
|
|
55
55
|
return factory.createArrayLiteralExpression(ast.map(function (el) { return objToTSNode(factory, el); }));
|
|
56
56
|
}
|
|
57
|
+
function literalToObj(ts, n) {
|
|
58
|
+
if (ts.isNumericLiteral(n)) {
|
|
59
|
+
return +n.text;
|
|
60
|
+
}
|
|
61
|
+
if (ts.isStringLiteral(n)) {
|
|
62
|
+
return n.text;
|
|
63
|
+
}
|
|
64
|
+
if (n.kind === ts.SyntaxKind.TrueKeyword) {
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
if (n.kind === ts.SyntaxKind.FalseKeyword) {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function objectLiteralExpressionToObj(ts, obj) {
|
|
72
|
+
return obj.properties.reduce(function (all, prop) {
|
|
73
|
+
if (ts.isPropertyAssignment(prop) && prop.name) {
|
|
74
|
+
if (ts.isIdentifier(prop.name)) {
|
|
75
|
+
all[prop.name.escapedText.toString()] = literalToObj(ts, prop.initializer);
|
|
76
|
+
}
|
|
77
|
+
else if (ts.isStringLiteral(prop.name)) {
|
|
78
|
+
all[prop.name.text] = literalToObj(ts, prop.initializer);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return all;
|
|
82
|
+
}, {});
|
|
83
|
+
}
|
|
57
84
|
var DEFAULT_OPTS = {
|
|
58
85
|
onMsgExtracted: function () { return undefined; },
|
|
59
86
|
onMetaExtracted: function () { return undefined; },
|
|
@@ -115,24 +142,8 @@ function extractMessageDescriptor(ts, node, _a, sf) {
|
|
|
115
142
|
? prop.initializer
|
|
116
143
|
: undefined;
|
|
117
144
|
if (name && ts.isIdentifier(name) && initializer) {
|
|
118
|
-
// <FormattedMessage foo={'barbaz'} />
|
|
119
|
-
if (ts.isJsxExpression(initializer) &&
|
|
120
|
-
initializer.expression &&
|
|
121
|
-
ts.isStringLiteral(initializer.expression)) {
|
|
122
|
-
switch (name.text) {
|
|
123
|
-
case 'id':
|
|
124
|
-
msg.id = initializer.expression.text;
|
|
125
|
-
break;
|
|
126
|
-
case 'defaultMessage':
|
|
127
|
-
msg.defaultMessage = initializer.expression.text;
|
|
128
|
-
break;
|
|
129
|
-
case 'description':
|
|
130
|
-
msg.description = initializer.expression.text;
|
|
131
|
-
break;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
145
|
// {id: 'id'}
|
|
135
|
-
|
|
146
|
+
if (ts.isStringLiteral(initializer)) {
|
|
136
147
|
switch (name.text) {
|
|
137
148
|
case 'id':
|
|
138
149
|
msg.id = initializer.text;
|
|
@@ -160,8 +171,27 @@ function extractMessageDescriptor(ts, node, _a, sf) {
|
|
|
160
171
|
}
|
|
161
172
|
}
|
|
162
173
|
else if (ts.isJsxExpression(initializer) && initializer.expression) {
|
|
174
|
+
// <FormattedMessage foo={'barbaz'} />
|
|
175
|
+
if (ts.isStringLiteral(initializer.expression)) {
|
|
176
|
+
switch (name.text) {
|
|
177
|
+
case 'id':
|
|
178
|
+
msg.id = initializer.expression.text;
|
|
179
|
+
break;
|
|
180
|
+
case 'defaultMessage':
|
|
181
|
+
msg.defaultMessage = initializer.expression.text;
|
|
182
|
+
break;
|
|
183
|
+
case 'description':
|
|
184
|
+
msg.description = initializer.expression.text;
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
// description={{custom: 1}}
|
|
189
|
+
else if (ts.isObjectLiteralExpression(initializer.expression) &&
|
|
190
|
+
name.text === 'description') {
|
|
191
|
+
msg.description = objectLiteralExpressionToObj(ts, initializer.expression);
|
|
192
|
+
}
|
|
163
193
|
// <FormattedMessage foo={`bar`} />
|
|
164
|
-
if (ts.isNoSubstitutionTemplateLiteral(initializer.expression)) {
|
|
194
|
+
else if (ts.isNoSubstitutionTemplateLiteral(initializer.expression)) {
|
|
165
195
|
var expression = initializer.expression;
|
|
166
196
|
switch (name.text) {
|
|
167
197
|
case 'id':
|
|
@@ -211,6 +241,11 @@ function extractMessageDescriptor(ts, node, _a, sf) {
|
|
|
211
241
|
}
|
|
212
242
|
}
|
|
213
243
|
}
|
|
244
|
+
// description: {custom: 1}
|
|
245
|
+
else if (ts.isObjectLiteralExpression(initializer) &&
|
|
246
|
+
name.text === 'description') {
|
|
247
|
+
msg.description = objectLiteralExpressionToObj(ts, initializer);
|
|
248
|
+
}
|
|
214
249
|
}
|
|
215
250
|
});
|
|
216
251
|
// We extracted nothing
|
package/src/types.d.ts
CHANGED
package/src/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../../../packages/ts-transformer/src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,iBAAiB;IAChC,EAAE,EAAE,MAAM,CAAA;IACV,WAAW,CAAC,EAAE,MAAM,CAAA;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../../../packages/ts-transformer/src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,iBAAiB;IAChC,EAAE,EAAE,MAAM,CAAA;IACV,WAAW,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;IAC7B,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,GAAG,CAAC,EAAE,MAAM,CAAA;CACb;AAED,MAAM,WAAW,QAAQ;IACvB,CAAC,GAAG,EAAE,MAAM,GAAG,iBAAiB,CAAA;CACjC"}
|