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

Function mapPatternToSource

packages/cli/src/cli/utils/buckets.ts:280–316  ·  view source on GitHub ↗
(
  pattern: string[],
  source: string[],
  locale: string,
  fullPattern: string,
  fullSource: string,
)

Source from the content-addressed store, hash-verified

278// [locale] segment when ** admits multiple valid alignments, the caller must
279// validate uniqueness via `hasAmbiguousLocaleMapping` below.
280function mapPatternToSource(
281 pattern: string[],
282 source: string[],
283 locale: string,
284 fullPattern: string,
285 fullSource: string,
286): { patToSrc: number[] } {
287 const result = alignPatternToSource(pattern, source, locale);
288
289 if (!result) {
290 throw new CLIError({
291 message: `Pattern "${fullPattern}" matched file "${fullSource}" via glob, but pattern segments could not be aligned with source segments. This is usually caused by ambiguous wildcard placement.`,
292 docUrl: "ambiguousPathPattern",
293 });
294 }
295
296 // For every [locale]-bearing pattern segment, verify there is no alternative
297 // alignment that maps it to a different source index. Multiple valid
298 // alignments that agree on [locale] positions (e.g. "**/**" against "a/b")
299 // are fine; alignments that disagree are not, because the downstream
300 // placeholder restoration would produce a different output path.
301 for (let i = 0; i < pattern.length; i += 1) {
302 if (!pattern[i].includes("[locale]")) continue;
303 const chosen = result.patToSrc[i];
304 const alternative = alignPatternToSource(pattern, source, locale, {
305 forbid: { patternIndex: i, sourceIndex: chosen },
306 });
307 if (alternative) {
308 throw new CLIError({
309 message: `Pattern "${fullPattern}" matched file "${fullSource}" via glob, but the [locale] segment "${pattern[i]}" can be aligned to multiple positions in the source path. Anchor the pattern with literal directory names so [locale] has a unique placement.`,
310 docUrl: "ambiguousPathPattern",
311 });
312 }
313 }
314
315 return { patToSrc: result.patToSrc };
316}
317
318// Runs the alignment DFS once and returns the first patToSrc found, or null if
319// none exists. The optional `forbid` constraint disallows assigning a given

Callers 1

expandPlaceholderedGlobFunction · 0.85

Calls 1

alignPatternToSourceFunction · 0.85

Tested by

no test coverage detected