Progess in parsing of the extra languages provided by the user

This commit is contained in:
Joseph Garrone 2024-09-22 15:39:32 +02:00
parent b0b6b994ed
commit 8d2679b76e

View File

@ -136,32 +136,111 @@ export function generateMessageProperties(params: {
return undefined; return undefined;
} }
let firstArgumentCode: string | undefined = undefined; let out: Record<string, { label: string; path: string }> = {};
recast.visit(i18nTsRoot, { recast.visit(i18nTsRoot, {
visitCallExpression: function (path) { visitCallExpression: function (path) {
const node = path.node; const node = path.node;
// Check if the callee is a MemberExpression with property 'withExtraLanguages'
if ( if (
node.callee.type === "MemberExpression" && node.callee.type === "MemberExpression" &&
node.callee.property.type === "Identifier" && node.callee.property.type === "Identifier" &&
node.callee.property.name === "withExtraLanguages" node.callee.property.name === "withExtraLanguages"
) { ) {
firstArgumentCode = babelGenerate(node.arguments[0] as any).code; const arg = node.arguments[0];
return false; if (arg && arg.type === "ObjectExpression") {
} // Iterate over the properties of the object
arg.properties.forEach(prop => {
if (
prop.type === "ObjectProperty" &&
prop.key.type === "Identifier"
) {
const lang = prop.key.name;
const value = prop.value;
this.traverse(path); if (value.type === "ObjectExpression") {
let label: string | undefined = undefined;
let pathStr: string | undefined = undefined;
// Iterate over the properties of the language object
value.properties.forEach(p => {
if (
p.type === "ObjectProperty" &&
p.key.type === "Identifier"
) {
if (
p.key.name === "label" &&
p.value.type === "StringLiteral"
) {
label = p.value.value;
}
if (
p.key.name === "getMessages" &&
(p.value.type ===
"ArrowFunctionExpression" ||
p.value.type === "FunctionExpression")
) {
// Extract the import path from the function body
const body = p.value.body;
if (
body.type === "CallExpression" &&
body.callee.type === "Import"
) {
const importArg = body.arguments[0];
if (
importArg.type === "StringLiteral"
) {
pathStr = importArg.value;
}
} else if (
body.type === "BlockStatement"
) {
// If the function body is a block (e.g., function with braces {})
// Look for return statement
body.body.forEach(statement => {
if (
statement.type ===
"ReturnStatement" &&
statement.argument &&
statement.argument.type ===
"CallExpression" &&
statement.argument.callee
.type === "Import"
) {
const importArg =
statement.argument
.arguments[0];
if (
importArg.type ===
"StringLiteral"
) {
pathStr = importArg.value;
}
}
});
}
}
} }
}); });
if (firstArgumentCode === undefined) { if (label && pathStr) {
return undefined; out[lang] = { label, path: pathStr };
}
}
}
});
} }
//todo return false; // Stop traversing this path
}
this.traverse(path); // Continue traversing other paths
}
});
console.log(out);
//TODO
return {}; return {};
})(); })();