babel-plugin-formatjs 10.3.25 → 10.3.28
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/global.d.ts +0 -0
- package/index.d.ts +0 -0
- package/index.d.ts.map +1 -1
- package/index.js +20 -25
- package/package.json +5 -4
- package/types.d.ts +0 -0
- package/types.d.ts.map +1 -1
- package/types.js +0 -0
- package/utils.d.ts +0 -0
- package/utils.d.ts.map +1 -1
- package/utils.js +26 -29
- package/visitors/call-expression.d.ts +0 -0
- package/visitors/call-expression.d.ts.map +1 -1
- package/visitors/call-expression.js +35 -38
- package/visitors/jsx-opening-element.d.ts +0 -0
- package/visitors/jsx-opening-element.d.ts.map +1 -1
- package/visitors/jsx-opening-element.js +20 -22
package/LICENSE.md
CHANGED
|
File without changes
|
package/README.md
CHANGED
|
File without changes
|
package/global.d.ts
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":"AACA,OAAO,EAAC,SAAS,EAAE,UAAU,EAAC,MAAM,aAAa,CAAA;AAEjD,OAAO,EAAC,0BAA0B,EAAE,OAAO,EAAQ,MAAM,SAAS,CAAA;AAIlE,oBAAY,gBAAgB,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,IAAI;IACzD,QAAQ,EAAE,0BAA0B,EAAE,CAAA;IACtC,IAAI,EAAE,CAAC,CAAA;CACR,CAAA;AAED,eAAO,MAAM,gCAAgC,kCAAkC,CAAA;;AAG/E,wBAwEE"}
|
package/index.js
CHANGED
|
@@ -1,48 +1,44 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DEFAULT_ID_INTERPOLATION_PATTERN = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const helper_plugin_utils_1 = require("@babel/helper-plugin-utils");
|
|
6
|
+
const plugin_syntax_jsx_1 = tslib_1.__importDefault(require("@babel/plugin-syntax-jsx"));
|
|
7
|
+
const jsx_opening_element_1 = require("./visitors/jsx-opening-element");
|
|
8
|
+
const call_expression_1 = require("./visitors/call-expression");
|
|
9
9
|
exports.DEFAULT_ID_INTERPOLATION_PATTERN = '[sha512:contenthash:base64:6]';
|
|
10
10
|
// @ts-expect-error PluginPass doesn't allow custom state but it actually does
|
|
11
|
-
exports.default = (0, helper_plugin_utils_1.declare)(
|
|
11
|
+
exports.default = (0, helper_plugin_utils_1.declare)((api, options) => {
|
|
12
12
|
api.assertVersion(7);
|
|
13
13
|
if (!options.idInterpolationPattern) {
|
|
14
14
|
options.idInterpolationPattern = exports.DEFAULT_ID_INTERPOLATION_PATTERN;
|
|
15
15
|
}
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
const { pragma } = options;
|
|
17
|
+
const componentNames = new Set(options.additionalComponentNames);
|
|
18
18
|
componentNames.add('FormattedMessage');
|
|
19
|
-
|
|
19
|
+
const functionNames = new Set(options.additionalFunctionNames);
|
|
20
20
|
functionNames.add('formatMessage');
|
|
21
21
|
// Vue
|
|
22
22
|
functionNames.add('$formatMessage');
|
|
23
23
|
return {
|
|
24
24
|
inherits: plugin_syntax_jsx_1.default,
|
|
25
|
-
pre
|
|
25
|
+
pre() {
|
|
26
26
|
this.componentNames = Array.from(componentNames);
|
|
27
27
|
this.functionNames = Array.from(functionNames);
|
|
28
28
|
},
|
|
29
29
|
visitor: {
|
|
30
30
|
Program: {
|
|
31
|
-
enter
|
|
32
|
-
var _this = this;
|
|
31
|
+
enter(path) {
|
|
33
32
|
this.messages = [];
|
|
34
33
|
this.meta = {};
|
|
35
34
|
if (!pragma) {
|
|
36
35
|
return;
|
|
37
36
|
}
|
|
38
|
-
for (
|
|
39
|
-
var leadingComments = _a[_i].leadingComments;
|
|
37
|
+
for (const { leadingComments } of path.node.body) {
|
|
40
38
|
if (!leadingComments) {
|
|
41
39
|
continue;
|
|
42
40
|
}
|
|
43
|
-
|
|
44
|
-
return c.value.includes(pragma);
|
|
45
|
-
});
|
|
41
|
+
const pragmaLineNode = leadingComments.find(c => c.value.includes(pragma));
|
|
46
42
|
if (!pragmaLineNode) {
|
|
47
43
|
continue;
|
|
48
44
|
}
|
|
@@ -50,19 +46,18 @@ exports.default = (0, helper_plugin_utils_1.declare)(function (api, options) {
|
|
|
50
46
|
.split(pragma)[1]
|
|
51
47
|
.trim()
|
|
52
48
|
.split(/\s+/g)
|
|
53
|
-
.forEach(
|
|
54
|
-
|
|
55
|
-
|
|
49
|
+
.forEach(kv => {
|
|
50
|
+
const [k, v] = kv.split(':');
|
|
51
|
+
this.meta[k] = v;
|
|
56
52
|
});
|
|
57
53
|
}
|
|
58
54
|
},
|
|
59
|
-
exit:
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
if (typeof (opts === null || opts === void 0 ? void 0 : opts.onMetaExtracted) === 'function') {
|
|
55
|
+
exit(_, { opts: _opts, file: { opts: { filename }, }, }) {
|
|
56
|
+
const opts = _opts;
|
|
57
|
+
if (typeof opts?.onMetaExtracted === 'function') {
|
|
63
58
|
opts.onMetaExtracted(filename || '', this.meta);
|
|
64
59
|
}
|
|
65
|
-
if (typeof
|
|
60
|
+
if (typeof opts?.onMsgExtracted === 'function') {
|
|
66
61
|
opts.onMsgExtracted(filename || '', this.messages);
|
|
67
62
|
}
|
|
68
63
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "babel-plugin-formatjs",
|
|
3
|
-
"version": "10.3.
|
|
3
|
+
"version": "10.3.28",
|
|
4
4
|
"description": "Extracts string messages for translation from modules that use formatjs.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -16,10 +16,11 @@
|
|
|
16
16
|
"@babel/plugin-syntax-jsx": "7",
|
|
17
17
|
"@babel/traverse": "7",
|
|
18
18
|
"@babel/types": "^7.12.11",
|
|
19
|
-
"@formatjs/icu-messageformat-parser": "2.1.
|
|
20
|
-
"@formatjs/ts-transformer": "3.
|
|
19
|
+
"@formatjs/icu-messageformat-parser": "2.1.7",
|
|
20
|
+
"@formatjs/ts-transformer": "3.10.0",
|
|
21
21
|
"@types/babel__core": "^7.1.7",
|
|
22
22
|
"@types/babel__helper-plugin-utils": "^7.10.0",
|
|
23
|
+
"@types/babel__traverse": "^7.1.7",
|
|
23
24
|
"tslib": "2.4.0"
|
|
24
25
|
},
|
|
25
26
|
"keywords": [
|
|
@@ -31,4 +32,4 @@
|
|
|
31
32
|
},
|
|
32
33
|
"homepage": "https://github.com/formatjs/formatjs#readme",
|
|
33
34
|
"gitHead": "8b0baec8eda5002715cf893274fe59782fc2d371"
|
|
34
|
-
}
|
|
35
|
+
}
|
package/types.d.ts
CHANGED
|
File without changes
|
package/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,OAAO,EAAC,QAAQ,EAAC,MAAM,aAAa,CAAA;AACpC,OAAO,EACL,sBAAsB,EACtB,cAAc,EACd,aAAa,EACd,MAAM,cAAc,CAAA;AAErB,MAAM,WAAW,iBAAiB;IAChC,EAAE,EAAE,MAAM,CAAA;IACV,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,WAAW,CAAC,EAAE,MAAM,CAAA;CACrB;AAED,MAAM,WAAW,KAAK;IACpB,QAAQ,EAAE,0BAA0B,EAAE,CAAA;IACtC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC5B,cAAc,EAAE,MAAM,EAAE,CAAA;IACxB,aAAa,EAAE,MAAM,EAAE,CAAA;CACxB;AAED,oBAAY,0BAA0B,GAAG,iBAAiB,GACxD,OAAO,CAAC,cAAc,CAAC,GAAG;IAAC,IAAI,CAAC,EAAE,MAAM,CAAA;CAAC,CAAA;AAE3C,oBAAY,qBAAqB,GAAG,MAAM,CACxC,MAAM,iBAAiB,EACvB,QAAQ,CAAC,aAAa,CAAC,GAAG,QAAQ,CAAC,sBAAsB,CAAC,GAAG,SAAS,CACvE,CAAA;AAED,MAAM,WAAW,OAAO;IACtB,YAAY,CAAC,EAAE,CACb,EAAE,CAAC,EAAE,MAAM,EACX,cAAc,CAAC,EAAE,MAAM,EACvB,WAAW,CAAC,EAAE,MAAM,EACpB,QAAQ,CAAC,EAAE,MAAM,KACd,MAAM,CAAA;IACX,cAAc,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,iBAAiB,EAAE,KAAK,IAAI,CAAA;IACtE,eAAe,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,KAAK,IAAI,CAAA;IAC1E,sBAAsB,CAAC,EAAE,MAAM,CAAA;IAC/B,oBAAoB,CAAC,EAAE,OAAO,CAAA;IAC9B,wBAAwB,CAAC,EAAE,MAAM,EAAE,CAAA;IACnC,uBAAuB,CAAC,EAAE,MAAM,EAAE,CAAA;IAClC,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,qBAAqB,CAAC,EAAE,OAAO,CAAA;IAC/B,GAAG,CAAC,EAAE,OAAO,CAAA;IACb,kBAAkB,CAAC,EAAE,OAAO,CAAA;CAC7B"}
|
package/types.js
CHANGED
|
File without changes
|
package/utils.d.ts
CHANGED
|
File without changes
|
package/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,cAAc,CAAA;AAIjC,OAAO,EACL,OAAO,EACP,0BAA0B,EAC1B,iBAAiB,EACjB,qBAAqB,EACtB,MAAM,SAAS,CAAA;AAChB,OAAO,EAAC,QAAQ,EAAC,MAAM,aAAa,CAAA;AAmBpC,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,QAAQ,CAAC,GAAG,CAAC,UAM1D;AA0BD,wBAAgB,uBAAuB,CACrC,SAAS,EAAE;IACT,QAAQ,CAAC,CAAC,CAAC,aAAa,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,UAAU,CAAC;IAClD,QAAQ,CAAC,CAAC,CAAC,aAAa,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,sBAAsB,CAAC;CAC/D,EAAE,GACF,qBAAqB,CAmBvB;AAED,wBAAgB,yBAAyB,CACvC,cAAc,EAAE,qBAAqB,EACrC,WAAW,qBAAQ,EACnB,QAAQ,EAAE,MAAM,GAAG,SAAS,EAC5B,sBAAsB,CAAC,EAAE,MAAM,EAC/B,YAAY,CAAC,EAAE,OAAO,CAAC,cAAc,CAAC,EACtC,kBAAkB,CAAC,EAAE,OAAO,CAAC,oBAAoB,CAAC,qBAqCnD;AA6CD;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,QAAQ,CAAC,GAAG,CAAC,QAEjD;AACD;;;GAGG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,QAAQ,CAAC,GAAG,CAAC,WAE/C;AAED;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAC1B,EAAC,EAAE,EAAE,WAAW,EAAE,cAAc,EAAC,EAAE,iBAAiB,EACpD,IAAI,EAAE,QAAQ,CAAC,GAAG,CAAC,EACnB,EAAC,qBAAqB,EAAC,EAAE,OAAO,EAEhC,QAAQ,EAAE,MAAM,GAAG,SAAS,EAC5B,QAAQ,EAAE,0BAA0B,EAAE,QAgBvC"}
|
package/utils.js
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.storeMessage = exports.wasExtracted = exports.tagAsExtracted = exports.evaluateMessageDescriptor = exports.createMessageDescriptor = exports.getMessageDescriptorKey = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var DESCRIPTOR_PROPS = new Set([
|
|
4
|
+
const icu_messageformat_parser_1 = require("@formatjs/icu-messageformat-parser");
|
|
5
|
+
const ts_transformer_1 = require("@formatjs/ts-transformer");
|
|
6
|
+
const DESCRIPTOR_PROPS = new Set([
|
|
8
7
|
'id',
|
|
9
8
|
'description',
|
|
10
9
|
'defaultMessage',
|
|
11
10
|
]);
|
|
12
11
|
function evaluatePath(path) {
|
|
13
|
-
|
|
12
|
+
const evaluated = path.evaluate();
|
|
14
13
|
if (evaluated.confident) {
|
|
15
14
|
return evaluated.value;
|
|
16
15
|
}
|
|
@@ -35,13 +34,12 @@ function getMessageDescriptorValue(path, isMessageNode) {
|
|
|
35
34
|
path = path.get('expression');
|
|
36
35
|
}
|
|
37
36
|
// Always trim the Message Descriptor values.
|
|
38
|
-
|
|
37
|
+
const descriptorValue = evaluatePath(path);
|
|
39
38
|
return descriptorValue;
|
|
40
39
|
}
|
|
41
40
|
function createMessageDescriptor(propPaths) {
|
|
42
|
-
return propPaths.reduce(
|
|
43
|
-
|
|
44
|
-
var key = getMessageDescriptorKey(keyPath);
|
|
41
|
+
return propPaths.reduce((hash, [keyPath, valuePath]) => {
|
|
42
|
+
const key = getMessageDescriptorKey(keyPath);
|
|
45
43
|
if (DESCRIPTOR_PROPS.has(key)) {
|
|
46
44
|
hash[key] = valuePath;
|
|
47
45
|
}
|
|
@@ -53,25 +51,24 @@ function createMessageDescriptor(propPaths) {
|
|
|
53
51
|
});
|
|
54
52
|
}
|
|
55
53
|
exports.createMessageDescriptor = createMessageDescriptor;
|
|
56
|
-
function evaluateMessageDescriptor(descriptorPath, isJSXSource, filename, idInterpolationPattern, overrideIdFn, preserveWhitespace) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
isJSXSource: isJSXSource,
|
|
54
|
+
function evaluateMessageDescriptor(descriptorPath, isJSXSource = false, filename, idInterpolationPattern, overrideIdFn, preserveWhitespace) {
|
|
55
|
+
let id = getMessageDescriptorValue(descriptorPath.id);
|
|
56
|
+
const defaultMessage = getICUMessageValue(descriptorPath.defaultMessage, {
|
|
57
|
+
isJSXSource,
|
|
61
58
|
}, preserveWhitespace);
|
|
62
|
-
|
|
59
|
+
const description = getMessageDescriptorValue(descriptorPath.description);
|
|
63
60
|
if (overrideIdFn) {
|
|
64
61
|
id = overrideIdFn(id, defaultMessage, description, filename);
|
|
65
62
|
}
|
|
66
63
|
else if (!id && idInterpolationPattern && defaultMessage) {
|
|
67
64
|
id = (0, ts_transformer_1.interpolateName)({ resourcePath: filename }, idInterpolationPattern, {
|
|
68
65
|
content: description
|
|
69
|
-
?
|
|
66
|
+
? `${defaultMessage}#${description}`
|
|
70
67
|
: defaultMessage,
|
|
71
68
|
});
|
|
72
69
|
}
|
|
73
|
-
|
|
74
|
-
id
|
|
70
|
+
const descriptor = {
|
|
71
|
+
id,
|
|
75
72
|
};
|
|
76
73
|
if (description) {
|
|
77
74
|
descriptor.description = description;
|
|
@@ -82,12 +79,11 @@ function evaluateMessageDescriptor(descriptorPath, isJSXSource, filename, idInte
|
|
|
82
79
|
return descriptor;
|
|
83
80
|
}
|
|
84
81
|
exports.evaluateMessageDescriptor = evaluateMessageDescriptor;
|
|
85
|
-
function getICUMessageValue(messagePath,
|
|
86
|
-
var _b = _a === void 0 ? {} : _a, _c = _b.isJSXSource, isJSXSource = _c === void 0 ? false : _c;
|
|
82
|
+
function getICUMessageValue(messagePath, { isJSXSource = false } = {}, preserveWhitespace) {
|
|
87
83
|
if (!messagePath) {
|
|
88
84
|
return '';
|
|
89
85
|
}
|
|
90
|
-
|
|
86
|
+
let message = getMessageDescriptorValue(messagePath, true);
|
|
91
87
|
if (!preserveWhitespace) {
|
|
92
88
|
message = message.trim().replace(/\s+/gm, ' ');
|
|
93
89
|
}
|
|
@@ -106,11 +102,11 @@ function getICUMessageValue(messagePath, _a, preserveWhitespace) {
|
|
|
106
102
|
}
|
|
107
103
|
throw messagePath.buildCodeFrameError('[React Intl] Message failed to parse. ' +
|
|
108
104
|
'See: https://formatjs.io/docs/core-concepts/icu-syntax' +
|
|
109
|
-
|
|
105
|
+
`\n${parseError}`);
|
|
110
106
|
}
|
|
111
107
|
return message;
|
|
112
108
|
}
|
|
113
|
-
|
|
109
|
+
const EXTRACTED = Symbol('FormatJSExtracted');
|
|
114
110
|
/**
|
|
115
111
|
* Tag a node as extracted
|
|
116
112
|
* Store this in the node itself so that multiple passes work. Specifically
|
|
@@ -140,16 +136,17 @@ exports.wasExtracted = wasExtracted;
|
|
|
140
136
|
* @param filename
|
|
141
137
|
* @param messages
|
|
142
138
|
*/
|
|
143
|
-
function storeMessage(
|
|
144
|
-
var id = _a.id, description = _a.description, defaultMessage = _a.defaultMessage;
|
|
145
|
-
var extractSourceLocation = _b.extractSourceLocation;
|
|
139
|
+
function storeMessage({ id, description, defaultMessage }, path, { extractSourceLocation }, filename, messages) {
|
|
146
140
|
if (!id && !defaultMessage) {
|
|
147
141
|
throw path.buildCodeFrameError('[React Intl] Message Descriptors require an `id` or `defaultMessage`.');
|
|
148
142
|
}
|
|
149
|
-
|
|
143
|
+
let loc = {};
|
|
150
144
|
if (extractSourceLocation) {
|
|
151
|
-
loc =
|
|
145
|
+
loc = {
|
|
146
|
+
file: filename,
|
|
147
|
+
...path.node.loc,
|
|
148
|
+
};
|
|
152
149
|
}
|
|
153
|
-
messages.push(
|
|
150
|
+
messages.push({ id, description, defaultMessage, ...loc });
|
|
154
151
|
}
|
|
155
152
|
exports.storeMessage = storeMessage;
|
|
File without changes
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"call-expression.d.ts","sourceRoot":"","sources":["
|
|
1
|
+
{"version":3,"file":"call-expression.d.ts","sourceRoot":"","sources":["call-expression.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,UAAU,EAAC,MAAM,aAAa,CAAA;AAChD,OAAO,KAAK,CAAC,MAAM,cAAc,CAAA;AACjC,OAAO,EAAU,KAAK,EAAC,MAAM,UAAU,CAAA;AACvC,OAAO,EAAC,iBAAiB,EAAC,MAAM,iBAAiB,CAAA;AAoDjD,eAAO,MAAM,OAAO,EAAE,iBAAiB,CAAC,UAAU,GAAG,KAAK,EAAE,CAAC,CAAC,cAAc,CAwJzE,CAAA"}
|
|
@@ -1,27 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.visitor = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const t = tslib_1.__importStar(require("@babel/types"));
|
|
6
|
+
const utils_1 = require("../utils");
|
|
7
|
+
const icu_messageformat_parser_1 = require("@formatjs/icu-messageformat-parser");
|
|
8
8
|
function assertObjectExpression(path, callee) {
|
|
9
9
|
if (!path || !path.isObjectExpression()) {
|
|
10
|
-
throw path.buildCodeFrameError(
|
|
10
|
+
throw path.buildCodeFrameError(`[React Intl] \`${callee.get('property').node.name}()\` must be called with an object expression with values that are React Intl Message Descriptors, also defined as object expressions.`);
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
13
|
function isFormatMessageCall(callee, functionNames) {
|
|
14
|
-
if (functionNames.find(
|
|
14
|
+
if (functionNames.find(name => callee.isIdentifier({ name }))) {
|
|
15
15
|
return true;
|
|
16
16
|
}
|
|
17
17
|
if (callee.isMemberExpression()) {
|
|
18
|
-
|
|
19
|
-
return !!functionNames.find(
|
|
18
|
+
const property = callee.get('property');
|
|
19
|
+
return !!functionNames.find(name => property.isIdentifier({ name }));
|
|
20
20
|
}
|
|
21
21
|
return false;
|
|
22
22
|
}
|
|
23
23
|
function getMessagesObjectFromExpression(nodePath) {
|
|
24
|
-
|
|
24
|
+
let currentPath = nodePath;
|
|
25
25
|
while (t.isTSAsExpression(currentPath.node) ||
|
|
26
26
|
t.isTSTypeAssertion(currentPath.node) ||
|
|
27
27
|
t.isTypeCastExpression(currentPath.node)) {
|
|
@@ -29,41 +29,37 @@ function getMessagesObjectFromExpression(nodePath) {
|
|
|
29
29
|
}
|
|
30
30
|
return currentPath;
|
|
31
31
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
var _b = opts, overrideIdFn = _b.overrideIdFn, idInterpolationPattern = _b.idInterpolationPattern, removeDefaultMessage = _b.removeDefaultMessage, ast = _b.ast, preserveWhitespace = _b.preserveWhitespace;
|
|
32
|
+
const visitor = function (path, { opts, file: { opts: { filename }, }, }) {
|
|
33
|
+
const { overrideIdFn, idInterpolationPattern, removeDefaultMessage, ast, preserveWhitespace, } = opts;
|
|
35
34
|
if ((0, utils_1.wasExtracted)(path)) {
|
|
36
35
|
return;
|
|
37
36
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
37
|
+
const { messages, functionNames } = this;
|
|
38
|
+
const callee = path.get('callee');
|
|
39
|
+
const args = path.get('arguments');
|
|
41
40
|
/**
|
|
42
41
|
* Process MessageDescriptor
|
|
43
42
|
* @param messageDescriptor Message Descriptor
|
|
44
43
|
*/
|
|
45
44
|
function processMessageObject(messageDescriptor) {
|
|
46
|
-
var _a, _b;
|
|
47
45
|
assertObjectExpression(messageDescriptor, callee);
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
return [prop.get('key'), prop.get('value')];
|
|
51
|
-
}));
|
|
46
|
+
const properties = messageDescriptor.get('properties');
|
|
47
|
+
const descriptorPath = (0, utils_1.createMessageDescriptor)(properties.map(prop => [prop.get('key'), prop.get('value')]));
|
|
52
48
|
// If the message is already compiled, don't re-compile it
|
|
53
|
-
if (
|
|
49
|
+
if (descriptorPath.defaultMessage?.isArrayExpression()) {
|
|
54
50
|
return;
|
|
55
51
|
}
|
|
56
52
|
// Evaluate the Message Descriptor values, then store it.
|
|
57
|
-
|
|
53
|
+
const descriptor = (0, utils_1.evaluateMessageDescriptor)(descriptorPath, false, filename || undefined, idInterpolationPattern, overrideIdFn, preserveWhitespace);
|
|
58
54
|
(0, utils_1.storeMessage)(descriptor, messageDescriptor, opts, filename || undefined, messages);
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
55
|
+
const firstProp = properties[0];
|
|
56
|
+
const defaultMessageProp = properties.find(prop => {
|
|
57
|
+
const keyProp = prop.get('key');
|
|
62
58
|
return (keyProp.isIdentifier({ name: 'defaultMessage' }) ||
|
|
63
59
|
keyProp.isStringLiteral({ value: 'defaultMessage' }));
|
|
64
60
|
});
|
|
65
|
-
|
|
66
|
-
|
|
61
|
+
const idProp = properties.find(prop => {
|
|
62
|
+
const keyProp = prop.get('key');
|
|
67
63
|
return (keyProp.isIdentifier({ name: 'id' }) ||
|
|
68
64
|
keyProp.isStringLiteral({ value: 'id' }));
|
|
69
65
|
});
|
|
@@ -75,19 +71,20 @@ var visitor = function (path, _a) {
|
|
|
75
71
|
firstProp.insertBefore(t.objectProperty(t.identifier('id'), t.stringLiteral(descriptor.id)));
|
|
76
72
|
}
|
|
77
73
|
// Remove description
|
|
78
|
-
|
|
79
|
-
.find(
|
|
80
|
-
|
|
74
|
+
properties
|
|
75
|
+
.find(prop => {
|
|
76
|
+
const keyProp = prop.get('key');
|
|
81
77
|
return (keyProp.isIdentifier({ name: 'description' }) ||
|
|
82
78
|
keyProp.isStringLiteral({ value: 'description' }));
|
|
83
|
-
})
|
|
79
|
+
})
|
|
80
|
+
?.remove();
|
|
84
81
|
// Pre-parse or remove defaultMessage
|
|
85
82
|
if (defaultMessageProp) {
|
|
86
83
|
if (removeDefaultMessage) {
|
|
87
|
-
defaultMessageProp
|
|
84
|
+
defaultMessageProp?.remove();
|
|
88
85
|
}
|
|
89
86
|
else if (descriptor.defaultMessage) {
|
|
90
|
-
|
|
87
|
+
const valueProp = defaultMessageProp.get('value');
|
|
91
88
|
if (ast) {
|
|
92
89
|
valueProp.replaceWithSourceString(JSON.stringify((0, icu_messageformat_parser_1.parse)(descriptor.defaultMessage)));
|
|
93
90
|
}
|
|
@@ -101,24 +98,24 @@ var visitor = function (path, _a) {
|
|
|
101
98
|
// Check that this is `defineMessages` call
|
|
102
99
|
if (callee.isIdentifier({ name: 'defineMessages' }) ||
|
|
103
100
|
callee.isIdentifier({ name: 'defineMessage' })) {
|
|
104
|
-
|
|
105
|
-
|
|
101
|
+
const firstArgument = args[0];
|
|
102
|
+
const messagesObj = getMessagesObjectFromExpression(firstArgument);
|
|
106
103
|
assertObjectExpression(messagesObj, callee);
|
|
107
104
|
if (callee.isIdentifier({ name: 'defineMessage' })) {
|
|
108
105
|
processMessageObject(messagesObj);
|
|
109
106
|
}
|
|
110
107
|
else {
|
|
111
|
-
|
|
108
|
+
const properties = messagesObj.get('properties');
|
|
112
109
|
if (Array.isArray(properties)) {
|
|
113
110
|
properties
|
|
114
|
-
.map(
|
|
111
|
+
.map(prop => prop.get('value'))
|
|
115
112
|
.forEach(processMessageObject);
|
|
116
113
|
}
|
|
117
114
|
}
|
|
118
115
|
}
|
|
119
116
|
// Check that this is `intl.formatMessage` call
|
|
120
117
|
if (isFormatMessageCall(callee, functionNames)) {
|
|
121
|
-
|
|
118
|
+
const messageDescriptor = args[0];
|
|
122
119
|
if (messageDescriptor.isObjectExpression()) {
|
|
123
120
|
processMessageObject(messageDescriptor);
|
|
124
121
|
}
|
|
File without changes
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jsx-opening-element.d.ts","sourceRoot":"","sources":["
|
|
1
|
+
{"version":3,"file":"jsx-opening-element.d.ts","sourceRoot":"","sources":["jsx-opening-element.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,UAAU,EAAC,MAAM,aAAa,CAAA;AAEhD,OAAO,EAAU,KAAK,EAAC,MAAM,UAAU,CAAA;AACvC,OAAO,KAAK,CAAC,MAAM,cAAc,CAAA;AACjC,OAAO,EAAC,iBAAiB,EAAC,MAAM,iBAAiB,CAAA;AAWjD,eAAO,MAAM,OAAO,EAAE,iBAAiB,CACrC,UAAU,GAAG,KAAK,EAClB,CAAC,CAAC,iBAAiB,CAiIpB,CAAA"}
|
|
@@ -1,28 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.visitor = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
var _c = this, componentNames = _c.componentNames, messages = _c.messages;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const t = tslib_1.__importStar(require("@babel/types"));
|
|
6
|
+
const icu_messageformat_parser_1 = require("@formatjs/icu-messageformat-parser");
|
|
7
|
+
const utils_1 = require("../utils");
|
|
8
|
+
const visitor = function (path, { opts, file: { opts: { filename }, }, }) {
|
|
9
|
+
const { removeDefaultMessage, idInterpolationPattern, overrideIdFn, ast, preserveWhitespace, } = opts;
|
|
10
|
+
const { componentNames, messages } = this;
|
|
12
11
|
if ((0, utils_1.wasExtracted)(path)) {
|
|
13
12
|
return;
|
|
14
13
|
}
|
|
15
|
-
|
|
16
|
-
if (!componentNames.find(
|
|
14
|
+
const name = path.get('name');
|
|
15
|
+
if (!componentNames.find(n => name.isJSXIdentifier({ name: n }))) {
|
|
17
16
|
return;
|
|
18
17
|
}
|
|
19
|
-
|
|
18
|
+
const attributes = path
|
|
20
19
|
.get('attributes')
|
|
21
|
-
.filter(
|
|
22
|
-
|
|
20
|
+
.filter(attr => attr.isJSXAttribute());
|
|
21
|
+
const descriptorPath = (0, utils_1.createMessageDescriptor)(attributes.map(attr => [
|
|
23
22
|
attr.get('name'),
|
|
24
23
|
attr.get('value'),
|
|
25
|
-
]
|
|
24
|
+
]));
|
|
26
25
|
// In order for a default message to be extracted when
|
|
27
26
|
// declaring a JSX element, it must be done with standard
|
|
28
27
|
// `key=value` attributes. But it's completely valid to
|
|
@@ -34,14 +33,13 @@ var visitor = function (path, _a) {
|
|
|
34
33
|
}
|
|
35
34
|
// Evaluate the Message Descriptor values in a JSX
|
|
36
35
|
// context, then store it.
|
|
37
|
-
|
|
36
|
+
const descriptor = (0, utils_1.evaluateMessageDescriptor)(descriptorPath, true, filename || undefined, idInterpolationPattern, overrideIdFn, preserveWhitespace);
|
|
38
37
|
(0, utils_1.storeMessage)(descriptor, path, opts, filename || undefined, messages);
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
for (
|
|
44
|
-
var attr = attributes_1[_i];
|
|
38
|
+
let idAttr;
|
|
39
|
+
let descriptionAttr;
|
|
40
|
+
let defaultMessageAttr;
|
|
41
|
+
const firstAttr = attributes[0];
|
|
42
|
+
for (const attr of attributes) {
|
|
45
43
|
if (!attr.isJSXAttribute()) {
|
|
46
44
|
continue;
|
|
47
45
|
}
|
|
@@ -77,7 +75,7 @@ var visitor = function (path, _a) {
|
|
|
77
75
|
defaultMessageAttr
|
|
78
76
|
.get('value')
|
|
79
77
|
.replaceWith(t.jsxExpressionContainer(t.nullLiteral()));
|
|
80
|
-
|
|
78
|
+
const valueAttr = defaultMessageAttr.get('value');
|
|
81
79
|
valueAttr
|
|
82
80
|
.get('expression')
|
|
83
81
|
.replaceWithSourceString(JSON.stringify((0, icu_messageformat_parser_1.parse)(descriptor.defaultMessage)));
|