浏览代码

Set AllowReturnOutsideFunction to true for PrepareScript (#1550)

Marko Lahma 2 年之前
父节点
当前提交
3f376c75dc
共有 3 个文件被更改,包括 27 次插入2 次删除
  1. 21 0
      Jint.Tests/Runtime/EngineTests.ScriptPreparation.cs
  2. 1 1
      Jint.Tests/Runtime/EngineTests.cs
  3. 5 1
      Jint/Engine.Ast.cs

+ 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);
     }