Explorar o código

Set AllowReturnOutsideFunction to true for PrepareScript (#1550)

Marko Lahma %!s(int64=2) %!d(string=hai) anos
pai
achega
3f376c75dc

+ 21 - 0
Jint.Tests/Runtime/EngineTests.ScriptPreparation.cs

@@ -0,0 +1,21 @@
+using Esprima.Ast;
+
+namespace Jint.Tests.Runtime;
+
+public partial class EngineTests
+{
+    [Fact]
+    public void ScriptPreparationAcceptsReturnOutsideOfFunctions()
+    {
+        var preparedScript = Engine.PrepareScript("return 1;");
+        Assert.IsType<ReturnStatement>(preparedScript.Body[0]);
+    }
+
+    // TODO when folding will be part of preparation
+    // [Fact]
+    public void ScriptPreparationFoldsConstants()
+    {
+        var preparedScript = Engine.PrepareScript("return 1 + 2;");
+        var returnStatement = Assert.IsType<ReturnStatement>(preparedScript.Body[0]);
+    }
+}

+ 1 - 1
Jint.Tests/Runtime/EngineTests.cs

@@ -14,7 +14,7 @@ using Xunit.Abstractions;
 
 namespace Jint.Tests.Runtime
 {
-    public class EngineTests : IDisposable
+    public partial class EngineTests : IDisposable
     {
         private readonly Engine _engine;
         private int countBreak = 0;

+ 5 - 1
Jint/Engine.Ast.cs

@@ -18,7 +18,11 @@ public partial class Engine
     public static Script PrepareScript(string script, string? source = null, bool strict = false)
     {
         var astAnalyzer = new AstAnalyzer();
-        var options = ParserOptions.Default with { OnNodeCreated = astAnalyzer.NodeVisitor };
+        var options = ParserOptions.Default with
+        {
+            AllowReturnOutsideFunction = true,
+            OnNodeCreated = astAnalyzer.NodeVisitor
+        };
 
         return new JavaScriptParser(options).ParseScript(script, source, strict);
     }