@lingui/macro 4.7.0 → 4.8.0-next.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/dist/index.cjs +129 -25
- package/dist/index.mjs +114 -23
- package/index.d.ts +34 -1
- package/package.json +5 -5
package/dist/index.cjs
CHANGED
|
@@ -2,9 +2,23 @@
|
|
|
2
2
|
|
|
3
3
|
const babelPluginMacros = require('babel-plugin-macros');
|
|
4
4
|
const conf = require('@lingui/conf');
|
|
5
|
-
const
|
|
5
|
+
const t = require('@babel/types');
|
|
6
6
|
const generateMessageId = require('@lingui/message-utils/generateMessageId');
|
|
7
7
|
|
|
8
|
+
function _interopNamespaceCompat(e) {
|
|
9
|
+
if (e && typeof e === 'object' && 'default' in e) return e;
|
|
10
|
+
const n = Object.create(null);
|
|
11
|
+
if (e) {
|
|
12
|
+
for (const k in e) {
|
|
13
|
+
n[k] = e[k];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
n.default = e;
|
|
17
|
+
return n;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const t__namespace = /*#__PURE__*/_interopNamespaceCompat(t);
|
|
21
|
+
|
|
8
22
|
const metaOptions = ["id", "comment", "props"];
|
|
9
23
|
const escapedMetaOptionsRe = new RegExp(`^_(${metaOptions.join("|")})$`);
|
|
10
24
|
class ICUMessageFormat {
|
|
@@ -30,7 +44,7 @@ class ICUMessageFormat {
|
|
|
30
44
|
message: token.value
|
|
31
45
|
};
|
|
32
46
|
} else if (token.type === "arg") {
|
|
33
|
-
if (token.value !== void 0 &&
|
|
47
|
+
if (token.value !== void 0 && t.isJSXEmptyExpression(token.value)) {
|
|
34
48
|
return null;
|
|
35
49
|
}
|
|
36
50
|
const values = token.value !== void 0 ? { [token.name]: token.value } : {};
|
|
@@ -124,6 +138,8 @@ class MacroJs {
|
|
|
124
138
|
__publicField$1(this, "stripNonEssentialProps");
|
|
125
139
|
__publicField$1(this, "nameMap");
|
|
126
140
|
__publicField$1(this, "nameMapReversed");
|
|
141
|
+
__publicField$1(this, "needsUseLinguiImport", false);
|
|
142
|
+
__publicField$1(this, "needsI18nImport", false);
|
|
127
143
|
// Positional expressions counter (e.g. for placeholders `Hello {0}, today is {1}`)
|
|
128
144
|
__publicField$1(this, "_expressionIndex", makeCounter());
|
|
129
145
|
__publicField$1(this, "replacePathWithMessage", (path, tokens, linguiInstance) => {
|
|
@@ -166,10 +182,36 @@ class MacroJs {
|
|
|
166
182
|
}
|
|
167
183
|
if (this.types.isCallExpression(path.node) && this.isLinguiIdentifier(path.node.callee, "t")) {
|
|
168
184
|
this.replaceTAsFunction(path);
|
|
185
|
+
this.needsI18nImport = true;
|
|
169
186
|
return true;
|
|
170
187
|
}
|
|
188
|
+
if (path.isTaggedTemplateExpression() && this.isLinguiIdentifier(path.node.tag, "_t")) {
|
|
189
|
+
this.needsUseLinguiImport = true;
|
|
190
|
+
const tokens2 = this.tokenizeTemplateLiteral(path.node);
|
|
191
|
+
const descriptor = this.createMessageDescriptorFromTokens(
|
|
192
|
+
tokens2,
|
|
193
|
+
path.node.loc
|
|
194
|
+
);
|
|
195
|
+
const callExpr = this.types.callExpression(
|
|
196
|
+
this.types.isIdentifier(path.node.tag) && path.node.tag,
|
|
197
|
+
[descriptor]
|
|
198
|
+
);
|
|
199
|
+
path.replaceWith(callExpr);
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
if (path.isCallExpression() && this.isLinguiIdentifier(path.node.callee, "_t") && this.types.isExpression(path.node.arguments[0])) {
|
|
203
|
+
this.needsUseLinguiImport = true;
|
|
204
|
+
let descriptor = this.processDescriptor(path.node.arguments[0]);
|
|
205
|
+
path.node.arguments = [descriptor];
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
208
|
+
if (this.types.isCallExpression(path.node) && this.isLinguiIdentifier(path.node.callee, "useLingui") && this.types.isVariableDeclarator(path.parentPath.node)) {
|
|
209
|
+
this.needsUseLinguiImport = true;
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
171
212
|
const tokens = this.tokenizeNode(path.node);
|
|
172
213
|
this.replacePathWithMessage(path, tokens);
|
|
214
|
+
this.needsI18nImport = true;
|
|
173
215
|
return true;
|
|
174
216
|
});
|
|
175
217
|
/**
|
|
@@ -380,7 +422,7 @@ class MacroJs {
|
|
|
380
422
|
}
|
|
381
423
|
getObjectPropertyByKey(objectExp, key) {
|
|
382
424
|
return objectExp.properties.find(
|
|
383
|
-
(property) =>
|
|
425
|
+
(property) => t.isObjectProperty(property) && this.isLinguiIdentifier(property.key, key)
|
|
384
426
|
);
|
|
385
427
|
}
|
|
386
428
|
/**
|
|
@@ -784,6 +826,7 @@ const jsMacroTags = /* @__PURE__ */ new Set([
|
|
|
784
826
|
"msg",
|
|
785
827
|
"arg",
|
|
786
828
|
"t",
|
|
829
|
+
"useLingui",
|
|
787
830
|
"plural",
|
|
788
831
|
"select",
|
|
789
832
|
"selectOrdinal"
|
|
@@ -801,22 +844,37 @@ function getConfig(_config) {
|
|
|
801
844
|
}
|
|
802
845
|
function macro({ references, state, babel, config: config2 }) {
|
|
803
846
|
const opts = config2;
|
|
847
|
+
const body = state.file.path.node.body;
|
|
804
848
|
const {
|
|
805
849
|
i18nImportModule,
|
|
806
850
|
i18nImportName,
|
|
807
851
|
TransImportModule,
|
|
808
|
-
TransImportName
|
|
852
|
+
TransImportName,
|
|
853
|
+
useLinguiImportModule,
|
|
854
|
+
useLinguiImportName
|
|
809
855
|
} = getConfig(opts.linguiConfig).runtimeConfigModule;
|
|
810
856
|
const jsxNodes = /* @__PURE__ */ new Set();
|
|
811
857
|
const jsNodes = /* @__PURE__ */ new Set();
|
|
812
858
|
let needsI18nImport = false;
|
|
859
|
+
let needsUseLinguiImport = false;
|
|
860
|
+
const uniq_tIdentifier = state.file.scope.generateUidIdentifier("_t");
|
|
813
861
|
let nameMap = /* @__PURE__ */ new Map();
|
|
814
862
|
Object.keys(references).forEach((tagName) => {
|
|
815
863
|
const nodes = references[tagName];
|
|
816
864
|
if (jsMacroTags.has(tagName)) {
|
|
817
865
|
nodes.forEach((path) => {
|
|
818
|
-
|
|
819
|
-
|
|
866
|
+
if (tagName !== "useLingui") {
|
|
867
|
+
nameMap.set(tagName, path.node.name);
|
|
868
|
+
jsNodes.add(path.parentPath);
|
|
869
|
+
} else {
|
|
870
|
+
needsUseLinguiImport = true;
|
|
871
|
+
nameMap.set("_t", uniq_tIdentifier.name);
|
|
872
|
+
processUseLingui(
|
|
873
|
+
path,
|
|
874
|
+
useLinguiImportName,
|
|
875
|
+
uniq_tIdentifier.name
|
|
876
|
+
)?.forEach((n) => jsNodes.add(n));
|
|
877
|
+
}
|
|
820
878
|
});
|
|
821
879
|
} else if (jsxMacroTags.has(tagName)) {
|
|
822
880
|
nodes.forEach((path) => {
|
|
@@ -836,8 +894,9 @@ function macro({ references, state, babel, config: config2 }) {
|
|
|
836
894
|
nameMap
|
|
837
895
|
});
|
|
838
896
|
try {
|
|
839
|
-
|
|
840
|
-
|
|
897
|
+
macro2.replacePath(path);
|
|
898
|
+
needsI18nImport = needsI18nImport || macro2.needsI18nImport;
|
|
899
|
+
needsUseLinguiImport = needsUseLinguiImport || macro2.needsUseLinguiImport;
|
|
841
900
|
} catch (e) {
|
|
842
901
|
reportUnsupportedSyntax(path, e);
|
|
843
902
|
}
|
|
@@ -851,39 +910,84 @@ function macro({ references, state, babel, config: config2 }) {
|
|
|
851
910
|
reportUnsupportedSyntax(path, e);
|
|
852
911
|
}
|
|
853
912
|
});
|
|
913
|
+
if (needsUseLinguiImport) {
|
|
914
|
+
addImport(babel, body, useLinguiImportModule, useLinguiImportName);
|
|
915
|
+
}
|
|
854
916
|
if (needsI18nImport) {
|
|
855
|
-
addImport(babel,
|
|
917
|
+
addImport(babel, body, i18nImportModule, i18nImportName);
|
|
856
918
|
}
|
|
857
919
|
if (jsxNodes.size) {
|
|
858
|
-
addImport(babel,
|
|
920
|
+
addImport(babel, body, TransImportModule, TransImportName);
|
|
859
921
|
}
|
|
860
922
|
}
|
|
861
923
|
function reportUnsupportedSyntax(path, e) {
|
|
862
924
|
throw path.buildCodeFrameError(
|
|
863
|
-
`Unsupported macro usage. Please check the examples at https://lingui.dev/ref/macro#examples-of-js-macros.
|
|
925
|
+
`Unsupported macro usage. Please check the examples at https://lingui.dev/ref/macro#examples-of-js-macros.
|
|
864
926
|
If you think this is a bug, fill in an issue at https://github.com/lingui/js-lingui/issues
|
|
865
|
-
|
|
927
|
+
|
|
866
928
|
Error: ${e.message}`
|
|
867
929
|
);
|
|
868
930
|
}
|
|
869
|
-
function
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
931
|
+
function processUseLingui(path, useLinguiName, newIdentifier) {
|
|
932
|
+
if (!path.parentPath.parentPath.isVariableDeclarator()) {
|
|
933
|
+
reportUnsupportedSyntax(
|
|
934
|
+
path,
|
|
935
|
+
new Error(
|
|
936
|
+
`\`useLingui\` macro must be used in variable declaration.
|
|
937
|
+
|
|
938
|
+
Example:
|
|
939
|
+
|
|
940
|
+
const { t } = useLingui()
|
|
941
|
+
`
|
|
942
|
+
)
|
|
943
|
+
);
|
|
944
|
+
return null;
|
|
945
|
+
}
|
|
946
|
+
const varDec = path.parentPath.parentPath.node;
|
|
947
|
+
const _property = t__namespace.isObjectPattern(varDec.id) ? varDec.id.properties.find(
|
|
948
|
+
(property) => t__namespace.isObjectProperty(property) && t__namespace.isIdentifier(property.key) && t__namespace.isIdentifier(property.value) && property.key.name == "t"
|
|
949
|
+
) : null;
|
|
950
|
+
if (!_property) {
|
|
951
|
+
reportUnsupportedSyntax(
|
|
952
|
+
path.parentPath.parentPath,
|
|
953
|
+
new Error(
|
|
954
|
+
`You have to destructure \`t\` when using the \`useLingui\` macro, i.e:
|
|
955
|
+
const { t } = useLingui()
|
|
956
|
+
or
|
|
957
|
+
const { t: _ } = useLingui()
|
|
958
|
+
`
|
|
959
|
+
)
|
|
960
|
+
);
|
|
961
|
+
return null;
|
|
962
|
+
}
|
|
963
|
+
if (t__namespace.isIdentifier(path.node)) {
|
|
964
|
+
path.scope.rename(path.node.name, useLinguiName);
|
|
965
|
+
}
|
|
966
|
+
_property.key.name = "_";
|
|
967
|
+
path.scope.rename(_property.value.name, newIdentifier);
|
|
968
|
+
return path.scope.getBinding(_property.value.name)?.referencePaths.filter(
|
|
969
|
+
// dont process array expression to allow use in dependency arrays
|
|
970
|
+
(path2) => !path2.parentPath.isArrayExpression()
|
|
971
|
+
).map((path2) => path2.parentPath);
|
|
972
|
+
}
|
|
973
|
+
function addImport(babel, body, module2, importName) {
|
|
974
|
+
const { types: t2 } = babel;
|
|
975
|
+
const linguiImport = body.find(
|
|
976
|
+
(importNode) => t2.isImportDeclaration(importNode) && importNode.source.value === module2 && // https://github.com/lingui/js-lingui/issues/777
|
|
873
977
|
importNode.importKind !== "type"
|
|
874
978
|
);
|
|
875
|
-
const tIdentifier =
|
|
979
|
+
const tIdentifier = t2.identifier(importName);
|
|
876
980
|
if (linguiImport) {
|
|
877
|
-
if (linguiImport.specifiers.
|
|
878
|
-
(specifier) =>
|
|
879
|
-
)
|
|
880
|
-
linguiImport.specifiers.push(
|
|
981
|
+
if (!linguiImport.specifiers.find(
|
|
982
|
+
(specifier) => t2.isImportSpecifier(specifier) && t2.isIdentifier(specifier.imported, { name: importName })
|
|
983
|
+
)) {
|
|
984
|
+
linguiImport.specifiers.push(t2.importSpecifier(tIdentifier, tIdentifier));
|
|
881
985
|
}
|
|
882
986
|
} else {
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
[
|
|
886
|
-
|
|
987
|
+
body.unshift(
|
|
988
|
+
t2.importDeclaration(
|
|
989
|
+
[t2.importSpecifier(tIdentifier, tIdentifier)],
|
|
990
|
+
t2.stringLiteral(module2)
|
|
887
991
|
)
|
|
888
992
|
);
|
|
889
993
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { createMacro } from 'babel-plugin-macros';
|
|
2
2
|
import { getConfig as getConfig$1 } from '@lingui/conf';
|
|
3
|
-
import
|
|
3
|
+
import * as t from '@babel/types';
|
|
4
|
+
import { isJSXEmptyExpression, isObjectProperty } from '@babel/types';
|
|
4
5
|
import { generateMessageId } from '@lingui/message-utils/generateMessageId';
|
|
5
6
|
|
|
6
7
|
const metaOptions = ["id", "comment", "props"];
|
|
@@ -122,6 +123,8 @@ class MacroJs {
|
|
|
122
123
|
__publicField$1(this, "stripNonEssentialProps");
|
|
123
124
|
__publicField$1(this, "nameMap");
|
|
124
125
|
__publicField$1(this, "nameMapReversed");
|
|
126
|
+
__publicField$1(this, "needsUseLinguiImport", false);
|
|
127
|
+
__publicField$1(this, "needsI18nImport", false);
|
|
125
128
|
// Positional expressions counter (e.g. for placeholders `Hello {0}, today is {1}`)
|
|
126
129
|
__publicField$1(this, "_expressionIndex", makeCounter());
|
|
127
130
|
__publicField$1(this, "replacePathWithMessage", (path, tokens, linguiInstance) => {
|
|
@@ -164,10 +167,36 @@ class MacroJs {
|
|
|
164
167
|
}
|
|
165
168
|
if (this.types.isCallExpression(path.node) && this.isLinguiIdentifier(path.node.callee, "t")) {
|
|
166
169
|
this.replaceTAsFunction(path);
|
|
170
|
+
this.needsI18nImport = true;
|
|
167
171
|
return true;
|
|
168
172
|
}
|
|
173
|
+
if (path.isTaggedTemplateExpression() && this.isLinguiIdentifier(path.node.tag, "_t")) {
|
|
174
|
+
this.needsUseLinguiImport = true;
|
|
175
|
+
const tokens2 = this.tokenizeTemplateLiteral(path.node);
|
|
176
|
+
const descriptor = this.createMessageDescriptorFromTokens(
|
|
177
|
+
tokens2,
|
|
178
|
+
path.node.loc
|
|
179
|
+
);
|
|
180
|
+
const callExpr = this.types.callExpression(
|
|
181
|
+
this.types.isIdentifier(path.node.tag) && path.node.tag,
|
|
182
|
+
[descriptor]
|
|
183
|
+
);
|
|
184
|
+
path.replaceWith(callExpr);
|
|
185
|
+
return false;
|
|
186
|
+
}
|
|
187
|
+
if (path.isCallExpression() && this.isLinguiIdentifier(path.node.callee, "_t") && this.types.isExpression(path.node.arguments[0])) {
|
|
188
|
+
this.needsUseLinguiImport = true;
|
|
189
|
+
let descriptor = this.processDescriptor(path.node.arguments[0]);
|
|
190
|
+
path.node.arguments = [descriptor];
|
|
191
|
+
return false;
|
|
192
|
+
}
|
|
193
|
+
if (this.types.isCallExpression(path.node) && this.isLinguiIdentifier(path.node.callee, "useLingui") && this.types.isVariableDeclarator(path.parentPath.node)) {
|
|
194
|
+
this.needsUseLinguiImport = true;
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
169
197
|
const tokens = this.tokenizeNode(path.node);
|
|
170
198
|
this.replacePathWithMessage(path, tokens);
|
|
199
|
+
this.needsI18nImport = true;
|
|
171
200
|
return true;
|
|
172
201
|
});
|
|
173
202
|
/**
|
|
@@ -782,6 +811,7 @@ const jsMacroTags = /* @__PURE__ */ new Set([
|
|
|
782
811
|
"msg",
|
|
783
812
|
"arg",
|
|
784
813
|
"t",
|
|
814
|
+
"useLingui",
|
|
785
815
|
"plural",
|
|
786
816
|
"select",
|
|
787
817
|
"selectOrdinal"
|
|
@@ -799,22 +829,37 @@ function getConfig(_config) {
|
|
|
799
829
|
}
|
|
800
830
|
function macro({ references, state, babel, config: config2 }) {
|
|
801
831
|
const opts = config2;
|
|
832
|
+
const body = state.file.path.node.body;
|
|
802
833
|
const {
|
|
803
834
|
i18nImportModule,
|
|
804
835
|
i18nImportName,
|
|
805
836
|
TransImportModule,
|
|
806
|
-
TransImportName
|
|
837
|
+
TransImportName,
|
|
838
|
+
useLinguiImportModule,
|
|
839
|
+
useLinguiImportName
|
|
807
840
|
} = getConfig(opts.linguiConfig).runtimeConfigModule;
|
|
808
841
|
const jsxNodes = /* @__PURE__ */ new Set();
|
|
809
842
|
const jsNodes = /* @__PURE__ */ new Set();
|
|
810
843
|
let needsI18nImport = false;
|
|
844
|
+
let needsUseLinguiImport = false;
|
|
845
|
+
const uniq_tIdentifier = state.file.scope.generateUidIdentifier("_t");
|
|
811
846
|
let nameMap = /* @__PURE__ */ new Map();
|
|
812
847
|
Object.keys(references).forEach((tagName) => {
|
|
813
848
|
const nodes = references[tagName];
|
|
814
849
|
if (jsMacroTags.has(tagName)) {
|
|
815
850
|
nodes.forEach((path) => {
|
|
816
|
-
|
|
817
|
-
|
|
851
|
+
if (tagName !== "useLingui") {
|
|
852
|
+
nameMap.set(tagName, path.node.name);
|
|
853
|
+
jsNodes.add(path.parentPath);
|
|
854
|
+
} else {
|
|
855
|
+
needsUseLinguiImport = true;
|
|
856
|
+
nameMap.set("_t", uniq_tIdentifier.name);
|
|
857
|
+
processUseLingui(
|
|
858
|
+
path,
|
|
859
|
+
useLinguiImportName,
|
|
860
|
+
uniq_tIdentifier.name
|
|
861
|
+
)?.forEach((n) => jsNodes.add(n));
|
|
862
|
+
}
|
|
818
863
|
});
|
|
819
864
|
} else if (jsxMacroTags.has(tagName)) {
|
|
820
865
|
nodes.forEach((path) => {
|
|
@@ -834,8 +879,9 @@ function macro({ references, state, babel, config: config2 }) {
|
|
|
834
879
|
nameMap
|
|
835
880
|
});
|
|
836
881
|
try {
|
|
837
|
-
|
|
838
|
-
|
|
882
|
+
macro2.replacePath(path);
|
|
883
|
+
needsI18nImport = needsI18nImport || macro2.needsI18nImport;
|
|
884
|
+
needsUseLinguiImport = needsUseLinguiImport || macro2.needsUseLinguiImport;
|
|
839
885
|
} catch (e) {
|
|
840
886
|
reportUnsupportedSyntax(path, e);
|
|
841
887
|
}
|
|
@@ -849,39 +895,84 @@ function macro({ references, state, babel, config: config2 }) {
|
|
|
849
895
|
reportUnsupportedSyntax(path, e);
|
|
850
896
|
}
|
|
851
897
|
});
|
|
898
|
+
if (needsUseLinguiImport) {
|
|
899
|
+
addImport(babel, body, useLinguiImportModule, useLinguiImportName);
|
|
900
|
+
}
|
|
852
901
|
if (needsI18nImport) {
|
|
853
|
-
addImport(babel,
|
|
902
|
+
addImport(babel, body, i18nImportModule, i18nImportName);
|
|
854
903
|
}
|
|
855
904
|
if (jsxNodes.size) {
|
|
856
|
-
addImport(babel,
|
|
905
|
+
addImport(babel, body, TransImportModule, TransImportName);
|
|
857
906
|
}
|
|
858
907
|
}
|
|
859
908
|
function reportUnsupportedSyntax(path, e) {
|
|
860
909
|
throw path.buildCodeFrameError(
|
|
861
|
-
`Unsupported macro usage. Please check the examples at https://lingui.dev/ref/macro#examples-of-js-macros.
|
|
910
|
+
`Unsupported macro usage. Please check the examples at https://lingui.dev/ref/macro#examples-of-js-macros.
|
|
862
911
|
If you think this is a bug, fill in an issue at https://github.com/lingui/js-lingui/issues
|
|
863
|
-
|
|
912
|
+
|
|
864
913
|
Error: ${e.message}`
|
|
865
914
|
);
|
|
866
915
|
}
|
|
867
|
-
function
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
916
|
+
function processUseLingui(path, useLinguiName, newIdentifier) {
|
|
917
|
+
if (!path.parentPath.parentPath.isVariableDeclarator()) {
|
|
918
|
+
reportUnsupportedSyntax(
|
|
919
|
+
path,
|
|
920
|
+
new Error(
|
|
921
|
+
`\`useLingui\` macro must be used in variable declaration.
|
|
922
|
+
|
|
923
|
+
Example:
|
|
924
|
+
|
|
925
|
+
const { t } = useLingui()
|
|
926
|
+
`
|
|
927
|
+
)
|
|
928
|
+
);
|
|
929
|
+
return null;
|
|
930
|
+
}
|
|
931
|
+
const varDec = path.parentPath.parentPath.node;
|
|
932
|
+
const _property = t.isObjectPattern(varDec.id) ? varDec.id.properties.find(
|
|
933
|
+
(property) => t.isObjectProperty(property) && t.isIdentifier(property.key) && t.isIdentifier(property.value) && property.key.name == "t"
|
|
934
|
+
) : null;
|
|
935
|
+
if (!_property) {
|
|
936
|
+
reportUnsupportedSyntax(
|
|
937
|
+
path.parentPath.parentPath,
|
|
938
|
+
new Error(
|
|
939
|
+
`You have to destructure \`t\` when using the \`useLingui\` macro, i.e:
|
|
940
|
+
const { t } = useLingui()
|
|
941
|
+
or
|
|
942
|
+
const { t: _ } = useLingui()
|
|
943
|
+
`
|
|
944
|
+
)
|
|
945
|
+
);
|
|
946
|
+
return null;
|
|
947
|
+
}
|
|
948
|
+
if (t.isIdentifier(path.node)) {
|
|
949
|
+
path.scope.rename(path.node.name, useLinguiName);
|
|
950
|
+
}
|
|
951
|
+
_property.key.name = "_";
|
|
952
|
+
path.scope.rename(_property.value.name, newIdentifier);
|
|
953
|
+
return path.scope.getBinding(_property.value.name)?.referencePaths.filter(
|
|
954
|
+
// dont process array expression to allow use in dependency arrays
|
|
955
|
+
(path2) => !path2.parentPath.isArrayExpression()
|
|
956
|
+
).map((path2) => path2.parentPath);
|
|
957
|
+
}
|
|
958
|
+
function addImport(babel, body, module2, importName) {
|
|
959
|
+
const { types: t2 } = babel;
|
|
960
|
+
const linguiImport = body.find(
|
|
961
|
+
(importNode) => t2.isImportDeclaration(importNode) && importNode.source.value === module2 && // https://github.com/lingui/js-lingui/issues/777
|
|
871
962
|
importNode.importKind !== "type"
|
|
872
963
|
);
|
|
873
|
-
const tIdentifier =
|
|
964
|
+
const tIdentifier = t2.identifier(importName);
|
|
874
965
|
if (linguiImport) {
|
|
875
|
-
if (linguiImport.specifiers.
|
|
876
|
-
(specifier) => isImportSpecifier(specifier) && isIdentifier(specifier.imported, { name: importName })
|
|
877
|
-
)
|
|
878
|
-
linguiImport.specifiers.push(
|
|
966
|
+
if (!linguiImport.specifiers.find(
|
|
967
|
+
(specifier) => t2.isImportSpecifier(specifier) && t2.isIdentifier(specifier.imported, { name: importName })
|
|
968
|
+
)) {
|
|
969
|
+
linguiImport.specifiers.push(t2.importSpecifier(tIdentifier, tIdentifier));
|
|
879
970
|
}
|
|
880
971
|
} else {
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
[
|
|
884
|
-
|
|
972
|
+
body.unshift(
|
|
973
|
+
t2.importDeclaration(
|
|
974
|
+
[t2.importSpecifier(tIdentifier, tIdentifier)],
|
|
975
|
+
t2.stringLiteral(module2)
|
|
885
976
|
)
|
|
886
977
|
);
|
|
887
978
|
}
|
package/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
2
2
|
import type { ReactNode, VFC, FC } from "react"
|
|
3
3
|
import type { I18n, MessageDescriptor } from "@lingui/core"
|
|
4
|
-
import type { TransRenderCallbackOrComponent } from "@lingui/react"
|
|
4
|
+
import type { TransRenderCallbackOrComponent, I18nContext } from "@lingui/react"
|
|
5
5
|
|
|
6
6
|
export type ChoiceOptions = {
|
|
7
7
|
/** Offset of value when calculating plural forms */
|
|
@@ -316,3 +316,36 @@ export const SelectOrdinal: VFC<PluralChoiceProps>
|
|
|
316
316
|
* ```
|
|
317
317
|
*/
|
|
318
318
|
export const Select: VFC<SelectChoiceProps>
|
|
319
|
+
|
|
320
|
+
export function _t(descriptor: MacroMessageDescriptor): string
|
|
321
|
+
export function _t(
|
|
322
|
+
literals: TemplateStringsArray,
|
|
323
|
+
...placeholders: any[]
|
|
324
|
+
): string
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
*
|
|
328
|
+
* Macro version of useLingui replaces _ function with `t` macro function which is bound to i18n passed from React.Context
|
|
329
|
+
*
|
|
330
|
+
* Returned `t` macro function has all the same signatures as global `t`
|
|
331
|
+
*
|
|
332
|
+
* @example
|
|
333
|
+
* ```
|
|
334
|
+
* const { t } = useLingui();
|
|
335
|
+
* const message = t`Text`;
|
|
336
|
+
* ```
|
|
337
|
+
*
|
|
338
|
+
* @example
|
|
339
|
+
* ```
|
|
340
|
+
* const { i18n, t } = useLingui();
|
|
341
|
+
* const locale = i18n.locale;
|
|
342
|
+
* const message = t({
|
|
343
|
+
* id: "msg.hello",
|
|
344
|
+
* comment: "Greetings at the homepage",
|
|
345
|
+
* message: `Hello ${name}`,
|
|
346
|
+
* });
|
|
347
|
+
* ```
|
|
348
|
+
*/
|
|
349
|
+
export function useLingui(): Omit<I18nContext, "_"> & {
|
|
350
|
+
t: typeof _t
|
|
351
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lingui/macro",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.8.0-next.0",
|
|
4
4
|
"description": "Macro for generating messages in ICU MessageFormat syntax",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -65,9 +65,9 @@
|
|
|
65
65
|
"dependencies": {
|
|
66
66
|
"@babel/runtime": "^7.20.13",
|
|
67
67
|
"@babel/types": "^7.20.7",
|
|
68
|
-
"@lingui/conf": "4.
|
|
69
|
-
"@lingui/core": "4.
|
|
70
|
-
"@lingui/message-utils": "4.
|
|
68
|
+
"@lingui/conf": "^4.8.0-next.0",
|
|
69
|
+
"@lingui/core": "^4.8.0-next.0",
|
|
70
|
+
"@lingui/message-utils": "^4.8.0-next.0"
|
|
71
71
|
},
|
|
72
72
|
"peerDependencies": {
|
|
73
73
|
"@lingui/react": "^4.0.0",
|
|
@@ -82,5 +82,5 @@
|
|
|
82
82
|
"tsd": "^0.26.1",
|
|
83
83
|
"unbuild": "2.0.0"
|
|
84
84
|
},
|
|
85
|
-
"gitHead": "
|
|
85
|
+
"gitHead": "d085ac65d66ce6c2f866b703b6346e53ff0bf6b3"
|
|
86
86
|
}
|