Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't restrict the alias pattern match groups' order #25

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading