Browse Source

Do not throw error immediately when receiving null Regex from Esprima (#931)

* enabled working regex test
* added parsing tests for [^] and [] regex patterns
* don't fail when handling null regex from esprima
Gökhan Kurt 4 years ago
parent
commit
e75235445c

+ 0 - 6
Jint.Tests.Test262/Test262Test.cs

@@ -321,12 +321,6 @@ namespace Jint.Tests.Test262
                     reason = "Unicode support and its special cases need more work";
                 }
 
-                if (name.StartsWith("built-ins/RegExp/CharacterClassEscapes/"))
-                {
-                    skip = true;
-                    reason = "for-of not implemented";
-                }
-
                 // Promises
                 if (name.StartsWith("built-ins/Promise/allSettled") ||
                     name.StartsWith("built-ins/Promise/any"))

+ 12 - 0
Jint.Tests/Runtime/RegExpTests.cs

@@ -69,5 +69,17 @@ namespace Jint.Tests.Runtime
 
             Assert.Equal("/test/g", result);
         }
+
+        [Fact]
+        public void ShouldNotThrowErrorOnIncompatibleRegex()
+        {
+            var engine = new Engine();
+            Assert.NotNull(engine.Evaluate(@"/[^]*?(:[rp][el]a[\w-]+)[^]*/"));
+            Assert.NotNull(engine.Evaluate("/[^]a/"));
+            Assert.NotNull(engine.Evaluate("new RegExp('[^]a')"));
+
+            Assert.NotNull(engine.Evaluate("/[]/"));
+            Assert.NotNull(engine.Evaluate("new RegExp('[]')"));
+        }
     }
 }

+ 3 - 3
Jint/Native/RegExp/RegExpConstructor.cs

@@ -111,7 +111,7 @@ namespace Jint.Native.RegExp
                 var timeout = _engine.Options._RegexTimeoutInterval;
                 if (timeout.Ticks > 0)
                 {
-                    r.Value = new Regex(r.Value.ToString(), r.Value.Options, timeout);
+                    r.Value = r.Value != null ? new Regex(r.Value.ToString(), r.Value.Options, timeout) : null;
                 }
             }
             catch (Exception ex)
@@ -142,12 +142,12 @@ namespace Jint.Native.RegExp
             r._prototype = PrototypeObject;
 
             r.Flags = flags;
-            r.Source = regExp.ToString();
+            r.Source = regExp?.ToString();
 
             var timeout = _engine.Options._RegexTimeoutInterval;
             if (timeout.Ticks > 0)
             {
-                r.Value = new Regex(regExp.ToString(), regExp.Options, timeout);
+                r.Value = regExp != null ? new Regex(regExp.ToString(), regExp.Options, timeout) : null;
             }
             else
             {