Skip to content

Commit

Permalink
Fix copy ctor of ParserOptions (default implementation doesn't deep c…
Browse files Browse the repository at this point in the history
…lone record type fields) (sebastienros#436)
  • Loading branch information
adams85 authored Feb 19, 2024
1 parent d0938c3 commit 4661614
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Esprima/ParserOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/// <summary>
/// Gets or sets whether the tokens are included in the parsed tree, defaults to <see langword="false"/>.
/// </summary>
Expand Down
26 changes: 26 additions & 0 deletions test/Esprima.Tests/ParserOptionsTests.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 4661614

Please sign in to comment.