Skip to content

Commit

Permalink
No longer consider number literal suffixes as identifiers. (#316)
Browse files Browse the repository at this point in the history
Fixes #300
  • Loading branch information
metoule committed Aug 19, 2024
1 parent c6835e3 commit 75c25c7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/DynamicExpresso.Core/Detector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,18 @@ public IdentifiersInfo DetectIdentifiers(string expression)
if (IsReservedKeyword(identifier))
continue;

// don't consider member accesses as identifiers (e.g. "x.Length" will only return x but not Length)
if (idGroup.Index > 0 && expression[idGroup.Index - 1] == '.')
continue;
if (idGroup.Index > 0)
{
var previousChar = expression[idGroup.Index - 1];

// don't consider member accesses as identifiers (e.g. "x.Length" will only return x but not Length)
if (previousChar == '.')
continue;

// don't consider number literals as identifiers
if (char.IsDigit(previousChar))
continue;
}

if (_settings.Identifiers.TryGetValue(identifier, out Identifier knownIdentifier))
knownIdentifiers.Add(knownIdentifier);
Expand Down
17 changes: 17 additions & 0 deletions test/DynamicExpresso.UnitTest/DetectIdentifiersTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,5 +324,22 @@ public void Dont_detect_members_with_at()
Assert.AreEqual(1, detectedIdentifiers.UnknownIdentifiers.Count());
Assert.AreEqual("@class", detectedIdentifiers.UnknownIdentifiers.ElementAt(0));
}


[Test]
[TestCase("1L")]
[TestCase("2M")]
[TestCase("3.0D")]
[TestCase("4.0F")]
[TestCase("6.7e-8")]
[TestCase("9U")]
[TestCase("10ul")]
[TestCase("11lu")]
public void Dont_detect_numbers_with_suffix(string code)
{
var target = new Interpreter(InterpreterOptions.Default | InterpreterOptions.LambdaExpressions);
var detectedIdentifiers = target.DetectIdentifiers(code);
Assert.IsEmpty(detectedIdentifiers.UnknownIdentifiers);
}
}
}

0 comments on commit 75c25c7

Please sign in to comment.