@lingui/macro 4.8.0-next.0 → 4.8.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 +25 -129
- package/dist/index.mjs +23 -114
- package/index.d.ts +1 -34
- package/package.json +5 -5
package/dist/index.cjs
CHANGED
|
@@ -2,23 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
const babelPluginMacros = require('babel-plugin-macros');
|
|
4
4
|
const conf = require('@lingui/conf');
|
|
5
|
-
const
|
|
5
|
+
const types = 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
|
-
|
|
22
8
|
const metaOptions = ["id", "comment", "props"];
|
|
23
9
|
const escapedMetaOptionsRe = new RegExp(`^_(${metaOptions.join("|")})$`);
|
|
24
10
|
class ICUMessageFormat {
|
|
@@ -44,7 +30,7 @@ class ICUMessageFormat {
|
|
|
44
30
|
message: token.value
|
|
45
31
|
};
|
|
46
32
|
} else if (token.type === "arg") {
|
|
47
|
-
if (token.value !== void 0 &&
|
|
33
|
+
if (token.value !== void 0 && types.isJSXEmptyExpression(token.value)) {
|
|
48
34
|
return null;
|
|
49
35
|
}
|
|
50
36
|
const values = token.value !== void 0 ? { [token.name]: token.value } : {};
|
|
@@ -138,8 +124,6 @@ class MacroJs {
|
|
|
138
124
|
__publicField$1(this, "stripNonEssentialProps");
|
|
139
125
|
__publicField$1(this, "nameMap");
|
|
140
126
|
__publicField$1(this, "nameMapReversed");
|
|
141
|
-
__publicField$1(this, "needsUseLinguiImport", false);
|
|
142
|
-
__publicField$1(this, "needsI18nImport", false);
|
|
143
127
|
// Positional expressions counter (e.g. for placeholders `Hello {0}, today is {1}`)
|
|
144
128
|
__publicField$1(this, "_expressionIndex", makeCounter());
|
|
145
129
|
__publicField$1(this, "replacePathWithMessage", (path, tokens, linguiInstance) => {
|
|
@@ -182,36 +166,10 @@ class MacroJs {
|
|
|
182
166
|
}
|
|
183
167
|
if (this.types.isCallExpression(path.node) && this.isLinguiIdentifier(path.node.callee, "t")) {
|
|
184
168
|
this.replaceTAsFunction(path);
|
|
185
|
-
this.needsI18nImport = true;
|
|
186
169
|
return true;
|
|
187
170
|
}
|
|
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
|
-
}
|
|
212
171
|
const tokens = this.tokenizeNode(path.node);
|
|
213
172
|
this.replacePathWithMessage(path, tokens);
|
|
214
|
-
this.needsI18nImport = true;
|
|
215
173
|
return true;
|
|
216
174
|
});
|
|
217
175
|
/**
|
|
@@ -422,7 +380,7 @@ class MacroJs {
|
|
|
422
380
|
}
|
|
423
381
|
getObjectPropertyByKey(objectExp, key) {
|
|
424
382
|
return objectExp.properties.find(
|
|
425
|
-
(property) =>
|
|
383
|
+
(property) => types.isObjectProperty(property) && this.isLinguiIdentifier(property.key, key)
|
|
426
384
|
);
|
|
427
385
|
}
|
|
428
386
|
/**
|
|
@@ -826,7 +784,6 @@ const jsMacroTags = /* @__PURE__ */ new Set([
|
|
|
826
784
|
"msg",
|
|
827
785
|
"arg",
|
|
828
786
|
"t",
|
|
829
|
-
"useLingui",
|
|
830
787
|
"plural",
|
|
831
788
|
"select",
|
|
832
789
|
"selectOrdinal"
|
|
@@ -844,37 +801,22 @@ function getConfig(_config) {
|
|
|
844
801
|
}
|
|
845
802
|
function macro({ references, state, babel, config: config2 }) {
|
|
846
803
|
const opts = config2;
|
|
847
|
-
const body = state.file.path.node.body;
|
|
848
804
|
const {
|
|
849
805
|
i18nImportModule,
|
|
850
806
|
i18nImportName,
|
|
851
807
|
TransImportModule,
|
|
852
|
-
TransImportName
|
|
853
|
-
useLinguiImportModule,
|
|
854
|
-
useLinguiImportName
|
|
808
|
+
TransImportName
|
|
855
809
|
} = getConfig(opts.linguiConfig).runtimeConfigModule;
|
|
856
810
|
const jsxNodes = /* @__PURE__ */ new Set();
|
|
857
811
|
const jsNodes = /* @__PURE__ */ new Set();
|
|
858
812
|
let needsI18nImport = false;
|
|
859
|
-
let needsUseLinguiImport = false;
|
|
860
|
-
const uniq_tIdentifier = state.file.scope.generateUidIdentifier("_t");
|
|
861
813
|
let nameMap = /* @__PURE__ */ new Map();
|
|
862
814
|
Object.keys(references).forEach((tagName) => {
|
|
863
815
|
const nodes = references[tagName];
|
|
864
816
|
if (jsMacroTags.has(tagName)) {
|
|
865
817
|
nodes.forEach((path) => {
|
|
866
|
-
|
|
867
|
-
|
|
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
|
-
}
|
|
818
|
+
nameMap.set(tagName, path.node.name);
|
|
819
|
+
jsNodes.add(path.parentPath);
|
|
878
820
|
});
|
|
879
821
|
} else if (jsxMacroTags.has(tagName)) {
|
|
880
822
|
nodes.forEach((path) => {
|
|
@@ -894,9 +836,8 @@ function macro({ references, state, babel, config: config2 }) {
|
|
|
894
836
|
nameMap
|
|
895
837
|
});
|
|
896
838
|
try {
|
|
897
|
-
macro2.replacePath(path)
|
|
898
|
-
|
|
899
|
-
needsUseLinguiImport = needsUseLinguiImport || macro2.needsUseLinguiImport;
|
|
839
|
+
if (macro2.replacePath(path))
|
|
840
|
+
needsI18nImport = true;
|
|
900
841
|
} catch (e) {
|
|
901
842
|
reportUnsupportedSyntax(path, e);
|
|
902
843
|
}
|
|
@@ -910,84 +851,39 @@ function macro({ references, state, babel, config: config2 }) {
|
|
|
910
851
|
reportUnsupportedSyntax(path, e);
|
|
911
852
|
}
|
|
912
853
|
});
|
|
913
|
-
if (needsUseLinguiImport) {
|
|
914
|
-
addImport(babel, body, useLinguiImportModule, useLinguiImportName);
|
|
915
|
-
}
|
|
916
854
|
if (needsI18nImport) {
|
|
917
|
-
addImport(babel,
|
|
855
|
+
addImport(babel, state, i18nImportModule, i18nImportName);
|
|
918
856
|
}
|
|
919
857
|
if (jsxNodes.size) {
|
|
920
|
-
addImport(babel,
|
|
858
|
+
addImport(babel, state, TransImportModule, TransImportName);
|
|
921
859
|
}
|
|
922
860
|
}
|
|
923
861
|
function reportUnsupportedSyntax(path, e) {
|
|
924
862
|
throw path.buildCodeFrameError(
|
|
925
|
-
`Unsupported macro usage. Please check the examples at https://lingui.dev/ref/macro#examples-of-js-macros.
|
|
863
|
+
`Unsupported macro usage. Please check the examples at https://lingui.dev/ref/macro#examples-of-js-macros.
|
|
926
864
|
If you think this is a bug, fill in an issue at https://github.com/lingui/js-lingui/issues
|
|
927
|
-
|
|
865
|
+
|
|
928
866
|
Error: ${e.message}`
|
|
929
867
|
);
|
|
930
868
|
}
|
|
931
|
-
function
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
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
|
|
869
|
+
function addImport(babel, state, module2, importName) {
|
|
870
|
+
const { types: t } = babel;
|
|
871
|
+
const linguiImport = state.file.path.node.body.find(
|
|
872
|
+
(importNode) => t.isImportDeclaration(importNode) && importNode.source.value === module2 && // https://github.com/lingui/js-lingui/issues/777
|
|
977
873
|
importNode.importKind !== "type"
|
|
978
874
|
);
|
|
979
|
-
const tIdentifier =
|
|
875
|
+
const tIdentifier = t.identifier(importName);
|
|
980
876
|
if (linguiImport) {
|
|
981
|
-
if (
|
|
982
|
-
(specifier) =>
|
|
983
|
-
)) {
|
|
984
|
-
linguiImport.specifiers.push(
|
|
877
|
+
if (linguiImport.specifiers.findIndex(
|
|
878
|
+
(specifier) => types.isImportSpecifier(specifier) && types.isIdentifier(specifier.imported, { name: importName })
|
|
879
|
+
) === -1) {
|
|
880
|
+
linguiImport.specifiers.push(t.importSpecifier(tIdentifier, tIdentifier));
|
|
985
881
|
}
|
|
986
882
|
} else {
|
|
987
|
-
body.unshift(
|
|
988
|
-
|
|
989
|
-
[
|
|
990
|
-
|
|
883
|
+
state.file.path.node.body.unshift(
|
|
884
|
+
t.importDeclaration(
|
|
885
|
+
[t.importSpecifier(tIdentifier, tIdentifier)],
|
|
886
|
+
t.stringLiteral(module2)
|
|
991
887
|
)
|
|
992
888
|
);
|
|
993
889
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { createMacro } from 'babel-plugin-macros';
|
|
2
2
|
import { getConfig as getConfig$1 } from '@lingui/conf';
|
|
3
|
-
import
|
|
4
|
-
import { isJSXEmptyExpression, isObjectProperty } from '@babel/types';
|
|
3
|
+
import { isJSXEmptyExpression, isObjectProperty, isImportSpecifier, isIdentifier } from '@babel/types';
|
|
5
4
|
import { generateMessageId } from '@lingui/message-utils/generateMessageId';
|
|
6
5
|
|
|
7
6
|
const metaOptions = ["id", "comment", "props"];
|
|
@@ -123,8 +122,6 @@ class MacroJs {
|
|
|
123
122
|
__publicField$1(this, "stripNonEssentialProps");
|
|
124
123
|
__publicField$1(this, "nameMap");
|
|
125
124
|
__publicField$1(this, "nameMapReversed");
|
|
126
|
-
__publicField$1(this, "needsUseLinguiImport", false);
|
|
127
|
-
__publicField$1(this, "needsI18nImport", false);
|
|
128
125
|
// Positional expressions counter (e.g. for placeholders `Hello {0}, today is {1}`)
|
|
129
126
|
__publicField$1(this, "_expressionIndex", makeCounter());
|
|
130
127
|
__publicField$1(this, "replacePathWithMessage", (path, tokens, linguiInstance) => {
|
|
@@ -167,36 +164,10 @@ class MacroJs {
|
|
|
167
164
|
}
|
|
168
165
|
if (this.types.isCallExpression(path.node) && this.isLinguiIdentifier(path.node.callee, "t")) {
|
|
169
166
|
this.replaceTAsFunction(path);
|
|
170
|
-
this.needsI18nImport = true;
|
|
171
167
|
return true;
|
|
172
168
|
}
|
|
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
|
-
}
|
|
197
169
|
const tokens = this.tokenizeNode(path.node);
|
|
198
170
|
this.replacePathWithMessage(path, tokens);
|
|
199
|
-
this.needsI18nImport = true;
|
|
200
171
|
return true;
|
|
201
172
|
});
|
|
202
173
|
/**
|
|
@@ -811,7 +782,6 @@ const jsMacroTags = /* @__PURE__ */ new Set([
|
|
|
811
782
|
"msg",
|
|
812
783
|
"arg",
|
|
813
784
|
"t",
|
|
814
|
-
"useLingui",
|
|
815
785
|
"plural",
|
|
816
786
|
"select",
|
|
817
787
|
"selectOrdinal"
|
|
@@ -829,37 +799,22 @@ function getConfig(_config) {
|
|
|
829
799
|
}
|
|
830
800
|
function macro({ references, state, babel, config: config2 }) {
|
|
831
801
|
const opts = config2;
|
|
832
|
-
const body = state.file.path.node.body;
|
|
833
802
|
const {
|
|
834
803
|
i18nImportModule,
|
|
835
804
|
i18nImportName,
|
|
836
805
|
TransImportModule,
|
|
837
|
-
TransImportName
|
|
838
|
-
useLinguiImportModule,
|
|
839
|
-
useLinguiImportName
|
|
806
|
+
TransImportName
|
|
840
807
|
} = getConfig(opts.linguiConfig).runtimeConfigModule;
|
|
841
808
|
const jsxNodes = /* @__PURE__ */ new Set();
|
|
842
809
|
const jsNodes = /* @__PURE__ */ new Set();
|
|
843
810
|
let needsI18nImport = false;
|
|
844
|
-
let needsUseLinguiImport = false;
|
|
845
|
-
const uniq_tIdentifier = state.file.scope.generateUidIdentifier("_t");
|
|
846
811
|
let nameMap = /* @__PURE__ */ new Map();
|
|
847
812
|
Object.keys(references).forEach((tagName) => {
|
|
848
813
|
const nodes = references[tagName];
|
|
849
814
|
if (jsMacroTags.has(tagName)) {
|
|
850
815
|
nodes.forEach((path) => {
|
|
851
|
-
|
|
852
|
-
|
|
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
|
-
}
|
|
816
|
+
nameMap.set(tagName, path.node.name);
|
|
817
|
+
jsNodes.add(path.parentPath);
|
|
863
818
|
});
|
|
864
819
|
} else if (jsxMacroTags.has(tagName)) {
|
|
865
820
|
nodes.forEach((path) => {
|
|
@@ -879,9 +834,8 @@ function macro({ references, state, babel, config: config2 }) {
|
|
|
879
834
|
nameMap
|
|
880
835
|
});
|
|
881
836
|
try {
|
|
882
|
-
macro2.replacePath(path)
|
|
883
|
-
|
|
884
|
-
needsUseLinguiImport = needsUseLinguiImport || macro2.needsUseLinguiImport;
|
|
837
|
+
if (macro2.replacePath(path))
|
|
838
|
+
needsI18nImport = true;
|
|
885
839
|
} catch (e) {
|
|
886
840
|
reportUnsupportedSyntax(path, e);
|
|
887
841
|
}
|
|
@@ -895,84 +849,39 @@ function macro({ references, state, babel, config: config2 }) {
|
|
|
895
849
|
reportUnsupportedSyntax(path, e);
|
|
896
850
|
}
|
|
897
851
|
});
|
|
898
|
-
if (needsUseLinguiImport) {
|
|
899
|
-
addImport(babel, body, useLinguiImportModule, useLinguiImportName);
|
|
900
|
-
}
|
|
901
852
|
if (needsI18nImport) {
|
|
902
|
-
addImport(babel,
|
|
853
|
+
addImport(babel, state, i18nImportModule, i18nImportName);
|
|
903
854
|
}
|
|
904
855
|
if (jsxNodes.size) {
|
|
905
|
-
addImport(babel,
|
|
856
|
+
addImport(babel, state, TransImportModule, TransImportName);
|
|
906
857
|
}
|
|
907
858
|
}
|
|
908
859
|
function reportUnsupportedSyntax(path, e) {
|
|
909
860
|
throw path.buildCodeFrameError(
|
|
910
|
-
`Unsupported macro usage. Please check the examples at https://lingui.dev/ref/macro#examples-of-js-macros.
|
|
861
|
+
`Unsupported macro usage. Please check the examples at https://lingui.dev/ref/macro#examples-of-js-macros.
|
|
911
862
|
If you think this is a bug, fill in an issue at https://github.com/lingui/js-lingui/issues
|
|
912
|
-
|
|
863
|
+
|
|
913
864
|
Error: ${e.message}`
|
|
914
865
|
);
|
|
915
866
|
}
|
|
916
|
-
function
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
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
|
|
867
|
+
function addImport(babel, state, module2, importName) {
|
|
868
|
+
const { types: t } = babel;
|
|
869
|
+
const linguiImport = state.file.path.node.body.find(
|
|
870
|
+
(importNode) => t.isImportDeclaration(importNode) && importNode.source.value === module2 && // https://github.com/lingui/js-lingui/issues/777
|
|
962
871
|
importNode.importKind !== "type"
|
|
963
872
|
);
|
|
964
|
-
const tIdentifier =
|
|
873
|
+
const tIdentifier = t.identifier(importName);
|
|
965
874
|
if (linguiImport) {
|
|
966
|
-
if (
|
|
967
|
-
(specifier) =>
|
|
968
|
-
)) {
|
|
969
|
-
linguiImport.specifiers.push(
|
|
875
|
+
if (linguiImport.specifiers.findIndex(
|
|
876
|
+
(specifier) => isImportSpecifier(specifier) && isIdentifier(specifier.imported, { name: importName })
|
|
877
|
+
) === -1) {
|
|
878
|
+
linguiImport.specifiers.push(t.importSpecifier(tIdentifier, tIdentifier));
|
|
970
879
|
}
|
|
971
880
|
} else {
|
|
972
|
-
body.unshift(
|
|
973
|
-
|
|
974
|
-
[
|
|
975
|
-
|
|
881
|
+
state.file.path.node.body.unshift(
|
|
882
|
+
t.importDeclaration(
|
|
883
|
+
[t.importSpecifier(tIdentifier, tIdentifier)],
|
|
884
|
+
t.stringLiteral(module2)
|
|
976
885
|
)
|
|
977
886
|
);
|
|
978
887
|
}
|
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
|
|
4
|
+
import type { TransRenderCallbackOrComponent } from "@lingui/react"
|
|
5
5
|
|
|
6
6
|
export type ChoiceOptions = {
|
|
7
7
|
/** Offset of value when calculating plural forms */
|
|
@@ -316,36 +316,3 @@ 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.8.0
|
|
3
|
+
"version": "4.8.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": "
|
|
69
|
-
"@lingui/core": "
|
|
70
|
-
"@lingui/message-utils": "
|
|
68
|
+
"@lingui/conf": "4.8.0",
|
|
69
|
+
"@lingui/core": "4.8.0",
|
|
70
|
+
"@lingui/message-utils": "4.8.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": "1b3a1209b06e9a6b6820ce54064da87052c21fa5"
|
|
86
86
|
}
|