@lingui/babel-plugin-extract-messages
Advanced tools
Comparing version 4.0.0-next.1 to 4.0.0-next.2
@@ -21,3 +21,3 @@ "use strict"; | ||
} | ||
function getTextFromExpression(t, exp, hub) { | ||
function getTextFromExpression(t, exp, hub, emitErrorOnVariable = true) { | ||
if (t.isStringLiteral(exp)) { | ||
@@ -27,3 +27,3 @@ return exp.value; | ||
if (t.isBinaryExpression(exp)) { | ||
return getTextFromExpression(t, exp.left, hub) + getTextFromExpression(t, exp.right, hub); | ||
return getTextFromExpression(t, exp.left, hub, emitErrorOnVariable) + getTextFromExpression(t, exp.right, hub, emitErrorOnVariable); | ||
} | ||
@@ -38,3 +38,5 @@ if (t.isTemplateLiteral(exp)) { | ||
} | ||
console.warn(hub.buildError(exp, "Only strings or template literals could be extracted.", SyntaxError).message); | ||
if (emitErrorOnVariable) { | ||
console.warn(hub.buildError(exp, "Only strings or template literals could be extracted.", SyntaxError).message); | ||
} | ||
} | ||
@@ -57,7 +59,2 @@ function extractFromObjectExpression(t, exp, hub, keys) { | ||
let localTransComponentName; | ||
// We need to remember all processed nodes. When JSX expressions are | ||
// replaced with CallExpressions, all children are traversed for each CallExpression. | ||
// Then, i18n._ methods are visited multiple times for each parent CallExpression. | ||
const visitedNodes = new WeakSet(); | ||
function isTransComponent(node) { | ||
@@ -128,3 +125,2 @@ return t.isJSXElement(node) && t.isJSXIdentifier(node.openingElement.name, { | ||
CallExpression(path, ctx) { | ||
if (visitedNodes.has(path.node)) return; | ||
const hasComment = [path.node, path.parent].some(node => hasI18nComment(node)); | ||
@@ -140,9 +136,5 @@ const firstArgument = path.node.arguments[0]; | ||
let props = { | ||
id: firstArgument.value | ||
id: getTextFromExpression(t, firstArgument, ctx.file.hub, false) | ||
}; | ||
if (!props.id) { | ||
// don't rise warning when translating from variables | ||
if (!t.isIdentifier(firstArgument)) { | ||
console.warn(path.buildCodeFrameError("Missing message ID, skipping.").message); | ||
} | ||
return; | ||
@@ -157,10 +149,8 @@ } | ||
} | ||
visitedNodes.add(path.node); | ||
collectMessage(path, props, ctx); | ||
}, | ||
StringLiteral(path, ctx) { | ||
if (!hasI18nComment(path.node) || visitedNodes.has(path.node)) { | ||
if (!hasI18nComment(path.node)) { | ||
return; | ||
} | ||
visitedNodes.add(path.node); | ||
const props = { | ||
@@ -177,6 +167,5 @@ id: path.node.value | ||
ObjectExpression(path, ctx) { | ||
if (!hasI18nComment(path.node) || visitedNodes.has(path.node)) { | ||
if (!hasI18nComment(path.node)) { | ||
return; | ||
} | ||
visitedNodes.add(path.node); | ||
const props = extractFromObjectExpression(t, path.node, ctx.file.hub, ["id", "message", "comment", "context"]); | ||
@@ -183,0 +172,0 @@ if (!props.id) { |
@@ -21,3 +21,3 @@ "use strict"; | ||
} | ||
function getTextFromExpression(t, exp, hub) { | ||
function getTextFromExpression(t, exp, hub, emitErrorOnVariable = true) { | ||
if (t.isStringLiteral(exp)) { | ||
@@ -27,3 +27,3 @@ return exp.value; | ||
if (t.isBinaryExpression(exp)) { | ||
return getTextFromExpression(t, exp.left, hub) + getTextFromExpression(t, exp.right, hub); | ||
return getTextFromExpression(t, exp.left, hub, emitErrorOnVariable) + getTextFromExpression(t, exp.right, hub, emitErrorOnVariable); | ||
} | ||
@@ -38,3 +38,5 @@ if (t.isTemplateLiteral(exp)) { | ||
} | ||
console.warn(hub.buildError(exp, "Only strings or template literals could be extracted.", SyntaxError).message); | ||
if (emitErrorOnVariable) { | ||
console.warn(hub.buildError(exp, "Only strings or template literals could be extracted.", SyntaxError).message); | ||
} | ||
} | ||
@@ -57,7 +59,2 @@ function extractFromObjectExpression(t, exp, hub, keys) { | ||
let localTransComponentName; | ||
// We need to remember all processed nodes. When JSX expressions are | ||
// replaced with CallExpressions, all children are traversed for each CallExpression. | ||
// Then, i18n._ methods are visited multiple times for each parent CallExpression. | ||
const visitedNodes = new WeakSet(); | ||
function isTransComponent(node) { | ||
@@ -128,3 +125,2 @@ return t.isJSXElement(node) && t.isJSXIdentifier(node.openingElement.name, { | ||
CallExpression(path, ctx) { | ||
if (visitedNodes.has(path.node)) return; | ||
const hasComment = [path.node, path.parent].some(node => hasI18nComment(node)); | ||
@@ -140,9 +136,5 @@ const firstArgument = path.node.arguments[0]; | ||
let props = { | ||
id: firstArgument.value | ||
id: getTextFromExpression(t, firstArgument, ctx.file.hub, false) | ||
}; | ||
if (!props.id) { | ||
// don't rise warning when translating from variables | ||
if (!t.isIdentifier(firstArgument)) { | ||
console.warn(path.buildCodeFrameError("Missing message ID, skipping.").message); | ||
} | ||
return; | ||
@@ -157,10 +149,8 @@ } | ||
} | ||
visitedNodes.add(path.node); | ||
collectMessage(path, props, ctx); | ||
}, | ||
StringLiteral(path, ctx) { | ||
if (!hasI18nComment(path.node) || visitedNodes.has(path.node)) { | ||
if (!hasI18nComment(path.node)) { | ||
return; | ||
} | ||
visitedNodes.add(path.node); | ||
const props = { | ||
@@ -177,6 +167,5 @@ id: path.node.value | ||
ObjectExpression(path, ctx) { | ||
if (!hasI18nComment(path.node) || visitedNodes.has(path.node)) { | ||
if (!hasI18nComment(path.node)) { | ||
return; | ||
} | ||
visitedNodes.add(path.node); | ||
const props = extractFromObjectExpression(t, path.node, ctx.file.hub, ["id", "message", "comment", "context"]); | ||
@@ -183,0 +172,0 @@ if (!props.id) { |
{ | ||
"name": "@lingui/babel-plugin-extract-messages", | ||
"version": "4.0.0-next.1", | ||
"version": "4.0.0-next.2", | ||
"description": "Babel plugin for collecting messages from source code for internationalization", | ||
@@ -44,3 +44,3 @@ "main": "./build/index.js", | ||
}, | ||
"gitHead": "27ee8e213ff6d0c7a0cd2b21c573d7f6da43fd85" | ||
"gitHead": "556ab57e20c2ac9d384a22424c6a90c2ba0dd133" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17526
336