From 75c25c726f07510bdf3d2efeb0b2d739494126d1 Mon Sep 17 00:00:00 2001 From: Christophe PLAT Date: Mon, 19 Aug 2024 21:51:22 +0200 Subject: [PATCH] No longer consider number literal suffixes as identifiers. (#316) Fixes #300 --- src/DynamicExpresso.Core/Detector.cs | 15 ++++++++++++--- .../DetectIdentifiersTest.cs | 17 +++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/DynamicExpresso.Core/Detector.cs b/src/DynamicExpresso.Core/Detector.cs index a315125..77a8191 100644 --- a/src/DynamicExpresso.Core/Detector.cs +++ b/src/DynamicExpresso.Core/Detector.cs @@ -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); diff --git a/test/DynamicExpresso.UnitTest/DetectIdentifiersTest.cs b/test/DynamicExpresso.UnitTest/DetectIdentifiersTest.cs index 27e8374..99d91f2 100644 --- a/test/DynamicExpresso.UnitTest/DetectIdentifiersTest.cs +++ b/test/DynamicExpresso.UnitTest/DetectIdentifiersTest.cs @@ -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); + } } }