@formatjs/ts-transformer 3.9.9 → 3.10.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/LICENSE.md +0 -0
- package/README.md +0 -0
- package/index.d.ts +0 -0
- package/index.d.ts.map +1 -1
- package/index.js +4 -4
- package/package.json +4 -4
- package/src/console_utils.d.ts +0 -0
- package/src/console_utils.d.ts.map +1 -1
- package/src/console_utils.js +13 -31
- package/src/interpolate-name.d.ts +0 -0
- package/src/interpolate-name.d.ts.map +1 -1
- package/src/interpolate-name.js +27 -32
- package/src/transform.d.ts +0 -0
- package/src/transform.d.ts.map +1 -1
- package/src/transform.js +111 -115
- package/src/types.d.ts +0 -0
- package/src/types.d.ts.map +1 -1
- package/src/types.js +0 -0
- package/ts-jest-integration.d.ts +1 -1
- package/ts-jest-integration.d.ts.map +1 -1
- package/ts-jest-integration.js +1 -1
package/LICENSE.md
CHANGED
|
File without changes
|
package/README.md
CHANGED
|
File without changes
|
package/index.d.ts
CHANGED
|
File without changes
|
package/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAA;AAC/B,cAAc,aAAa,CAAA;AAC3B,cAAc,wBAAwB,CAAA"}
|
package/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./src/transform"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./src/types"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./src/interpolate-name"), exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@formatjs/ts-transformer",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.10.0",
|
|
4
4
|
"description": "TS Compiler transformer for formatjs",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -17,13 +17,13 @@
|
|
|
17
17
|
"react-intl"
|
|
18
18
|
],
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@formatjs/icu-messageformat-parser": "2.1.
|
|
20
|
+
"@formatjs/icu-messageformat-parser": "2.1.7",
|
|
21
21
|
"@types/json-stable-stringify": "^1.0.32",
|
|
22
22
|
"@types/node": "14 || 16 || 17",
|
|
23
23
|
"chalk": "^4.0.0",
|
|
24
24
|
"json-stable-stringify": "^1.0.1",
|
|
25
25
|
"tslib": "2.4.0",
|
|
26
|
-
"typescript": "^4.
|
|
26
|
+
"typescript": "^4.7"
|
|
27
27
|
},
|
|
28
28
|
"peerDependencies": {
|
|
29
29
|
"ts-jest": "27 || 28"
|
|
@@ -39,4 +39,4 @@
|
|
|
39
39
|
"url": "https://github.com/formatjs/formatjs/issues"
|
|
40
40
|
},
|
|
41
41
|
"homepage": "https://github.com/formatjs/formatjs#readme"
|
|
42
|
-
}
|
|
42
|
+
}
|
package/src/console_utils.d.ts
CHANGED
|
File without changes
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"console_utils.d.ts","sourceRoot":"","sources":["
|
|
1
|
+
{"version":3,"file":"console_utils.d.ts","sourceRoot":"","sources":["console_utils.ts"],"names":[],"mappings":"AAeA,wBAAsB,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,iBAM1D;AAED,wBAAgB,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,IAAI,CAG1D;AAED,wBAAgB,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,IAAI,CAG3D"}
|
package/src/console_utils.js
CHANGED
|
@@ -1,49 +1,31 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.error = exports.warn = exports.debug = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var LEVEL_COLORS = {
|
|
4
|
+
const chalk_1 = require("chalk");
|
|
5
|
+
const util_1 = require("util");
|
|
6
|
+
const LEVEL_COLORS = {
|
|
8
7
|
debug: chalk_1.green,
|
|
9
8
|
warn: chalk_1.yellow,
|
|
10
9
|
error: chalk_1.red,
|
|
11
10
|
};
|
|
12
11
|
function label(level, message) {
|
|
13
|
-
return
|
|
12
|
+
return `[@formatjs/ts-transformer] [${LEVEL_COLORS[level](level.toUpperCase())}] ${message}`;
|
|
14
13
|
}
|
|
15
|
-
function debug(message) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
args[_i - 1] = arguments[_i];
|
|
14
|
+
async function debug(message, ...args) {
|
|
15
|
+
if (process.env.LOG_LEVEL !== 'debug') {
|
|
16
|
+
return;
|
|
19
17
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
if (process.env.LOG_LEVEL !== 'debug') {
|
|
23
|
-
return [2 /*return*/];
|
|
24
|
-
}
|
|
25
|
-
console.error(util_1.format.apply(void 0, (0, tslib_1.__spreadArray)([label('debug', message)], args, false)));
|
|
26
|
-
console.error('\n');
|
|
27
|
-
return [2 /*return*/];
|
|
28
|
-
});
|
|
29
|
-
});
|
|
18
|
+
console.error((0, util_1.format)(label('debug', message), ...args));
|
|
19
|
+
console.error('\n');
|
|
30
20
|
}
|
|
31
21
|
exports.debug = debug;
|
|
32
|
-
function warn(message) {
|
|
33
|
-
|
|
34
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
|
35
|
-
args[_i - 1] = arguments[_i];
|
|
36
|
-
}
|
|
37
|
-
console.error(util_1.format.apply(void 0, (0, tslib_1.__spreadArray)([label('warn', message)], args, false)));
|
|
22
|
+
function warn(message, ...args) {
|
|
23
|
+
console.error((0, util_1.format)(label('warn', message), ...args));
|
|
38
24
|
console.error('\n');
|
|
39
25
|
}
|
|
40
26
|
exports.warn = warn;
|
|
41
|
-
function error(message) {
|
|
42
|
-
|
|
43
|
-
for (var _i = 1; _i < arguments.length; _i++) {
|
|
44
|
-
args[_i - 1] = arguments[_i];
|
|
45
|
-
}
|
|
46
|
-
console.error(util_1.format.apply(void 0, (0, tslib_1.__spreadArray)([label('error', message)], args, false)));
|
|
27
|
+
function error(message, ...args) {
|
|
28
|
+
console.error((0, util_1.format)(label('error', message), ...args));
|
|
47
29
|
console.error('\n');
|
|
48
30
|
}
|
|
49
31
|
exports.error = error;
|
|
File without changes
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interpolate-name.d.ts","sourceRoot":"","sources":["
|
|
1
|
+
{"version":3,"file":"interpolate-name.d.ts","sourceRoot":"","sources":["interpolate-name.ts"],"names":[],"mappings":"AAEA,MAAM,WAAW,aAAa;IAC5B,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,OAAO,CAAC,EAAE;QACR,qBAAqB,CACnB,IAAI,EAAE,aAAa,EACnB,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,MAAM,GAAG,MAAM,EACrB,OAAO,EAAE,OAAO,GACf,MAAM,CAAA;KACV,CAAA;CACF;AAED,MAAM,WAAW,OAAO;IACtB,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,MAAM,CAAC,EAAE,MAAM,CAAA;CAChB;AAED,oBAAY,MAAM,GAAG,CAAC,YAAY,CAAC,EAAE,MAAM,EAAE,aAAa,CAAC,EAAE,MAAM,KAAK,MAAM,CAAA;AAa9E,wBAAgB,eAAe,CAC7B,aAAa,EAAE,aAAa,EAC5B,IAAI,EAAE,MAAM,GAAG,MAAM,EACrB,OAAO,EAAE,OAAO,UA6GjB"}
|
package/src/interpolate-name.js
CHANGED
|
@@ -1,37 +1,34 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.interpolateName = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
function getHashDigest(content, hashType, digestType, length) {
|
|
8
|
-
|
|
9
|
-
if (digestType === void 0) { digestType = 'hex'; }
|
|
10
|
-
if (length === void 0) { length = 9999; }
|
|
11
|
-
var hasher = (0, crypto_1.createHash)(hashType);
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const path = tslib_1.__importStar(require("path"));
|
|
6
|
+
const crypto_1 = require("crypto");
|
|
7
|
+
function getHashDigest(content, hashType = 'md5', digestType = 'hex', length = 9999) {
|
|
8
|
+
const hasher = (0, crypto_1.createHash)(hashType);
|
|
12
9
|
hasher.update(content);
|
|
13
10
|
return hasher.digest(digestType).slice(0, length);
|
|
14
11
|
}
|
|
15
12
|
function interpolateName(loaderContext, name, options) {
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
let filename;
|
|
14
|
+
const hasQuery = loaderContext.resourceQuery && loaderContext.resourceQuery.length > 1;
|
|
18
15
|
if (typeof name === 'function') {
|
|
19
16
|
filename = name(loaderContext.resourcePath, hasQuery ? loaderContext.resourceQuery : undefined);
|
|
20
17
|
}
|
|
21
18
|
else {
|
|
22
19
|
filename = name || '[hash].[ext]';
|
|
23
20
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
21
|
+
const context = options.context;
|
|
22
|
+
const content = options.content;
|
|
23
|
+
const regExp = options.regExp;
|
|
24
|
+
let ext = 'bin';
|
|
25
|
+
let basename = 'file';
|
|
26
|
+
let directory = '';
|
|
27
|
+
let folder = '';
|
|
28
|
+
let query = '';
|
|
32
29
|
if (loaderContext.resourcePath) {
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
const parsed = path.parse(loaderContext.resourcePath);
|
|
31
|
+
let resourcePath = loaderContext.resourcePath;
|
|
35
32
|
if (parsed.ext) {
|
|
36
33
|
ext = parsed.ext.slice(1);
|
|
37
34
|
}
|
|
@@ -58,31 +55,29 @@ function interpolateName(loaderContext, name, options) {
|
|
|
58
55
|
}
|
|
59
56
|
if (loaderContext.resourceQuery && loaderContext.resourceQuery.length > 1) {
|
|
60
57
|
query = loaderContext.resourceQuery;
|
|
61
|
-
|
|
58
|
+
const hashIdx = query.indexOf('#');
|
|
62
59
|
if (hashIdx >= 0) {
|
|
63
60
|
query = query.slice(0, hashIdx);
|
|
64
61
|
}
|
|
65
62
|
}
|
|
66
|
-
|
|
63
|
+
let url = filename;
|
|
67
64
|
if (content) {
|
|
68
65
|
// Match hash template
|
|
69
66
|
url = url
|
|
70
67
|
// `hash` and `contenthash` are same in `loader-utils` context
|
|
71
68
|
// let's keep `hash` for backward compatibility
|
|
72
|
-
.replace(/\[(?:([^:\]]+):)?(?:hash|contenthash)(?::([a-z]+\d*))?(?::(\d+))?\]/gi,
|
|
73
|
-
return getHashDigest(content, hashType, digestType, parseInt(maxLength, 10));
|
|
74
|
-
});
|
|
69
|
+
.replace(/\[(?:([^:\]]+):)?(?:hash|contenthash)(?::([a-z]+\d*))?(?::(\d+))?\]/gi, (_, hashType, digestType, maxLength) => getHashDigest(content, hashType, digestType, parseInt(maxLength, 10)));
|
|
75
70
|
}
|
|
76
71
|
url = url
|
|
77
|
-
.replace(/\[ext\]/gi,
|
|
78
|
-
.replace(/\[name\]/gi,
|
|
79
|
-
.replace(/\[path\]/gi,
|
|
80
|
-
.replace(/\[folder\]/gi,
|
|
81
|
-
.replace(/\[query\]/gi,
|
|
72
|
+
.replace(/\[ext\]/gi, () => ext)
|
|
73
|
+
.replace(/\[name\]/gi, () => basename)
|
|
74
|
+
.replace(/\[path\]/gi, () => directory)
|
|
75
|
+
.replace(/\[folder\]/gi, () => folder)
|
|
76
|
+
.replace(/\[query\]/gi, () => query);
|
|
82
77
|
if (regExp && loaderContext.resourcePath) {
|
|
83
|
-
|
|
78
|
+
const match = loaderContext.resourcePath.match(new RegExp(regExp));
|
|
84
79
|
match &&
|
|
85
|
-
match.forEach(
|
|
80
|
+
match.forEach((matched, i) => {
|
|
86
81
|
url = url.replace(new RegExp('\\[' + i + '\\]', 'ig'), matched);
|
|
87
82
|
});
|
|
88
83
|
}
|
package/src/transform.d.ts
CHANGED
|
File without changes
|
package/src/transform.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transform.d.ts","sourceRoot":"","sources":["
|
|
1
|
+
{"version":3,"file":"transform.d.ts","sourceRoot":"","sources":["transform.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,UAAU,MAAM,YAAY,CAAA;AACxC,OAAO,EAAC,iBAAiB,EAAC,MAAM,SAAS,CAAA;AAKzC,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,iBAAiB,CAAC,IAAI,CAAC,EAC5B,cAAc,CAAC,EAAE,iBAAiB,CAAC,gBAAgB,CAAC,EACpD,WAAW,CAAC,EAAE,iBAAiB,CAAC,aAAa,CAAC,EAC9C,QAAQ,CAAC,EAAE,MAAM,KACd,MAAM,CAAA;AAQX,aAAK,UAAU,GAAG,OAAO,UAAU,CAAA;AA2FnC,MAAM,WAAW,IAAI;IACnB;;;;;;;;;;;;OAYG;IACH,MAAM,CAAC,EAAE,MAAM,CAAA;IACf;;;;;OAKG;IACH,qBAAqB,CAAC,EAAE,OAAO,CAAA;IAC/B;;OAEG;IACH,oBAAoB,CAAC,EAAE,OAAO,CAAA;IAC9B;;;OAGG;IACH,wBAAwB,CAAC,EAAE,MAAM,EAAE,CAAA;IACnC;;;;OAIG;IACH,uBAAuB,CAAC,EAAE,MAAM,EAAE,CAAA;IAClC;;;;;;OAMG;IACH,cAAc,CAAC,EAAE,SAAS,CAAA;IAC1B;;;OAGG;IACH,eAAe,CAAC,EAAE,aAAa,CAAA;IAC/B;;;;;;OAMG;IACH,YAAY,CAAC,EAAE,iBAAiB,GAAG,MAAM,CAAA;IACzC;;;OAGG;IACH,GAAG,CAAC,EAAE,OAAO,CAAA;IACb;;OAEG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAA;CAC7B;AAmiBD,wBAAgB,eAAe,CAAC,EAAE,EAAE,UAAU,EAAE,IAAI,EAAE,IAAI,wDA6BzD;AAED,wBAAgB,SAAS,CAAC,IAAI,EAAE,IAAI,wDAEnC"}
|
package/src/transform.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.transform = exports.transformWithTs = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const typescript = tslib_1.__importStar(require("typescript"));
|
|
6
|
+
const interpolate_name_1 = require("./interpolate-name");
|
|
7
|
+
const icu_messageformat_parser_1 = require("@formatjs/icu-messageformat-parser");
|
|
8
|
+
const console_utils_1 = require("./console_utils");
|
|
9
|
+
const json_stable_stringify_1 = tslib_1.__importDefault(require("json-stable-stringify"));
|
|
10
|
+
const MESSAGE_DESC_KEYS = [
|
|
11
11
|
'id',
|
|
12
12
|
'defaultMessage',
|
|
13
13
|
'description',
|
|
@@ -25,7 +25,7 @@ function primitiveToTSNode(factory, v) {
|
|
|
25
25
|
}
|
|
26
26
|
function isValidIdentifier(k) {
|
|
27
27
|
try {
|
|
28
|
-
new Function(
|
|
28
|
+
new Function(`return {${k}:1}`);
|
|
29
29
|
return true;
|
|
30
30
|
}
|
|
31
31
|
catch (e) {
|
|
@@ -36,24 +36,18 @@ function objToTSNode(factory, obj) {
|
|
|
36
36
|
if (typeof obj === 'object' && !obj) {
|
|
37
37
|
return factory.createNull();
|
|
38
38
|
}
|
|
39
|
-
|
|
40
|
-
.filter(
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
(Array.isArray(v)
|
|
48
|
-
? factory.createArrayLiteralExpression(v
|
|
49
|
-
.filter(function (n) { return typeof n !== 'undefined'; })
|
|
50
|
-
.map(function (n) { return objToTSNode(factory, n); }))
|
|
51
|
-
: objToTSNode(factory, v)));
|
|
52
|
-
});
|
|
39
|
+
const props = Object.entries(obj)
|
|
40
|
+
.filter(([_, v]) => typeof v !== 'undefined')
|
|
41
|
+
.map(([k, v]) => factory.createPropertyAssignment(isValidIdentifier(k) ? k : factory.createStringLiteral(k), primitiveToTSNode(factory, v) ||
|
|
42
|
+
(Array.isArray(v)
|
|
43
|
+
? factory.createArrayLiteralExpression(v
|
|
44
|
+
.filter(n => typeof n !== 'undefined')
|
|
45
|
+
.map(n => objToTSNode(factory, n)))
|
|
46
|
+
: objToTSNode(factory, v))));
|
|
53
47
|
return factory.createObjectLiteralExpression(props);
|
|
54
48
|
}
|
|
55
49
|
function messageASTToTSNode(factory, ast) {
|
|
56
|
-
return factory.createArrayLiteralExpression(ast.map(
|
|
50
|
+
return factory.createArrayLiteralExpression(ast.map(el => objToTSNode(factory, el)));
|
|
57
51
|
}
|
|
58
52
|
function literalToObj(ts, n) {
|
|
59
53
|
if (ts.isNumericLiteral(n)) {
|
|
@@ -70,7 +64,7 @@ function literalToObj(ts, n) {
|
|
|
70
64
|
}
|
|
71
65
|
}
|
|
72
66
|
function objectLiteralExpressionToObj(ts, obj) {
|
|
73
|
-
return obj.properties.reduce(
|
|
67
|
+
return obj.properties.reduce((all, prop) => {
|
|
74
68
|
if (ts.isPropertyAssignment(prop) && prop.name) {
|
|
75
69
|
if (ts.isIdentifier(prop.name)) {
|
|
76
70
|
all[prop.name.escapedText.toString()] = literalToObj(ts, prop.initializer);
|
|
@@ -82,22 +76,23 @@ function objectLiteralExpressionToObj(ts, obj) {
|
|
|
82
76
|
return all;
|
|
83
77
|
}, {});
|
|
84
78
|
}
|
|
85
|
-
|
|
86
|
-
onMsgExtracted:
|
|
87
|
-
onMetaExtracted:
|
|
79
|
+
const DEFAULT_OPTS = {
|
|
80
|
+
onMsgExtracted: () => undefined,
|
|
81
|
+
onMetaExtracted: () => undefined,
|
|
88
82
|
};
|
|
89
83
|
function isMultipleMessageDecl(ts, node) {
|
|
90
84
|
return (ts.isIdentifier(node.expression) &&
|
|
91
85
|
node.expression.text === 'defineMessages');
|
|
92
86
|
}
|
|
93
87
|
function isSingularMessageDecl(ts, node, additionalComponentNames) {
|
|
94
|
-
|
|
88
|
+
const compNames = new Set([
|
|
95
89
|
'FormattedMessage',
|
|
96
90
|
'defineMessage',
|
|
97
91
|
'formatMessage',
|
|
98
|
-
'$formatMessage'
|
|
99
|
-
|
|
100
|
-
|
|
92
|
+
'$formatMessage',
|
|
93
|
+
...additionalComponentNames,
|
|
94
|
+
]);
|
|
95
|
+
let fnName = '';
|
|
101
96
|
if (ts.isCallExpression(node) && ts.isIdentifier(node.expression)) {
|
|
102
97
|
fnName = node.expression.text;
|
|
103
98
|
}
|
|
@@ -111,7 +106,7 @@ function isSingularMessageDecl(ts, node, additionalComponentNames) {
|
|
|
111
106
|
return compNames.has(fnName);
|
|
112
107
|
}
|
|
113
108
|
function evaluateStringConcat(ts, node) {
|
|
114
|
-
|
|
109
|
+
const { right, left } = node;
|
|
115
110
|
if (!ts.isStringLiteral(right)) {
|
|
116
111
|
return ['', false];
|
|
117
112
|
}
|
|
@@ -119,27 +114,26 @@ function evaluateStringConcat(ts, node) {
|
|
|
119
114
|
return [left.text + right.text, true];
|
|
120
115
|
}
|
|
121
116
|
if (ts.isBinaryExpression(left)) {
|
|
122
|
-
|
|
117
|
+
const [result, isStatic] = evaluateStringConcat(ts, left);
|
|
123
118
|
return [result + right.text, isStatic];
|
|
124
119
|
}
|
|
125
120
|
return ['', false];
|
|
126
121
|
}
|
|
127
|
-
function extractMessageDescriptor(ts, node,
|
|
128
|
-
|
|
129
|
-
var properties = undefined;
|
|
122
|
+
function extractMessageDescriptor(ts, node, { overrideIdFn, extractSourceLocation, preserveWhitespace }, sf) {
|
|
123
|
+
let properties = undefined;
|
|
130
124
|
if (ts.isObjectLiteralExpression(node)) {
|
|
131
125
|
properties = node.properties;
|
|
132
126
|
}
|
|
133
127
|
else if (ts.isJsxOpeningElement(node) || ts.isJsxSelfClosingElement(node)) {
|
|
134
128
|
properties = node.attributes.properties;
|
|
135
129
|
}
|
|
136
|
-
|
|
130
|
+
const msg = { id: '' };
|
|
137
131
|
if (!properties) {
|
|
138
132
|
return;
|
|
139
133
|
}
|
|
140
|
-
properties.forEach(
|
|
141
|
-
|
|
142
|
-
|
|
134
|
+
properties.forEach(prop => {
|
|
135
|
+
const { name } = prop;
|
|
136
|
+
const initializer = ts.isPropertyAssignment(prop) || ts.isJsxAttribute(prop)
|
|
143
137
|
? prop.initializer
|
|
144
138
|
: undefined;
|
|
145
139
|
if (name && ts.isIdentifier(name) && initializer) {
|
|
@@ -193,7 +187,7 @@ function extractMessageDescriptor(ts, node, _a, sf) {
|
|
|
193
187
|
}
|
|
194
188
|
// <FormattedMessage foo={`bar`} />
|
|
195
189
|
else if (ts.isNoSubstitutionTemplateLiteral(initializer.expression)) {
|
|
196
|
-
|
|
190
|
+
const { expression } = initializer;
|
|
197
191
|
switch (name.text) {
|
|
198
192
|
case 'id':
|
|
199
193
|
msg.id = expression.text;
|
|
@@ -208,8 +202,8 @@ function extractMessageDescriptor(ts, node, _a, sf) {
|
|
|
208
202
|
}
|
|
209
203
|
// <FormattedMessage foo={'bar' + 'baz'} />
|
|
210
204
|
else if (ts.isBinaryExpression(initializer.expression)) {
|
|
211
|
-
|
|
212
|
-
|
|
205
|
+
const { expression } = initializer;
|
|
206
|
+
const [result, isStatic] = evaluateStringConcat(ts, expression);
|
|
213
207
|
if (isStatic) {
|
|
214
208
|
switch (name.text) {
|
|
215
209
|
case 'id':
|
|
@@ -227,7 +221,7 @@ function extractMessageDescriptor(ts, node, _a, sf) {
|
|
|
227
221
|
}
|
|
228
222
|
// {defaultMessage: 'asd' + bar'}
|
|
229
223
|
else if (ts.isBinaryExpression(initializer)) {
|
|
230
|
-
|
|
224
|
+
const [result, isStatic] = evaluateStringConcat(ts, initializer);
|
|
231
225
|
if (isStatic) {
|
|
232
226
|
switch (name.text) {
|
|
233
227
|
case 'id':
|
|
@@ -262,9 +256,9 @@ function extractMessageDescriptor(ts, node, _a, sf) {
|
|
|
262
256
|
if (!msg.id) {
|
|
263
257
|
msg.id = (0, interpolate_name_1.interpolateName)({ resourcePath: sf.fileName }, overrideIdFn, {
|
|
264
258
|
content: msg.description
|
|
265
|
-
?
|
|
259
|
+
? `${msg.defaultMessage}#${typeof msg.description === 'string'
|
|
266
260
|
? msg.description
|
|
267
|
-
: (0, json_stable_stringify_1.default)(msg.description)
|
|
261
|
+
: (0, json_stable_stringify_1.default)(msg.description)}`
|
|
268
262
|
: msg.defaultMessage,
|
|
269
263
|
});
|
|
270
264
|
}
|
|
@@ -275,7 +269,12 @@ function extractMessageDescriptor(ts, node, _a, sf) {
|
|
|
275
269
|
}
|
|
276
270
|
}
|
|
277
271
|
if (extractSourceLocation) {
|
|
278
|
-
return
|
|
272
|
+
return {
|
|
273
|
+
...msg,
|
|
274
|
+
file: sf.fileName,
|
|
275
|
+
start: node.pos,
|
|
276
|
+
end: node.end,
|
|
277
|
+
};
|
|
279
278
|
}
|
|
280
279
|
return msg;
|
|
281
280
|
}
|
|
@@ -285,11 +284,12 @@ function extractMessageDescriptor(ts, node, _a, sf) {
|
|
|
285
284
|
* @param sf
|
|
286
285
|
*/
|
|
287
286
|
function isMemberMethodFormatMessageCall(ts, node, additionalFunctionNames) {
|
|
288
|
-
|
|
287
|
+
const fnNames = new Set([
|
|
289
288
|
'formatMessage',
|
|
290
|
-
'$formatMessage'
|
|
291
|
-
|
|
292
|
-
|
|
289
|
+
'$formatMessage',
|
|
290
|
+
...additionalFunctionNames,
|
|
291
|
+
]);
|
|
292
|
+
const method = node.expression;
|
|
293
293
|
// Handle foo.formatMessage()
|
|
294
294
|
if (ts.isPropertyAccessExpression(method)) {
|
|
295
295
|
return fnNames.has(method.name.text);
|
|
@@ -298,18 +298,18 @@ function isMemberMethodFormatMessageCall(ts, node, additionalFunctionNames) {
|
|
|
298
298
|
return ts.isIdentifier(method) && fnNames.has(method.text);
|
|
299
299
|
}
|
|
300
300
|
function extractMessageFromJsxComponent(ts, factory, node, opts, sf) {
|
|
301
|
-
|
|
301
|
+
const { onMsgExtracted } = opts;
|
|
302
302
|
if (!isSingularMessageDecl(ts, node, opts.additionalComponentNames || [])) {
|
|
303
303
|
return node;
|
|
304
304
|
}
|
|
305
|
-
|
|
305
|
+
const msg = extractMessageDescriptor(ts, node, opts, sf);
|
|
306
306
|
if (!msg) {
|
|
307
307
|
return node;
|
|
308
308
|
}
|
|
309
309
|
if (typeof onMsgExtracted === 'function') {
|
|
310
310
|
onMsgExtracted(sf.fileName, [msg]);
|
|
311
311
|
}
|
|
312
|
-
|
|
312
|
+
const newProps = generateNewProperties(ts, factory, node.attributes, {
|
|
313
313
|
defaultMessage: opts.removeDefaultMessage
|
|
314
314
|
? undefined
|
|
315
315
|
: msg.defaultMessage,
|
|
@@ -321,17 +321,17 @@ function extractMessageFromJsxComponent(ts, factory, node, opts, sf) {
|
|
|
321
321
|
return factory.updateJsxSelfClosingElement(node, node.tagName, node.typeArguments, factory.createJsxAttributes(newProps));
|
|
322
322
|
}
|
|
323
323
|
function setAttributesInObject(ts, factory, node, msg, ast) {
|
|
324
|
-
|
|
325
|
-
factory.createPropertyAssignment('id', factory.createStringLiteral(msg.id))
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
324
|
+
const newProps = [
|
|
325
|
+
factory.createPropertyAssignment('id', factory.createStringLiteral(msg.id)),
|
|
326
|
+
...(msg.defaultMessage
|
|
327
|
+
? [
|
|
328
|
+
factory.createPropertyAssignment('defaultMessage', ast
|
|
329
|
+
? messageASTToTSNode(factory, (0, icu_messageformat_parser_1.parse)(msg.defaultMessage))
|
|
330
|
+
: factory.createStringLiteral(msg.defaultMessage)),
|
|
331
|
+
]
|
|
332
|
+
: []),
|
|
333
|
+
];
|
|
334
|
+
for (const prop of node.properties) {
|
|
335
335
|
if (ts.isPropertyAssignment(prop) &&
|
|
336
336
|
ts.isIdentifier(prop.name) &&
|
|
337
337
|
MESSAGE_DESC_KEYS.includes(prop.name.text)) {
|
|
@@ -344,17 +344,17 @@ function setAttributesInObject(ts, factory, node, msg, ast) {
|
|
|
344
344
|
return factory.createObjectLiteralExpression(factory.createNodeArray(newProps));
|
|
345
345
|
}
|
|
346
346
|
function generateNewProperties(ts, factory, node, msg, ast) {
|
|
347
|
-
|
|
348
|
-
factory.createJsxAttribute(factory.createIdentifier('id'), factory.createStringLiteral(msg.id))
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
347
|
+
const newProps = [
|
|
348
|
+
factory.createJsxAttribute(factory.createIdentifier('id'), factory.createStringLiteral(msg.id)),
|
|
349
|
+
...(msg.defaultMessage
|
|
350
|
+
? [
|
|
351
|
+
factory.createJsxAttribute(factory.createIdentifier('defaultMessage'), ast
|
|
352
|
+
? factory.createJsxExpression(undefined, messageASTToTSNode(factory, (0, icu_messageformat_parser_1.parse)(msg.defaultMessage)))
|
|
353
|
+
: factory.createStringLiteral(msg.defaultMessage)),
|
|
354
|
+
]
|
|
355
|
+
: []),
|
|
356
|
+
];
|
|
357
|
+
for (const prop of node.properties) {
|
|
358
358
|
if (ts.isJsxAttribute(prop) &&
|
|
359
359
|
ts.isIdentifier(prop.name) &&
|
|
360
360
|
MESSAGE_DESC_KEYS.includes(prop.name.text)) {
|
|
@@ -367,10 +367,10 @@ function generateNewProperties(ts, factory, node, msg, ast) {
|
|
|
367
367
|
return newProps;
|
|
368
368
|
}
|
|
369
369
|
function extractMessagesFromCallExpression(ts, factory, node, opts, sf) {
|
|
370
|
-
|
|
370
|
+
const { onMsgExtracted, additionalFunctionNames } = opts;
|
|
371
371
|
if (isMultipleMessageDecl(ts, node)) {
|
|
372
|
-
|
|
373
|
-
|
|
372
|
+
const [arg, ...restArgs] = node.arguments;
|
|
373
|
+
let descriptorsObj;
|
|
374
374
|
if (ts.isObjectLiteralExpression(arg)) {
|
|
375
375
|
descriptorsObj = arg;
|
|
376
376
|
}
|
|
@@ -379,24 +379,20 @@ function extractMessagesFromCallExpression(ts, factory, node, opts, sf) {
|
|
|
379
379
|
descriptorsObj = arg.expression;
|
|
380
380
|
}
|
|
381
381
|
if (descriptorsObj) {
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
.filter(
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
.
|
|
388
|
-
|
|
389
|
-
extractMessageDescriptor(ts, prop.initializer, opts, sf);
|
|
390
|
-
})
|
|
391
|
-
.filter(function (msg) { return !!msg; });
|
|
392
|
-
if (!msgs_1.length) {
|
|
382
|
+
const properties = descriptorsObj.properties;
|
|
383
|
+
const msgs = properties
|
|
384
|
+
.filter((prop) => ts.isPropertyAssignment(prop))
|
|
385
|
+
.map(prop => ts.isObjectLiteralExpression(prop.initializer) &&
|
|
386
|
+
extractMessageDescriptor(ts, prop.initializer, opts, sf))
|
|
387
|
+
.filter((msg) => !!msg);
|
|
388
|
+
if (!msgs.length) {
|
|
393
389
|
return node;
|
|
394
390
|
}
|
|
395
|
-
(0, console_utils_1.debug)('Multiple messages extracted from "%s": %s', sf.fileName,
|
|
391
|
+
(0, console_utils_1.debug)('Multiple messages extracted from "%s": %s', sf.fileName, msgs);
|
|
396
392
|
if (typeof onMsgExtracted === 'function') {
|
|
397
|
-
onMsgExtracted(sf.fileName,
|
|
393
|
+
onMsgExtracted(sf.fileName, msgs);
|
|
398
394
|
}
|
|
399
|
-
|
|
395
|
+
const clonedProperties = factory.createNodeArray(properties.map((prop, i) => {
|
|
400
396
|
if (!ts.isPropertyAssignment(prop) ||
|
|
401
397
|
!ts.isObjectLiteralExpression(prop.initializer)) {
|
|
402
398
|
return prop;
|
|
@@ -404,19 +400,19 @@ function extractMessagesFromCallExpression(ts, factory, node, opts, sf) {
|
|
|
404
400
|
return factory.createPropertyAssignment(prop.name, setAttributesInObject(ts, factory, prop.initializer, {
|
|
405
401
|
defaultMessage: opts.removeDefaultMessage
|
|
406
402
|
? undefined
|
|
407
|
-
:
|
|
408
|
-
id:
|
|
403
|
+
: msgs[i].defaultMessage,
|
|
404
|
+
id: msgs[i] ? msgs[i].id : '',
|
|
409
405
|
}, opts.ast));
|
|
410
406
|
}));
|
|
411
|
-
|
|
412
|
-
return factory.updateCallExpression(node, node.expression, node.typeArguments,
|
|
407
|
+
const clonedDescriptorsObj = factory.createObjectLiteralExpression(clonedProperties);
|
|
408
|
+
return factory.updateCallExpression(node, node.expression, node.typeArguments, [clonedDescriptorsObj, ...restArgs]);
|
|
413
409
|
}
|
|
414
410
|
}
|
|
415
411
|
else if (isSingularMessageDecl(ts, node, opts.additionalComponentNames || []) ||
|
|
416
412
|
isMemberMethodFormatMessageCall(ts, node, additionalFunctionNames || [])) {
|
|
417
|
-
|
|
413
|
+
const [descriptorsObj, ...restArgs] = node.arguments;
|
|
418
414
|
if (ts.isObjectLiteralExpression(descriptorsObj)) {
|
|
419
|
-
|
|
415
|
+
const msg = extractMessageDescriptor(ts, descriptorsObj, opts, sf);
|
|
420
416
|
if (!msg) {
|
|
421
417
|
return node;
|
|
422
418
|
}
|
|
@@ -424,22 +420,23 @@ function extractMessagesFromCallExpression(ts, factory, node, opts, sf) {
|
|
|
424
420
|
if (typeof onMsgExtracted === 'function') {
|
|
425
421
|
onMsgExtracted(sf.fileName, [msg]);
|
|
426
422
|
}
|
|
427
|
-
return factory.updateCallExpression(node, node.expression, node.typeArguments,
|
|
423
|
+
return factory.updateCallExpression(node, node.expression, node.typeArguments, [
|
|
428
424
|
setAttributesInObject(ts, factory, descriptorsObj, {
|
|
429
425
|
defaultMessage: opts.removeDefaultMessage
|
|
430
426
|
? undefined
|
|
431
427
|
: msg.defaultMessage,
|
|
432
428
|
id: msg.id,
|
|
433
|
-
}, opts.ast)
|
|
434
|
-
|
|
429
|
+
}, opts.ast),
|
|
430
|
+
...restArgs,
|
|
431
|
+
]);
|
|
435
432
|
}
|
|
436
433
|
}
|
|
437
434
|
return node;
|
|
438
435
|
}
|
|
439
|
-
|
|
436
|
+
const PRAGMA_REGEX = /^\/\/ @([^\s]*) (.*)$/m;
|
|
440
437
|
function getVisitor(ts, ctx, sf, opts) {
|
|
441
|
-
|
|
442
|
-
|
|
438
|
+
const visitor = (node) => {
|
|
439
|
+
const newNode = ts.isCallExpression(node)
|
|
443
440
|
? extractMessagesFromCallExpression(ts, ctx.factory, node, opts, sf)
|
|
444
441
|
: ts.isJsxOpeningElement(node) || ts.isJsxSelfClosingElement(node)
|
|
445
442
|
? extractMessageFromJsxComponent(ts, ctx.factory, node, opts, sf)
|
|
@@ -449,20 +446,19 @@ function getVisitor(ts, ctx, sf, opts) {
|
|
|
449
446
|
return visitor;
|
|
450
447
|
}
|
|
451
448
|
function transformWithTs(ts, opts) {
|
|
452
|
-
opts =
|
|
449
|
+
opts = { ...DEFAULT_OPTS, ...opts };
|
|
453
450
|
(0, console_utils_1.debug)('Transforming options', opts);
|
|
454
|
-
|
|
455
|
-
return
|
|
456
|
-
|
|
451
|
+
const transformFn = ctx => {
|
|
452
|
+
return (sf) => {
|
|
453
|
+
const pragmaResult = PRAGMA_REGEX.exec(sf.text);
|
|
457
454
|
if (pragmaResult) {
|
|
458
455
|
(0, console_utils_1.debug)('Pragma found', pragmaResult);
|
|
459
|
-
|
|
456
|
+
const [, pragma, kvString] = pragmaResult;
|
|
460
457
|
if (pragma === opts.pragma) {
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
for (
|
|
464
|
-
|
|
465
|
-
var _a = kv.split(':'), k = _a[0], v = _a[1];
|
|
458
|
+
const kvs = kvString.split(' ');
|
|
459
|
+
const result = {};
|
|
460
|
+
for (const kv of kvs) {
|
|
461
|
+
const [k, v] = kv.split(':');
|
|
466
462
|
result[k] = v;
|
|
467
463
|
}
|
|
468
464
|
(0, console_utils_1.debug)('Pragma extracted', result);
|
package/src/types.d.ts
CHANGED
|
File without changes
|
package/src/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["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"}
|
package/src/types.js
CHANGED
|
File without changes
|
package/ts-jest-integration.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Opts } from '.';
|
|
2
|
-
import type { TsCompilerInstance } from 'ts-jest
|
|
2
|
+
import type { TsCompilerInstance } from 'ts-jest';
|
|
3
3
|
export declare const name = "@formatjs/ts-transformer";
|
|
4
4
|
export declare const version = "2.10.1";
|
|
5
5
|
export declare function factory(compilerInstance: TsCompilerInstance, opts: Opts): import("typescript").TransformerFactory<import("typescript").SourceFile>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ts-jest-integration.d.ts","sourceRoot":"","sources":["
|
|
1
|
+
{"version":3,"file":"ts-jest-integration.d.ts","sourceRoot":"","sources":["ts-jest-integration.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,IAAI,EAAC,MAAM,GAAG,CAAA;AACvC,OAAO,KAAK,EAAC,kBAAkB,EAAC,MAAM,SAAS,CAAA;AAE/C,eAAO,MAAM,IAAI,6BAA6B,CAAA;AAC9C,eAAO,MAAM,OAAO,WAAW,CAAA;AAE/B,wBAAgB,OAAO,CAAC,gBAAgB,EAAE,kBAAkB,EAAE,IAAI,EAAE,IAAI,4EAEvE"}
|
package/ts-jest-integration.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.factory = exports.version = exports.name = void 0;
|
|
4
|
-
|
|
4
|
+
const _1 = require(".");
|
|
5
5
|
exports.name = '@formatjs/ts-transformer';
|
|
6
6
|
exports.version = '2.10.1';
|
|
7
7
|
function factory(compilerInstance, opts) {
|