Skip to content

Commit

Permalink
Fix edge case in path compilation from patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
yanthomasdev committed Nov 13, 2024
1 parent f00ede3 commit e57b9f7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
12 changes: 9 additions & 3 deletions packages/core/src/files/paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,16 @@ export function createPathResolver(
) => MatchResult<{ lang?: string; path: string }>;

return compile<{ lang?: string; path: string }>(selectedPattern)({
lang: toLang,
// We extract the common path from `fromPath` to build the resulting path.
path: matcher(fromPath).params.path,
// We extract and inject any parameters that could be found
// from the initial path to the resulting path, this is what
// enables the path inferring.
...matcher(fromPath).params,
// Locale parameters are injected as-is from the locale's `parameters` field,
// if the pattern needs any of those parameters, it will have the values needed.
...localeParameters,
// The lang has to be given last since it could be overwritten by the initial path's
// parameters, which we don't want to happen.
lang: toLang,
});
},
sourcePattern: sourcePattern,
Expand Down
13 changes: 5 additions & 8 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,11 @@ class Lunaria {
try {
const sourcePath = isSourcePath(path) ? path : toPath(path, this.config.sourceLocale.lang);

// There's a few cases in which the pattern might match, but the include/exclude filters don't,
// therefore we need to test both to find the correct `files` config.
return (
isSourcePath(sourcePath) &&
picomatch.isMatch(sourcePath, file.include, {
ignore: file.exclude,
})
);
// Checks if the path matches the `include` and `exclude` fields.
return picomatch.isMatch(sourcePath, file.include, {
ignore: file.exclude,
});

// If it fails to match, we assume it's not the respective `files` config and return false.
} catch {
return false;
Expand Down

0 comments on commit e57b9f7

Please sign in to comment.