From 4661614f09d4bb476a552f8dae82a5887060d570 Mon Sep 17 00:00:00 2001 From: adams85 <31276480+adams85@users.noreply.github.com> Date: Mon, 19 Feb 2024 10:59:27 +0100 Subject: [PATCH] Fix copy ctor of ParserOptions (default implementation doesn't deep clone record type fields) (#436) --- src/Esprima/ParserOptions.cs | 9 ++++++++ test/Esprima.Tests/ParserOptionsTests.cs | 26 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 test/Esprima.Tests/ParserOptionsTests.cs diff --git a/src/Esprima/ParserOptions.cs b/src/Esprima/ParserOptions.cs index 9a05fec7..f90191b8 100644 --- a/src/Esprima/ParserOptions.cs +++ b/src/Esprima/ParserOptions.cs @@ -14,6 +14,15 @@ public record class ParserOptions public ScannerOptions GetScannerOptions() => _scannerOptions; + protected ParserOptions(ParserOptions original) + { + _scannerOptions = original._scannerOptions with { }; + Tokens = original.Tokens; + AllowReturnOutsideFunction = original.AllowReturnOutsideFunction; + MaxAssignmentDepth = original.MaxAssignmentDepth; + OnNodeCreated = original.OnNodeCreated; + } + /// /// Gets or sets whether the tokens are included in the parsed tree, defaults to . /// diff --git a/test/Esprima.Tests/ParserOptionsTests.cs b/test/Esprima.Tests/ParserOptionsTests.cs new file mode 100644 index 00000000..6c9783af --- /dev/null +++ b/test/Esprima.Tests/ParserOptionsTests.cs @@ -0,0 +1,26 @@ +namespace Esprima.Tests; + +public class ParserOptionsTests +{ + [Fact] + public void CopyCtorShouldCreateDeepClone() + { + var options1 = new ParserOptions { Tolerant = false }; + var options2 = options1 with { Tolerant = true }; + + Assert.NotSame(options1.GetScannerOptions(), options2.GetScannerOptions()); + Assert.True(options2.Tolerant); + Assert.False(options1.Tolerant); + } + + [Fact] + public void EqualsShouldCheckStructuralEquality() + { + var options1 = new ParserOptions { Tolerant = false }; + var options2 = options1 with { Tolerant = false }; + + Assert.NotSame(options1.GetScannerOptions(), options2.GetScannerOptions()); + Assert.Equal(options1.GetScannerOptions(), options2.GetScannerOptions()); + Assert.Equal(options1, options2); + } +}