Skip to content

Commit

Permalink
Don't restrict the alias pattern match groups' order (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
z4kn4fein authored May 28, 2024
1 parent 9d21747 commit 27999a8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
18 changes: 12 additions & 6 deletions src/ConfigCat.Cli.Services/Scan/AliasCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,24 @@ public async Task<AliasScanResult> CollectAsync(FlagModel[] flags, FileInfo file
if (!matchPattern.Contains("CC_KEY"))
continue;

var regMatch = Regex.Match(line, matchPattern.Replace("CC_KEY", $"[`'\"]?({keys})[`'\"]?"), RegexOptions.Compiled);
var regMatch = Regex.Match(line, matchPattern.Replace("CC_KEY", $"[`'\"]?(?<keys>{keys})[`'\"]?"), RegexOptions.Compiled);
while (regMatch.Success && !cancellation.IsCancellationRequested)
{
var key = regMatch.Groups[2].Value;
var found = regMatch.Groups[1].Value;
var flag = flags.FirstOrDefault(f => f.Key == key);
var keyGroup = regMatch.Groups["keys"];
var found = regMatch.Groups.Values.Skip(1).Except([keyGroup]).FirstOrDefault();
if (found is null)
{
regMatch = regMatch.NextMatch();
continue;
}

var flag = flags.FirstOrDefault(f => f.Key == keyGroup.Value);

if (flag != null)
result.FoundFlags.Add(flag);

if (flag != null && !found.IsEmpty())
result.FlagAliases.AddOrUpdate(flag, [found], (k, v) => { v.Add(found); return v; });
if (flag != null && !found.Value.IsEmpty())
result.FlagAliases.AddOrUpdate(flag, [found.Value], (k, v) => { v.Add(found.Value); return v; });

regMatch = regMatch.NextMatch();
}
Expand Down
11 changes: 11 additions & 0 deletions test/ConfigCat.Cli.Tests/ScanTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,17 @@ public async Task Custom_Other()

Assert.Contains("CUS2_TEST_FLAG = client_wrapper.get_flag(:test_flag)", referenceLines);
Assert.Contains("Reference to CUS2_TEST_FLAG", referenceLines);

result = await aliasCollector.CollectAsync(new[] { flag }, file, [@"client_wrapper\.get_flag\(:CC_KEY, (\w+) =>"], CancellationToken.None);
flag.Aliases = result.FlagAliases[flag].ToList();

Assert.Contains("cust_flag_val", flag.Aliases);

references = await scanner.CollectAsync(new[] { flag }, file, 0, CancellationToken.None);
referenceLines = references.References.Select(r => r.ReferenceLine.LineText);

Assert.Contains("client_wrapper.get_flag(:test_flag, cust_flag_val => {", referenceLines);
Assert.Contains("Reference to cust_flag_val", referenceLines);
}

[Fact]
Expand Down
6 changes: 5 additions & 1 deletion test/ConfigCat.Cli.Tests/custom.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ Somewhere else refer to CUS_TEST_FLAG

CUS2_TEST_FLAG = client_wrapper.get_flag(:test_flag)

client_wrapper.get_flag(:test_flag, cust_flag_val => {

Somewhere else refer to CUS_TEST_FLAG

let is_test_flag_on := FLAGS('test_flag')

Reference to is_test_flag_on

Reference to CUS2_TEST_FLAG
Reference to CUS2_TEST_FLAG

Reference to cust_flag_val

0 comments on commit 27999a8

Please sign in to comment.