Skip to content

Commit f2cb48a

Browse files
committed
Refactor argument parsing in translation functions to support multiple translations and improve node handling logic
1 parent 796772f commit f2cb48a

1 file changed

Lines changed: 43 additions & 11 deletions

File tree

src/parser/tree.ts

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,26 +182,58 @@ export function doTree(
182182
const translationKeys =
183183
i18nFunctions[functionName as keyof typeof i18nFunctions];
184184

185-
// Filter children to find only arguments, ignoring comments etc.
186-
const argumentNodes = argsNode.children.filter(
187-
(child) => child.type === "argument",
188-
);
189-
185+
// Slice the children to skip the opening and closing parentheses/brackets or process them directly
186+
// We iterate over all children and handle them based on type
187+
const children = argsNode.children;
190188
let translationKeyIndex = 0;
191189

192190
// Get the translation from the arguments
193-
for (const child of argumentNodes) {
191+
for (const child of children) {
194192
let node = child;
195193

194+
// Skip parentheses and commas
195+
if (
196+
node.type === "(" ||
197+
node.type === ")" ||
198+
node.type === "," ||
199+
node.type === "[" ||
200+
node.type === "]"
201+
) {
202+
continue;
203+
}
204+
205+
// Skip comments
206+
if (node.type === "comment") {
207+
continue;
208+
}
209+
196210
// unwrap the argument node, which is used in PHP.
197211
if (child.type === "argument") {
198212
if (child.children.length === 0) continue;
199-
node = child.children[0];
200-
}
201213

202-
if (node?.type === ",") {
203-
// skip the comma between arguments
204-
continue;
214+
// Check if this is a named argument
215+
const nameNode = child.childForFieldName("name");
216+
217+
// Iterate over children to find the value
218+
// The value is the child that is NOT the name node, not a comment, and not punctuation.
219+
let foundValue = false;
220+
for (const argChild of child.children) {
221+
if (argChild.id === nameNode?.id) {
222+
continue; // Skip the name label
223+
}
224+
if (argChild.type === "comment" || argChild.type === ":") {
225+
continue; // Skip comments and colon
226+
}
227+
// Found the value!
228+
node = argChild;
229+
foundValue = true;
230+
break;
231+
}
232+
233+
// If we didn't find a value (e.g. only comments?), skip this argument
234+
if (!foundValue) {
235+
continue;
236+
}
205237
}
206238

207239
// Stop if we have more arguments than keys defined

0 commit comments

Comments
 (0)