@lingui/babel-plugin-extract-messages 4.0.0-next.0 → 4.0.0-next.2

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.
Files changed (2) hide show
  1. package/build/index.js +8 -19
  2. package/package.json +2 -2
package/build/index.js CHANGED
@@ -18,12 +18,12 @@ function collectMessage(path, props, ctx) {
18
18
  origin: [ctx.file.opts.filename, line, column]
19
19
  });
20
20
  }
21
- function getTextFromExpression(t, exp, hub) {
21
+ function getTextFromExpression(t, exp, hub, emitErrorOnVariable = true) {
22
22
  if (t.isStringLiteral(exp)) {
23
23
  return exp.value;
24
24
  }
25
25
  if (t.isBinaryExpression(exp)) {
26
- return getTextFromExpression(t, exp.left, hub) + getTextFromExpression(t, exp.right, hub);
26
+ return getTextFromExpression(t, exp.left, hub, emitErrorOnVariable) + getTextFromExpression(t, exp.right, hub, emitErrorOnVariable);
27
27
  }
28
28
  if (t.isTemplateLiteral(exp)) {
29
29
  var _exp$quasis$, _exp$quasis$$value;
@@ -33,7 +33,9 @@ function getTextFromExpression(t, exp, hub) {
33
33
  }
34
34
  return (_exp$quasis$ = exp.quasis[0]) === null || _exp$quasis$ === void 0 ? void 0 : (_exp$quasis$$value = _exp$quasis$.value) === null || _exp$quasis$$value === void 0 ? void 0 : _exp$quasis$$value.cooked;
35
35
  }
36
- console.warn(hub.buildError(exp, "Only strings or template literals could be extracted.", SyntaxError).message);
36
+ if (emitErrorOnVariable) {
37
+ console.warn(hub.buildError(exp, "Only strings or template literals could be extracted.", SyntaxError).message);
38
+ }
37
39
  }
38
40
  function extractFromObjectExpression(t, exp, hub, keys) {
39
41
  const props = {};
@@ -51,11 +53,6 @@ function _default({
51
53
  types: t
52
54
  }) {
53
55
  let localTransComponentName;
54
-
55
- // We need to remember all processed nodes. When JSX expressions are
56
- // replaced with CallExpressions, all children are traversed for each CallExpression.
57
- // Then, i18n._ methods are visited multiple times for each parent CallExpression.
58
- const visitedNodes = new WeakSet();
59
56
  function isTransComponent(node) {
60
57
  return t.isJSXElement(node) && t.isJSXIdentifier(node.openingElement.name, {
61
58
  name: localTransComponentName
@@ -121,7 +118,6 @@ function _default({
121
118
  collectMessage(path, props, ctx);
122
119
  },
123
120
  CallExpression(path, ctx) {
124
- if (visitedNodes.has(path.node)) return;
125
121
  const hasComment = [path.node, path.parent].some(node => hasI18nComment(node));
126
122
  const firstArgument = path.node.arguments[0];
127
123
 
@@ -132,13 +128,9 @@ function _default({
132
128
  const isNonMacroI18n = isI18nMethod(path.node.callee) && !(firstArgument !== null && firstArgument !== void 0 && firstArgument.leadingComments);
133
129
  if (!hasComment && !isNonMacroI18n) return;
134
130
  let props = {
135
- id: firstArgument.value
131
+ id: getTextFromExpression(t, firstArgument, ctx.file.hub, false)
136
132
  };
137
133
  if (!props.id) {
138
- // don't rise warning when translating from variables
139
- if (!t.isIdentifier(firstArgument)) {
140
- console.warn(path.buildCodeFrameError("Missing message ID, skipping.").message);
141
- }
142
134
  return;
143
135
  }
144
136
  const msgDescArg = path.node.arguments[2];
@@ -148,14 +140,12 @@ function _default({
148
140
  ...extractFromObjectExpression(t, msgDescArg, ctx.file.hub, ["message", "comment", "context"])
149
141
  };
150
142
  }
151
- visitedNodes.add(path.node);
152
143
  collectMessage(path, props, ctx);
153
144
  },
154
145
  StringLiteral(path, ctx) {
155
- if (!hasI18nComment(path.node) || visitedNodes.has(path.node)) {
146
+ if (!hasI18nComment(path.node)) {
156
147
  return;
157
148
  }
158
- visitedNodes.add(path.node);
159
149
  const props = {
160
150
  id: path.node.value
161
151
  };
@@ -167,10 +157,9 @@ function _default({
167
157
  },
168
158
  // Extract message descriptors
169
159
  ObjectExpression(path, ctx) {
170
- if (!hasI18nComment(path.node) || visitedNodes.has(path.node)) {
160
+ if (!hasI18nComment(path.node)) {
171
161
  return;
172
162
  }
173
- visitedNodes.add(path.node);
174
163
  const props = extractFromObjectExpression(t, path.node, ctx.file.hub, ["id", "message", "comment", "context"]);
175
164
  if (!props.id) {
176
165
  console.warn(path.buildCodeFrameError("Missing message ID, skipping.").message);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lingui/babel-plugin-extract-messages",
3
- "version": "4.0.0-next.0",
3
+ "version": "4.0.0-next.2",
4
4
  "description": "Babel plugin for collecting messages from source code for internationalization",
5
5
  "main": "./build/index.js",
6
6
  "author": {
@@ -41,5 +41,5 @@
41
41
  "@babel/types": "^7.20.7",
42
42
  "@lingui/jest-mocks": "^3.0.3"
43
43
  },
44
- "gitHead": "637f5cf6beda9d4ebce4e15cd9fdaa23e404b660"
44
+ "gitHead": "556ab57e20c2ac9d384a22424c6a90c2ba0dd133"
45
45
  }