@lingui/macro 3.16.1 → 3.17.1
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/CHANGELOG.md +27 -0
- package/LICENSE +1 -1
- package/README.md +2 -2
- package/build/LICENSE +1 -1
- package/build/constants.js +1 -1
- package/build/global.d.ts +1 -3
- package/build/icu.js +6 -17
- package/build/index.d.ts +1 -3
- package/build/index.js +17 -48
- package/build/macroJs.js +36 -54
- package/build/macroJsx.js +28 -63
- package/build/utils.js +3 -9
- package/package.json +5 -4
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,33 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [3.17.1](https://github.com/lingui/js-lingui/compare/v3.17.0...v3.17.1) (2023-02-07)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* **build:** undeclared dependencies ([#1391](https://github.com/lingui/js-lingui/issues/1391)) ([f390ca4](https://github.com/lingui/js-lingui/commit/f390ca4517144344fcbbbf9c73a42a1a17d0e519))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
# [3.17.0](https://github.com/lingui/js-lingui/compare/v3.16.1...v3.17.0) (2023-02-01)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Bug Fixes
|
|
21
|
+
|
|
22
|
+
* **macro:** JS macros don't strip non-essential props in production ([#1389](https://github.com/lingui/js-lingui/issues/1389)) ([0ff55d6](https://github.com/lingui/js-lingui/commit/0ff55d606e4eba496548675e42c744d0d872d838))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
### Features
|
|
26
|
+
|
|
27
|
+
* **macro:** throw useful error message if macro used without a plugin ([#1355](https://github.com/lingui/js-lingui/issues/1355)) ([7d55904](https://github.com/lingui/js-lingui/commit/7d55904bb76fae384558945863423145978b9bd6))
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
6
33
|
## [3.16.1](https://github.com/lingui/js-lingui/compare/v3.16.0...v3.16.1) (2023-01-24)
|
|
7
34
|
|
|
8
35
|
**Note:** Version bump only for package @lingui/macro
|
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
The MIT License (MIT)
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2017 Tomáš Ehrlich
|
|
3
|
+
Copyright (c) 2017-2022 Tomáš Ehrlich, (c) 2022-2023 Crowdin.
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -40,8 +40,8 @@ const message = i18n._(t`Hello, my name is ${name} and today is ${date(now)}`)
|
|
|
40
40
|
|
|
41
41
|
[license]: https://github.com/lingui/js-lingui/blob/main/LICENSE
|
|
42
42
|
[linguijs]: https://github.com/lingui/js-lingui
|
|
43
|
-
[documentation]: https://lingui.
|
|
44
|
-
[reference]: https://lingui.
|
|
43
|
+
[documentation]: https://lingui.dev
|
|
44
|
+
[reference]: https://lingui.dev/ref/macro/
|
|
45
45
|
[package]: https://www.npmjs.com/package/@lingui/macro
|
|
46
46
|
[badge-downloads]: https://img.shields.io/npm/dw/@lingui/macro.svg
|
|
47
47
|
[badge-version]: https://img.shields.io/npm/v/@lingui/macro.svg
|
package/build/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
The MIT License (MIT)
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2017 Tomáš Ehrlich
|
|
3
|
+
Copyright (c) 2017-2022 Tomáš Ehrlich, (c) 2022-2023 Crowdin.
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/build/constants.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.
|
|
6
|
+
exports.MESSAGE = exports.ID = exports.EXTRACT_MARK = exports.CONTEXT = exports.COMMENT = void 0;
|
|
7
7
|
const ID = "id";
|
|
8
8
|
exports.ID = ID;
|
|
9
9
|
const MESSAGE = "message";
|
package/build/global.d.ts
CHANGED
|
@@ -72,9 +72,7 @@ declare module "@lingui/macro" {
|
|
|
72
72
|
* const message = t(i18n)({ message: `Hello ${name}` });
|
|
73
73
|
* ```
|
|
74
74
|
*/
|
|
75
|
-
export function t(
|
|
76
|
-
i18n: I18n
|
|
77
|
-
): {
|
|
75
|
+
export function t(i18n: I18n): {
|
|
78
76
|
(literals: TemplateStringsArray, ...placeholders: any[]): string
|
|
79
77
|
(descriptor: MessageDescriptor): string
|
|
80
78
|
}
|
package/build/icu.js
CHANGED
|
@@ -4,20 +4,20 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
7
|
var _types = require("@babel/types");
|
|
9
|
-
|
|
10
8
|
const metaOptions = ["id", "comment", "props"];
|
|
11
9
|
const escapedMetaOptionsRe = new RegExp(`^_(${metaOptions.join("|")})$`);
|
|
12
|
-
|
|
13
10
|
class ICUMessageFormat {
|
|
14
11
|
fromTokens(tokens) {
|
|
15
|
-
return (Array.isArray(tokens) ? tokens : [tokens]).map(token => this.processToken(token)).filter(Boolean).reduce((props, message) => ({
|
|
12
|
+
return (Array.isArray(tokens) ? tokens : [tokens]).map(token => this.processToken(token)).filter(Boolean).reduce((props, message) => ({
|
|
13
|
+
...message,
|
|
16
14
|
message: props.message + message.message,
|
|
17
|
-
values: {
|
|
15
|
+
values: {
|
|
16
|
+
...props.values,
|
|
18
17
|
...message.values
|
|
19
18
|
},
|
|
20
|
-
jsxElements: {
|
|
19
|
+
jsxElements: {
|
|
20
|
+
...props.jsxElements,
|
|
21
21
|
...message.jsxElements
|
|
22
22
|
}
|
|
23
23
|
}), {
|
|
@@ -26,10 +26,8 @@ class ICUMessageFormat {
|
|
|
26
26
|
jsxElements: {}
|
|
27
27
|
});
|
|
28
28
|
}
|
|
29
|
-
|
|
30
29
|
processToken(token) {
|
|
31
30
|
const jsxElements = {};
|
|
32
|
-
|
|
33
31
|
if (token.type === "text") {
|
|
34
32
|
return {
|
|
35
33
|
message: token.value
|
|
@@ -38,11 +36,9 @@ class ICUMessageFormat {
|
|
|
38
36
|
if (token.value !== undefined && (0, _types.isJSXEmptyExpression)(token.value)) {
|
|
39
37
|
return null;
|
|
40
38
|
}
|
|
41
|
-
|
|
42
39
|
const values = token.value !== undefined ? {
|
|
43
40
|
[token.name]: token.value
|
|
44
41
|
} : {};
|
|
45
|
-
|
|
46
42
|
switch (token.format) {
|
|
47
43
|
case "plural":
|
|
48
44
|
case "select":
|
|
@@ -50,12 +46,10 @@ class ICUMessageFormat {
|
|
|
50
46
|
const formatOptions = Object.keys(token.options).filter(key => token.options[key] != null).map(key => {
|
|
51
47
|
let value = token.options[key];
|
|
52
48
|
key = key.replace(escapedMetaOptionsRe, "$1");
|
|
53
|
-
|
|
54
49
|
if (key === "offset") {
|
|
55
50
|
// offset has special syntax `offset:number`
|
|
56
51
|
return `offset:${value}`;
|
|
57
52
|
}
|
|
58
|
-
|
|
59
53
|
if (typeof value !== "string") {
|
|
60
54
|
// process tokens from nested formatters
|
|
61
55
|
const {
|
|
@@ -67,7 +61,6 @@ class ICUMessageFormat {
|
|
|
67
61
|
Object.assign(jsxElements, childJsxElements);
|
|
68
62
|
value = message;
|
|
69
63
|
}
|
|
70
|
-
|
|
71
64
|
return `${key} {${value}}`;
|
|
72
65
|
}).join(" ");
|
|
73
66
|
return {
|
|
@@ -75,7 +68,6 @@ class ICUMessageFormat {
|
|
|
75
68
|
values,
|
|
76
69
|
jsxElements
|
|
77
70
|
};
|
|
78
|
-
|
|
79
71
|
default:
|
|
80
72
|
return {
|
|
81
73
|
message: `{${token.name}}`,
|
|
@@ -104,10 +96,7 @@ class ICUMessageFormat {
|
|
|
104
96
|
jsxElements
|
|
105
97
|
};
|
|
106
98
|
}
|
|
107
|
-
|
|
108
99
|
throw new Error(`Unknown token type ${token.type}`);
|
|
109
100
|
}
|
|
110
|
-
|
|
111
101
|
}
|
|
112
|
-
|
|
113
102
|
exports.default = ICUMessageFormat;
|
package/build/index.d.ts
CHANGED
|
@@ -77,9 +77,7 @@ export function t(
|
|
|
77
77
|
* const message = t(i18n)({ message: `Hello ${name}` });
|
|
78
78
|
* ```
|
|
79
79
|
*/
|
|
80
|
-
export function t(
|
|
81
|
-
i18n: I18n
|
|
82
|
-
): {
|
|
80
|
+
export function t(i18n: I18n): {
|
|
83
81
|
(literals: TemplateStringsArray, ...placeholders: any[]): string
|
|
84
82
|
(descriptor: MessageDescriptor): string
|
|
85
83
|
}
|
package/build/index.js
CHANGED
|
@@ -4,23 +4,15 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
7
|
var _babelPluginMacros = require("babel-plugin-macros");
|
|
9
|
-
|
|
10
8
|
var _conf = require("@lingui/conf");
|
|
11
|
-
|
|
12
9
|
var _macroJs = _interopRequireDefault(require("./macroJs"));
|
|
13
|
-
|
|
14
10
|
var _macroJsx = _interopRequireDefault(require("./macroJsx"));
|
|
15
|
-
|
|
16
11
|
var _types = require("@babel/types");
|
|
17
|
-
|
|
18
12
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
19
|
-
|
|
20
13
|
const config = (0, _conf.getConfig)({
|
|
21
14
|
configPath: process.env.LINGUI_CONFIG
|
|
22
15
|
});
|
|
23
|
-
|
|
24
16
|
const getSymbolSource = name => {
|
|
25
17
|
if (Array.isArray(config.runtimeConfigModule)) {
|
|
26
18
|
if (name === "i18n") {
|
|
@@ -36,10 +28,10 @@ const getSymbolSource = name => {
|
|
|
36
28
|
}
|
|
37
29
|
}
|
|
38
30
|
};
|
|
39
|
-
|
|
40
31
|
const [i18nImportModule, i18nImportName = "i18n"] = getSymbolSource("i18n");
|
|
41
32
|
const [TransImportModule, TransImportName = "Trans"] = getSymbolSource("Trans");
|
|
42
|
-
|
|
33
|
+
const jsMacroTags = new Set(["defineMessage", "arg", "t", "plural", "select", "selectOrdinal"]);
|
|
34
|
+
const jsxMacroTags = new Set(["Trans", "Plural", "Select", "SelectOrdinal"]);
|
|
43
35
|
function macro({
|
|
44
36
|
references,
|
|
45
37
|
state,
|
|
@@ -50,21 +42,17 @@ function macro({
|
|
|
50
42
|
let needsI18nImport = false;
|
|
51
43
|
Object.keys(references).forEach(tagName => {
|
|
52
44
|
const nodes = references[tagName];
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
if (macroType == null) {
|
|
56
|
-
throw nodes[0].buildCodeFrameError(`Unknown macro ${tagName}`);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (macroType === "js") {
|
|
45
|
+
if (jsMacroTags.has(tagName)) {
|
|
60
46
|
nodes.forEach(node => {
|
|
61
47
|
jsNodes.push(node.parentPath);
|
|
62
48
|
});
|
|
63
|
-
} else {
|
|
49
|
+
} else if (jsxMacroTags.has(tagName)) {
|
|
64
50
|
nodes.forEach(node => {
|
|
65
51
|
// identifier.openingElement.jsxElement
|
|
66
52
|
jsxNodes.push(node.parentPath.parentPath);
|
|
67
53
|
});
|
|
54
|
+
} else {
|
|
55
|
+
throw nodes[0].buildCodeFrameError(`Unknown macro ${tagName}`);
|
|
68
56
|
}
|
|
69
57
|
});
|
|
70
58
|
jsNodes.filter(isRootPath(jsNodes)).forEach(path => {
|
|
@@ -79,30 +67,27 @@ function macro({
|
|
|
79
67
|
const macro = new _macroJsx.default(babel);
|
|
80
68
|
macro.replacePath(path);
|
|
81
69
|
});
|
|
82
|
-
|
|
83
70
|
if (needsI18nImport) {
|
|
84
71
|
addImport(babel, state, i18nImportModule, i18nImportName);
|
|
85
72
|
}
|
|
86
|
-
|
|
87
73
|
if (jsxNodes.length) {
|
|
88
74
|
addImport(babel, state, TransImportModule, TransImportName);
|
|
89
75
|
}
|
|
90
|
-
|
|
91
76
|
if (process.env.LINGUI_EXTRACT === "1") {
|
|
92
77
|
return {
|
|
93
78
|
keepImports: true
|
|
94
79
|
};
|
|
95
80
|
}
|
|
96
81
|
}
|
|
97
|
-
|
|
98
82
|
function addImport(babel, state, module, importName) {
|
|
99
83
|
const {
|
|
100
84
|
types: t
|
|
101
85
|
} = babel;
|
|
102
|
-
const linguiImport = state.file.path.node.body.find(importNode => t.isImportDeclaration(importNode) && importNode.source.value === module &&
|
|
86
|
+
const linguiImport = state.file.path.node.body.find(importNode => t.isImportDeclaration(importNode) && importNode.source.value === module &&
|
|
87
|
+
// https://github.com/lingui/js-lingui/issues/777
|
|
103
88
|
importNode.importKind !== "type");
|
|
104
|
-
const tIdentifier = t.identifier(importName);
|
|
105
|
-
|
|
89
|
+
const tIdentifier = t.identifier(importName);
|
|
90
|
+
// Handle adding the import or altering the existing import
|
|
106
91
|
if (linguiImport) {
|
|
107
92
|
if (linguiImport.specifiers.findIndex(specifier => (0, _types.isImportSpecifier)(specifier) && (0, _types.isIdentifier)(specifier.imported, {
|
|
108
93
|
name: importName
|
|
@@ -113,7 +98,6 @@ function addImport(babel, state, module, importName) {
|
|
|
113
98
|
state.file.path.node.body.unshift(t.importDeclaration([t.importSpecifier(tIdentifier, tIdentifier)], t.stringLiteral(module)));
|
|
114
99
|
}
|
|
115
100
|
}
|
|
116
|
-
|
|
117
101
|
function isRootPath(allPath) {
|
|
118
102
|
return node => function traverse(path) {
|
|
119
103
|
if (!path.parentPath) {
|
|
@@ -123,9 +107,7 @@ function isRootPath(allPath) {
|
|
|
123
107
|
}
|
|
124
108
|
}(node);
|
|
125
109
|
}
|
|
126
|
-
|
|
127
110
|
const alreadyVisitedCache = new WeakSet();
|
|
128
|
-
|
|
129
111
|
const alreadyVisited = path => {
|
|
130
112
|
if (alreadyVisitedCache.has(path)) {
|
|
131
113
|
return true;
|
|
@@ -134,25 +116,12 @@ const alreadyVisited = path => {
|
|
|
134
116
|
return false;
|
|
135
117
|
}
|
|
136
118
|
};
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
case "select":
|
|
145
|
-
case "selectOrdinal":
|
|
146
|
-
return "js";
|
|
147
|
-
|
|
148
|
-
case "Trans":
|
|
149
|
-
case "Plural":
|
|
150
|
-
case "Select":
|
|
151
|
-
case "SelectOrdinal":
|
|
152
|
-
return "jsx";
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
119
|
+
[...jsMacroTags, ...jsxMacroTags].forEach(name => {
|
|
120
|
+
Object.defineProperty(module.exports, name, {
|
|
121
|
+
get() {
|
|
122
|
+
throw new Error(`The macro you imported from "@lingui/macro" is being executed outside the context of compilation with babel-plugin-macros. ` + `This indicates that you don't have the babel plugin "babel-plugin-macros" configured correctly. ` + `Please see the documentation for how to configure babel-plugin-macros properly: ` + "https://github.com/kentcdodds/babel-plugin-macros/blob/main/other/docs/user.md");
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
});
|
|
156
126
|
var _default = (0, _babelPluginMacros.createMacro)(macro);
|
|
157
|
-
|
|
158
127
|
exports.default = _default;
|
package/build/macroJs.js
CHANGED
|
@@ -4,36 +4,26 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
7
|
var R = _interopRequireWildcard(require("ramda"));
|
|
9
|
-
|
|
10
8
|
var _types = require("@babel/types");
|
|
11
|
-
|
|
12
9
|
var _icu = _interopRequireDefault(require("./icu"));
|
|
13
|
-
|
|
14
10
|
var _utils = require("./utils");
|
|
15
|
-
|
|
16
11
|
var _constants = require("./constants");
|
|
17
|
-
|
|
18
12
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
19
|
-
|
|
20
|
-
function
|
|
21
|
-
|
|
22
|
-
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
23
|
-
|
|
13
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
14
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
24
15
|
const keepSpaceRe = /(?:\\(?:\r\n|\r|\n))+\s+/g;
|
|
25
16
|
const keepNewLineRe = /(?:\r\n|\r|\n)+\s+/g;
|
|
26
|
-
|
|
27
17
|
function normalizeWhitespace(text) {
|
|
28
18
|
return text.replace(keepSpaceRe, " ").replace(keepNewLineRe, "\n").trim();
|
|
29
19
|
}
|
|
30
|
-
|
|
31
20
|
class MacroJs {
|
|
32
21
|
// Babel Types
|
|
22
|
+
|
|
33
23
|
// Identifier of i18n object
|
|
24
|
+
|
|
34
25
|
// Positional expressions counter (e.g. for placeholders `Hello {0}, today is {1}`)
|
|
35
26
|
_expressionIndex = (0, _utils.makeCounter)();
|
|
36
|
-
|
|
37
27
|
constructor({
|
|
38
28
|
types
|
|
39
29
|
}, {
|
|
@@ -42,36 +32,34 @@ class MacroJs {
|
|
|
42
32
|
this.types = types;
|
|
43
33
|
this.i18nImportName = i18nImportName;
|
|
44
34
|
}
|
|
45
|
-
|
|
46
35
|
replacePathWithMessage = (path, {
|
|
47
36
|
message,
|
|
48
37
|
values
|
|
49
38
|
}, linguiInstance) => {
|
|
50
39
|
const args = [];
|
|
51
40
|
args.push(isString(message) ? this.types.stringLiteral(message) : message);
|
|
52
|
-
|
|
53
41
|
if (Object.keys(values).length) {
|
|
54
42
|
const valuesObject = Object.keys(values).map(key => this.types.objectProperty(this.types.identifier(key), values[key]));
|
|
55
43
|
args.push(this.types.objectExpression(valuesObject));
|
|
56
44
|
}
|
|
45
|
+
const newNode = this.types.callExpression(this.types.memberExpression(linguiInstance ?? this.types.identifier(this.i18nImportName), this.types.identifier("_")), args);
|
|
57
46
|
|
|
58
|
-
|
|
59
|
-
|
|
47
|
+
// preserve line number
|
|
60
48
|
newNode.loc = path.node.loc;
|
|
61
49
|
path.addComment("leading", _constants.EXTRACT_MARK);
|
|
62
50
|
path.replaceWith(newNode);
|
|
63
|
-
};
|
|
51
|
+
};
|
|
64
52
|
|
|
53
|
+
// Returns a boolean indicating if the replacement requires i18n import
|
|
65
54
|
replacePath = path => {
|
|
66
55
|
// reset the expression counter
|
|
67
56
|
this._expressionIndex = (0, _utils.makeCounter)();
|
|
68
|
-
|
|
69
57
|
if (this.isDefineMessage(path.node)) {
|
|
70
58
|
this.replaceDefineMessage(path);
|
|
71
59
|
return true;
|
|
72
|
-
}
|
|
73
|
-
|
|
60
|
+
}
|
|
74
61
|
|
|
62
|
+
// t(i18nInstance)`Message` -> i18nInstance._('Message')
|
|
75
63
|
if (this.types.isCallExpression(path.node) && this.types.isTaggedTemplateExpression(path.parentPath.node) && this.types.isIdentifier(path.node.arguments[0]) && this.isIdentifier(path.node.callee, "t")) {
|
|
76
64
|
// Use the first argument as i18n instance instead of the default i18n instance
|
|
77
65
|
const i18nInstance = path.node.arguments[0];
|
|
@@ -87,20 +75,18 @@ class MacroJs {
|
|
|
87
75
|
values
|
|
88
76
|
}, i18nInstance);
|
|
89
77
|
return false;
|
|
90
|
-
}
|
|
91
|
-
|
|
78
|
+
}
|
|
92
79
|
|
|
80
|
+
// t(i18nInstance)(messageDescriptor) -> i18nInstance._(messageDescriptor)
|
|
93
81
|
if (this.types.isCallExpression(path.node) && this.types.isCallExpression(path.parentPath.node) && this.types.isIdentifier(path.node.arguments[0]) && this.isIdentifier(path.node.callee, "t")) {
|
|
94
82
|
const i18nInstance = path.node.arguments[0];
|
|
95
83
|
this.replaceTAsFunction(path.parentPath, i18nInstance);
|
|
96
84
|
return false;
|
|
97
85
|
}
|
|
98
|
-
|
|
99
86
|
if (this.types.isCallExpression(path.node) && this.isIdentifier(path.node.callee, "t")) {
|
|
100
87
|
this.replaceTAsFunction(path);
|
|
101
88
|
return true;
|
|
102
89
|
}
|
|
103
|
-
|
|
104
90
|
const tokens = this.tokenizeNode(path.node);
|
|
105
91
|
const messageFormat = new _icu.default();
|
|
106
92
|
const {
|
|
@@ -114,6 +100,7 @@ class MacroJs {
|
|
|
114
100
|
});
|
|
115
101
|
return true;
|
|
116
102
|
};
|
|
103
|
+
|
|
117
104
|
/**
|
|
118
105
|
* macro `defineMessage` is called with MessageDescriptor. The only
|
|
119
106
|
* thing that happens is that any macros used in `message` property
|
|
@@ -135,23 +122,23 @@ class MacroJs {
|
|
|
135
122
|
* }
|
|
136
123
|
*
|
|
137
124
|
*/
|
|
138
|
-
|
|
139
125
|
replaceDefineMessage = path => {
|
|
140
126
|
// reset the expression counter
|
|
141
127
|
this._expressionIndex = (0, _utils.makeCounter)();
|
|
142
|
-
|
|
128
|
+
let descriptor = this.processDescriptor(path.node.arguments[0]);
|
|
143
129
|
path.replaceWith(descriptor);
|
|
144
130
|
};
|
|
131
|
+
|
|
145
132
|
/**
|
|
146
133
|
* macro `t` is called with MessageDescriptor, after that
|
|
147
134
|
* we create a new node to append it to i18n._
|
|
148
135
|
*/
|
|
149
|
-
|
|
150
136
|
replaceTAsFunction = (path, linguiInstance) => {
|
|
151
|
-
|
|
137
|
+
let descriptor = this.processDescriptor(path.node.arguments[0]);
|
|
152
138
|
const newNode = this.types.callExpression(this.types.memberExpression(linguiInstance ?? this.types.identifier(this.i18nImportName), this.types.identifier("_")), [descriptor]);
|
|
153
139
|
path.replaceWith(newNode);
|
|
154
140
|
};
|
|
141
|
+
|
|
155
142
|
/**
|
|
156
143
|
* `processDescriptor` expand macros inside message descriptor.
|
|
157
144
|
* Message descriptor is used in `defineMessage`.
|
|
@@ -169,23 +156,21 @@ class MacroJs {
|
|
|
169
156
|
* }
|
|
170
157
|
*
|
|
171
158
|
*/
|
|
172
|
-
|
|
173
159
|
processDescriptor = descriptor_ => {
|
|
174
160
|
const descriptor = descriptor_;
|
|
175
161
|
this.types.addComment(descriptor, "leading", _constants.EXTRACT_MARK);
|
|
176
162
|
const messageIndex = descriptor.properties.findIndex(property => (0, _types.isObjectProperty)(property) && this.isIdentifier(property.key, _constants.MESSAGE));
|
|
177
|
-
|
|
178
163
|
if (messageIndex === -1) {
|
|
179
164
|
return descriptor;
|
|
180
|
-
}
|
|
165
|
+
}
|
|
181
166
|
|
|
167
|
+
// if there's `message` property, replace macros with formatted message
|
|
168
|
+
const node = descriptor.properties[messageIndex];
|
|
182
169
|
|
|
183
|
-
|
|
170
|
+
// Inside message descriptor the `t` macro in `message` prop is optional.
|
|
184
171
|
// Template strings are always processed as if they were wrapped by `t`.
|
|
185
|
-
|
|
186
172
|
const tokens = this.types.isTemplateLiteral(node.value) ? this.tokenizeTemplateLiteral(node.value) : this.tokenizeNode(node.value, true);
|
|
187
173
|
let messageNode = node.value;
|
|
188
|
-
|
|
189
174
|
if (tokens != null) {
|
|
190
175
|
const messageFormat = new _icu.default();
|
|
191
176
|
const {
|
|
@@ -195,11 +180,14 @@ class MacroJs {
|
|
|
195
180
|
const message = normalizeWhitespace(messageRaw);
|
|
196
181
|
messageNode = this.types.stringLiteral(message);
|
|
197
182
|
this.addValues(descriptor.properties, values);
|
|
198
|
-
}
|
|
199
|
-
|
|
183
|
+
}
|
|
200
184
|
|
|
185
|
+
// Don't override custom ID
|
|
201
186
|
const hasId = descriptor.properties.findIndex(property => (0, _types.isObjectProperty)(property) && this.isIdentifier(property.key, _constants.ID)) !== -1;
|
|
202
187
|
descriptor.properties[messageIndex] = this.types.objectProperty(this.types.identifier(hasId ? _constants.MESSAGE : _constants.ID), messageNode);
|
|
188
|
+
if (process.env.NODE_ENV === "production") {
|
|
189
|
+
descriptor.properties = descriptor.properties.filter(property => (0, _types.isObjectProperty)(property) && !this.isIdentifier(property.key, _constants.MESSAGE) && (0, _types.isObjectProperty)(property) && !this.isIdentifier(property.key, _constants.COMMENT));
|
|
190
|
+
}
|
|
203
191
|
return descriptor;
|
|
204
192
|
};
|
|
205
193
|
addValues = (obj, values) => {
|
|
@@ -213,19 +201,20 @@ class MacroJs {
|
|
|
213
201
|
return this.tokenizeTemplateLiteral(node);
|
|
214
202
|
} else if (this.isChoiceMethod(node)) {
|
|
215
203
|
// plural, select and selectOrdinal
|
|
216
|
-
return [this.tokenizeChoiceComponent(node)];
|
|
204
|
+
return [this.tokenizeChoiceComponent(node)];
|
|
205
|
+
// } else if (isFormatMethod(node.callee)) {
|
|
217
206
|
// // date, number
|
|
218
207
|
// return transformFormatMethod(node, file, props, root)
|
|
219
208
|
} else if (!ignoreExpression) {
|
|
220
209
|
return this.tokenizeExpression(node);
|
|
221
210
|
}
|
|
222
211
|
};
|
|
212
|
+
|
|
223
213
|
/**
|
|
224
214
|
* `node` is a TemplateLiteral. node.quasi contains
|
|
225
215
|
* text chunks and node.expressions contains expressions.
|
|
226
216
|
* Both arrays must be zipped together to get the final list of tokens.
|
|
227
217
|
*/
|
|
228
|
-
|
|
229
218
|
tokenizeTemplateLiteral = node => {
|
|
230
219
|
const tokenize = R.pipe(R.evolve({
|
|
231
220
|
quasis: R.map(text => {
|
|
@@ -246,30 +235,29 @@ class MacroJs {
|
|
|
246
235
|
};
|
|
247
236
|
tokenizeChoiceComponent = node => {
|
|
248
237
|
const format = node.callee.name.toLowerCase();
|
|
249
|
-
const token = {
|
|
238
|
+
const token = {
|
|
239
|
+
...this.tokenizeExpression(node.arguments[0]),
|
|
250
240
|
format,
|
|
251
241
|
options: {
|
|
252
242
|
offset: undefined
|
|
253
243
|
}
|
|
254
244
|
};
|
|
255
245
|
const props = node.arguments[1].properties;
|
|
256
|
-
|
|
257
246
|
for (const attr of props) {
|
|
258
247
|
const {
|
|
259
248
|
key,
|
|
260
249
|
value: attrValue
|
|
261
|
-
} = attr;
|
|
250
|
+
} = attr;
|
|
251
|
+
|
|
252
|
+
// name is either:
|
|
262
253
|
// NumericLiteral => convert to `={number}`
|
|
263
254
|
// StringLiteral => key.value
|
|
264
255
|
// Identifier => key.name
|
|
265
|
-
|
|
266
256
|
const name = this.types.isNumericLiteral(key) ? `=${key.value}` : key.name || key.value;
|
|
267
|
-
|
|
268
257
|
if (format !== "select" && name === "offset") {
|
|
269
258
|
token.options.offset = attrValue.value;
|
|
270
259
|
} else {
|
|
271
260
|
let value;
|
|
272
|
-
|
|
273
261
|
if (this.types.isTemplateLiteral(attrValue)) {
|
|
274
262
|
value = this.tokenizeTemplateLiteral(attrValue);
|
|
275
263
|
} else if (this.types.isCallExpression(attrValue)) {
|
|
@@ -277,11 +265,9 @@ class MacroJs {
|
|
|
277
265
|
} else {
|
|
278
266
|
value = attrValue.value;
|
|
279
267
|
}
|
|
280
|
-
|
|
281
268
|
token.options[name] = value;
|
|
282
269
|
}
|
|
283
270
|
}
|
|
284
|
-
|
|
285
271
|
return token;
|
|
286
272
|
};
|
|
287
273
|
tokenizeExpression = node => {
|
|
@@ -292,7 +278,6 @@ class MacroJs {
|
|
|
292
278
|
value: undefined
|
|
293
279
|
};
|
|
294
280
|
}
|
|
295
|
-
|
|
296
281
|
return {
|
|
297
282
|
type: "arg",
|
|
298
283
|
name: this.expressionToArgument(node),
|
|
@@ -308,19 +293,18 @@ class MacroJs {
|
|
|
308
293
|
return String(this._expressionIndex());
|
|
309
294
|
}
|
|
310
295
|
};
|
|
296
|
+
|
|
311
297
|
/**
|
|
312
298
|
* We clean '//\` ' to just '`'
|
|
313
299
|
*/
|
|
314
|
-
|
|
315
300
|
clearBackslashes(value) {
|
|
316
301
|
// if not we replace the extra scaped literals
|
|
317
302
|
return value.replace(/\\`/g, "`");
|
|
318
303
|
}
|
|
304
|
+
|
|
319
305
|
/**
|
|
320
306
|
* Custom matchers
|
|
321
307
|
*/
|
|
322
|
-
|
|
323
|
-
|
|
324
308
|
isIdentifier = (node, name) => {
|
|
325
309
|
return this.types.isIdentifier(node, {
|
|
326
310
|
name
|
|
@@ -339,7 +323,5 @@ class MacroJs {
|
|
|
339
323
|
return this.types.isCallExpression(node) && (this.isIdentifier(node.callee, "plural") || this.isIdentifier(node.callee, "select") || this.isIdentifier(node.callee, "selectOrdinal"));
|
|
340
324
|
};
|
|
341
325
|
}
|
|
342
|
-
|
|
343
326
|
exports.default = MacroJs;
|
|
344
|
-
|
|
345
327
|
const isString = s => typeof s === "string";
|
package/build/macroJsx.js
CHANGED
|
@@ -3,32 +3,22 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.normalizeWhitespace = normalizeWhitespace;
|
|
7
6
|
exports.default = void 0;
|
|
8
|
-
|
|
7
|
+
exports.normalizeWhitespace = normalizeWhitespace;
|
|
9
8
|
var R = _interopRequireWildcard(require("ramda"));
|
|
10
|
-
|
|
11
9
|
var _icu = _interopRequireDefault(require("./icu"));
|
|
12
|
-
|
|
13
10
|
var _utils = require("./utils");
|
|
14
|
-
|
|
15
11
|
var _constants = require("./constants");
|
|
16
|
-
|
|
17
12
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
18
|
-
|
|
19
|
-
function
|
|
20
|
-
|
|
21
|
-
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
22
|
-
|
|
13
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
14
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
23
15
|
const pluralRuleRe = /(_[\d\w]+|zero|one|two|few|many|other)/;
|
|
16
|
+
const jsx2icuExactChoice = value => value.replace(/_(\d+)/, "=$1").replace(/_(\w+)/, "$1");
|
|
24
17
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const keepSpaceRe = /\s*(?:\r\n|\r|\n)+\s*/g; // remove whitespace before/after tag or expression
|
|
29
|
-
|
|
18
|
+
// replace whitespace before/after newline with single space
|
|
19
|
+
const keepSpaceRe = /\s*(?:\r\n|\r|\n)+\s*/g;
|
|
20
|
+
// remove whitespace before/after tag or expression
|
|
30
21
|
const stripAroundTagsRe = /(?:([>}])(?:\r\n|\r|\n)+\s*|(?:\r\n|\r|\n)+\s*(?=[<{]))/g;
|
|
31
|
-
|
|
32
22
|
function maybeNodeValue(node) {
|
|
33
23
|
if (!node) return null;
|
|
34
24
|
if (node.type === "StringLiteral") return node.value;
|
|
@@ -37,23 +27,21 @@ function maybeNodeValue(node) {
|
|
|
37
27
|
if (node.type === "TemplateLiteral" && node.expressions.length === 0) return node.quasis[0].value.raw;
|
|
38
28
|
return null;
|
|
39
29
|
}
|
|
40
|
-
|
|
41
30
|
function normalizeWhitespace(text) {
|
|
42
|
-
return text.replace(stripAroundTagsRe, "$1").replace(keepSpaceRe, " ")
|
|
43
|
-
|
|
31
|
+
return text.replace(stripAroundTagsRe, "$1").replace(keepSpaceRe, " ")
|
|
32
|
+
// keep escaped newlines
|
|
33
|
+
.replace(/\\n/g, "\n").replace(/\\s/g, " ")
|
|
34
|
+
// we remove trailing whitespace inside Plural
|
|
44
35
|
.replace(/(\s+})/gm, "}").trim();
|
|
45
36
|
}
|
|
46
|
-
|
|
47
37
|
class MacroJSX {
|
|
48
38
|
expressionIndex = (0, _utils.makeCounter)();
|
|
49
39
|
elementIndex = (0, _utils.makeCounter)();
|
|
50
|
-
|
|
51
40
|
constructor({
|
|
52
41
|
types
|
|
53
42
|
}) {
|
|
54
43
|
this.types = types;
|
|
55
44
|
}
|
|
56
|
-
|
|
57
45
|
safeJsxAttribute = (name, value) => {
|
|
58
46
|
// This handles quoted JSX attributes and html entities.
|
|
59
47
|
return this.types.jsxAttribute(this.types.jsxIdentifier(name), this.types.jsxExpressionContainer(this.types.stringLiteral(value)));
|
|
@@ -73,14 +61,12 @@ class MacroJSX {
|
|
|
73
61
|
comment,
|
|
74
62
|
context
|
|
75
63
|
} = this.stripMacroAttributes(path.node);
|
|
76
|
-
|
|
77
64
|
if (!id && !message) {
|
|
78
65
|
return;
|
|
79
66
|
} else if (id && id !== message) {
|
|
80
67
|
// If `id` prop already exists and generated ID is different,
|
|
81
68
|
// add it as a `default` prop
|
|
82
69
|
attributes.push(this.types.jsxAttribute(this.types.jsxIdentifier(_constants.ID), this.types.stringLiteral(id)));
|
|
83
|
-
|
|
84
70
|
if (process.env.NODE_ENV !== "production") {
|
|
85
71
|
if (message) {
|
|
86
72
|
attributes.push(this.safeJsxAttribute(_constants.MESSAGE, message));
|
|
@@ -89,38 +75,26 @@ class MacroJSX {
|
|
|
89
75
|
} else {
|
|
90
76
|
attributes.push(this.safeJsxAttribute(_constants.ID, message));
|
|
91
77
|
}
|
|
92
|
-
|
|
93
78
|
if (process.env.NODE_ENV !== "production") {
|
|
94
79
|
if (comment) {
|
|
95
80
|
attributes.push(this.types.jsxAttribute(this.types.jsxIdentifier(_constants.COMMENT), this.types.stringLiteral(comment)));
|
|
96
81
|
}
|
|
97
82
|
}
|
|
98
|
-
|
|
99
83
|
if (context) {
|
|
100
84
|
attributes.push(this.types.jsxAttribute(this.types.jsxIdentifier(_constants.CONTEXT), this.types.stringLiteral(context)));
|
|
101
|
-
}
|
|
102
|
-
|
|
85
|
+
}
|
|
103
86
|
|
|
87
|
+
// Parameters for variable substitution
|
|
104
88
|
const valuesObject = Object.keys(values).map(key => this.types.objectProperty(this.types.identifier(key), values[key]));
|
|
105
|
-
|
|
106
89
|
if (valuesObject.length) {
|
|
107
90
|
attributes.push(this.types.jsxAttribute(this.types.jsxIdentifier("values"), this.types.jsxExpressionContainer(this.types.objectExpression(valuesObject))));
|
|
108
|
-
}
|
|
109
|
-
|
|
91
|
+
}
|
|
110
92
|
|
|
93
|
+
// Inline elements
|
|
111
94
|
if (Object.keys(jsxElements).length) {
|
|
112
95
|
attributes.push(this.types.jsxAttribute(this.types.jsxIdentifier("components"), this.types.jsxExpressionContainer(this.types.objectExpression(Object.keys(jsxElements).map(key => this.types.objectProperty(this.types.identifier(key), jsxElements[key]))))));
|
|
113
96
|
}
|
|
114
|
-
|
|
115
|
-
const newNode = this.types.jsxElement(this.types.jsxOpeningElement(this.types.jsxIdentifier("Trans"), attributes,
|
|
116
|
-
/*selfClosing*/
|
|
117
|
-
true),
|
|
118
|
-
/*closingElement*/
|
|
119
|
-
null,
|
|
120
|
-
/*children*/
|
|
121
|
-
[],
|
|
122
|
-
/*selfClosing*/
|
|
123
|
-
true);
|
|
97
|
+
const newNode = this.types.jsxElement(this.types.jsxOpeningElement(this.types.jsxIdentifier("Trans"), attributes, /*selfClosing*/true), /*closingElement*/null, /*children*/[], /*selfClosing*/true);
|
|
124
98
|
newNode.loc = path.node.loc;
|
|
125
99
|
path.replaceWith(newNode);
|
|
126
100
|
};
|
|
@@ -140,12 +114,11 @@ class MacroJSX {
|
|
|
140
114
|
const comment = attributes.filter(this.attrName([_constants.COMMENT]))[0];
|
|
141
115
|
const context = attributes.filter(this.attrName([_constants.CONTEXT]))[0];
|
|
142
116
|
let reserved = [_constants.ID, _constants.MESSAGE, _constants.COMMENT, _constants.CONTEXT];
|
|
143
|
-
|
|
144
|
-
|
|
117
|
+
if (this.isI18nComponent(node)) {
|
|
118
|
+
// no reserved prop names
|
|
145
119
|
} else if (this.isChoiceComponent(node)) {
|
|
146
120
|
reserved = [...reserved, "_\\w+", "_\\d+", "zero", "one", "two", "few", "many", "other", "value", "offset"];
|
|
147
121
|
}
|
|
148
|
-
|
|
149
122
|
return {
|
|
150
123
|
id: maybeNodeValue(id),
|
|
151
124
|
message: maybeNodeValue(message),
|
|
@@ -173,7 +146,6 @@ class MacroJSX {
|
|
|
173
146
|
tokenizeChildren = node => {
|
|
174
147
|
if (this.types.isJSXExpressionContainer(node)) {
|
|
175
148
|
const exp = node.expression;
|
|
176
|
-
|
|
177
149
|
if (this.types.isStringLiteral(exp)) {
|
|
178
150
|
// Escape forced newlines to keep them in message.
|
|
179
151
|
return {
|
|
@@ -181,7 +153,8 @@ class MacroJSX {
|
|
|
181
153
|
value: exp.value.replace(/\n/g, "\\n")
|
|
182
154
|
};
|
|
183
155
|
} else if (this.types.isTemplateLiteral(exp)) {
|
|
184
|
-
const tokenize = R.pipe(
|
|
156
|
+
const tokenize = R.pipe(
|
|
157
|
+
// Don"t output tokens without text.
|
|
185
158
|
R.evolve({
|
|
186
159
|
quasis: R.map(text => {
|
|
187
160
|
// if it's an unicode we keep the cooked value because it's the parsed value by babel (without unicode chars)
|
|
@@ -201,17 +174,20 @@ class MacroJSX {
|
|
|
201
174
|
}
|
|
202
175
|
} else if (this.types.isJSXElement(node)) {
|
|
203
176
|
return this.tokenizeNode(node);
|
|
204
|
-
} else if (this.types.isJSXSpreadChild(node)) {
|
|
177
|
+
} else if (this.types.isJSXSpreadChild(node)) {
|
|
178
|
+
// just do nothing
|
|
205
179
|
} else if (this.types.isJSXText(node)) {
|
|
206
180
|
return this.tokenizeText(node.value);
|
|
207
|
-
} else {
|
|
181
|
+
} else {
|
|
182
|
+
// impossible path
|
|
208
183
|
// return this.tokenizeText(node.value)
|
|
209
184
|
}
|
|
210
185
|
};
|
|
211
186
|
tokenizeChoiceComponent = node => {
|
|
212
187
|
const element = node.openingElement;
|
|
213
188
|
const format = this.getJsxTagName(node).toLowerCase();
|
|
214
|
-
const props = element.attributes.filter(this.attrName([_constants.ID, _constants.COMMENT, _constants.MESSAGE, _constants.CONTEXT, "key",
|
|
189
|
+
const props = element.attributes.filter(this.attrName([_constants.ID, _constants.COMMENT, _constants.MESSAGE, _constants.CONTEXT, "key",
|
|
190
|
+
// we remove <Trans /> react props that are not useful for translation
|
|
215
191
|
"render", "component", "components"], true));
|
|
216
192
|
const token = {
|
|
217
193
|
type: "arg",
|
|
@@ -222,18 +198,14 @@ class MacroJSX {
|
|
|
222
198
|
offset: undefined
|
|
223
199
|
}
|
|
224
200
|
};
|
|
225
|
-
|
|
226
201
|
for (const attr of props) {
|
|
227
202
|
if (this.types.isJSXSpreadAttribute(attr)) {
|
|
228
203
|
continue;
|
|
229
204
|
}
|
|
230
|
-
|
|
231
205
|
if (this.types.isJSXNamespacedName(attr.name)) {
|
|
232
206
|
continue;
|
|
233
207
|
}
|
|
234
|
-
|
|
235
208
|
const name = attr.name.name;
|
|
236
|
-
|
|
237
209
|
if (name === "value") {
|
|
238
210
|
const exp = this.types.isLiteral(attr.value) ? attr.value : attr.value.expression;
|
|
239
211
|
token.name = this.expressionToArgument(exp);
|
|
@@ -243,13 +215,11 @@ class MacroJSX {
|
|
|
243
215
|
token.options.offset = this.types.isStringLiteral(attr.value) ? attr.value.value : attr.value.expression.value;
|
|
244
216
|
} else {
|
|
245
217
|
let value;
|
|
246
|
-
|
|
247
218
|
if (this.types.isStringLiteral(attr.value)) {
|
|
248
219
|
value = attr.value.extra.raw.replace(/(["'])(.*)\1/, "$2");
|
|
249
220
|
} else {
|
|
250
221
|
value = this.tokenizeChildren(attr.value);
|
|
251
222
|
}
|
|
252
|
-
|
|
253
223
|
if (pluralRuleRe.test(name)) {
|
|
254
224
|
token.options[jsx2icuExactChoice(name)] = value;
|
|
255
225
|
} else {
|
|
@@ -257,7 +227,6 @@ class MacroJSX {
|
|
|
257
227
|
}
|
|
258
228
|
}
|
|
259
229
|
}
|
|
260
|
-
|
|
261
230
|
return token;
|
|
262
231
|
};
|
|
263
232
|
tokenizeElement = node => {
|
|
@@ -287,24 +256,21 @@ class MacroJSX {
|
|
|
287
256
|
value
|
|
288
257
|
};
|
|
289
258
|
};
|
|
290
|
-
|
|
291
259
|
expressionToArgument(exp) {
|
|
292
260
|
return this.types.isIdentifier(exp) ? exp.name : String(this.expressionIndex());
|
|
293
261
|
}
|
|
262
|
+
|
|
294
263
|
/**
|
|
295
264
|
* We clean '//\` ' to just '`'
|
|
296
265
|
**/
|
|
297
|
-
|
|
298
|
-
|
|
299
266
|
clearBackslashes(value) {
|
|
300
267
|
// if not we replace the extra scaped literals
|
|
301
268
|
return value.replace(/\\`/g, "`");
|
|
302
269
|
}
|
|
270
|
+
|
|
303
271
|
/**
|
|
304
272
|
* Custom matchers
|
|
305
273
|
*/
|
|
306
|
-
|
|
307
|
-
|
|
308
274
|
isIdentifier = (node, name) => {
|
|
309
275
|
return this.types.isIdentifier(node, {
|
|
310
276
|
name
|
|
@@ -324,5 +290,4 @@ class MacroJSX {
|
|
|
324
290
|
}
|
|
325
291
|
};
|
|
326
292
|
}
|
|
327
|
-
|
|
328
293
|
exports.default = MacroJSX;
|
package/build/utils.js
CHANGED
|
@@ -3,15 +3,11 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.zip = zip;
|
|
7
6
|
exports.makeCounter = void 0;
|
|
8
|
-
|
|
7
|
+
exports.zip = zip;
|
|
9
8
|
var R = _interopRequireWildcard(require("ramda"));
|
|
10
|
-
|
|
11
|
-
function
|
|
12
|
-
|
|
13
|
-
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
14
|
-
|
|
9
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
10
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
15
11
|
/**
|
|
16
12
|
* Custom zip method which takes length of the larger array
|
|
17
13
|
* (usually zip functions use the `smaller` length, discarding values in larger array)
|
|
@@ -19,7 +15,5 @@ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj;
|
|
|
19
15
|
function zip(a, b) {
|
|
20
16
|
return R.range(0, Math.max(a.length, b.length)).map(index => [a[index], b[index]]);
|
|
21
17
|
}
|
|
22
|
-
|
|
23
18
|
const makeCounter = (index = 0) => () => index++;
|
|
24
|
-
|
|
25
19
|
exports.makeCounter = makeCounter;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lingui/macro",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.17.1",
|
|
4
4
|
"description": "Macro for generating messages in ICU MessageFormat syntax",
|
|
5
5
|
"main": "./build/index.js",
|
|
6
6
|
"author": {
|
|
@@ -27,8 +27,9 @@
|
|
|
27
27
|
"build/"
|
|
28
28
|
],
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@babel/runtime": "^7.
|
|
31
|
-
"@
|
|
30
|
+
"@babel/runtime": "^7.20.13",
|
|
31
|
+
"@babel/types": "^7.20.7",
|
|
32
|
+
"@lingui/conf": "3.17.1",
|
|
32
33
|
"ramda": "^0.27.1"
|
|
33
34
|
},
|
|
34
35
|
"peerDependencies": {
|
|
@@ -39,5 +40,5 @@
|
|
|
39
40
|
"devDependencies": {
|
|
40
41
|
"@types/babel-plugin-macros": "^2.8.5"
|
|
41
42
|
},
|
|
42
|
-
"gitHead": "
|
|
43
|
+
"gitHead": "76ef4e8d1c668578ce2c3829ebf35d22ca5e679c"
|
|
43
44
|
}
|