MCPcopy
hub / github.com/lingodotdev/lingo.dev / findAssociatedKey

Function findAssociatedKey

packages/cli/src/cli/loaders/jsonc.ts:167–229  ·  view source on GitHub ↗
(
  lines: string[],
  commentLineIndex: number,
  contextStack: Array<{ key: string; isArray: boolean; arrayIndex?: number }>,
  arrayObjectCount: Record<number, number>,
)

Source from the content-addressed store, hash-verified

165}
166
167function findAssociatedKey(
168 lines: string[],
169 commentLineIndex: number,
170 contextStack: Array<{ key: string; isArray: boolean; arrayIndex?: number }>,
171 arrayObjectCount: Record<number, number>,
172): { key: string | null; path: string[] } {
173 // Look for the next key after the comment
174 for (let i = commentLineIndex + 1; i < lines.length; i++) {
175 const line = lines[i].trim();
176
177 if (
178 !line ||
179 line.startsWith("//") ||
180 line.startsWith("/*")
181 ) {
182 continue;
183 }
184
185 // Check if we're about to enter an array object
186 if (line === "{" && contextStack.length > 0) {
187 const parent = contextStack[contextStack.length - 1];
188 if (parent.isArray) {
189 // Get the current array index from arrayObjectCount
190 const depth = contextStack.length - 1;
191 const arrayIndex = arrayObjectCount[depth] || 0;
192
193 // Continue looking for the key inside this object
194 for (let j = i + 1; j < lines.length; j++) {
195 const innerLine = lines[j].trim();
196 if (!innerLine || innerLine.startsWith("//") || innerLine.startsWith("/*")) continue;
197
198 const keyMatch = innerLine.match(/^\s*["']?([^"':,\s]+)["']?\s*:/);
199 if (keyMatch) {
200 const key = keyMatch[1];
201 const path = contextStack
202 .map((ctx) => ctx.arrayIndex !== undefined ? String(ctx.arrayIndex) : ctx.key)
203 .filter(Boolean);
204 path.push(String(arrayIndex));
205 return { key, path };
206 }
207
208 if (innerLine === "}") break;
209 }
210 }
211 }
212
213 if (line === "{" || line === "}") {
214 continue;
215 }
216
217 // Extract key from line
218 const keyMatch = line.match(/^\s*["']?([^"':,\s]+)["']?\s*:/);
219 if (keyMatch) {
220 const key = keyMatch[1];
221 const path = contextStack
222 .map((ctx) => ctx.arrayIndex !== undefined ? String(ctx.arrayIndex) : ctx.key)
223 .filter(Boolean);
224 return { key, path };

Callers 1

extractCommentsFromJsoncFunction · 0.85

Calls 1

pushMethod · 0.65

Tested by

no test coverage detected