Skip to content

Commit 4661614

Browse files
authored
Fix copy ctor of ParserOptions (default implementation doesn't deep clone record type fields) (#436)
1 parent d0938c3 commit 4661614

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/Esprima/ParserOptions.cs

+9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ public record class ParserOptions
1414

1515
public ScannerOptions GetScannerOptions() => _scannerOptions;
1616

17+
protected ParserOptions(ParserOptions original)
18+
{
19+
_scannerOptions = original._scannerOptions with { };
20+
Tokens = original.Tokens;
21+
AllowReturnOutsideFunction = original.AllowReturnOutsideFunction;
22+
MaxAssignmentDepth = original.MaxAssignmentDepth;
23+
OnNodeCreated = original.OnNodeCreated;
24+
}
25+
1726
/// <summary>
1827
/// Gets or sets whether the tokens are included in the parsed tree, defaults to <see langword="false"/>.
1928
/// </summary>
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
namespace Esprima.Tests;
2+
3+
public class ParserOptionsTests
4+
{
5+
[Fact]
6+
public void CopyCtorShouldCreateDeepClone()
7+
{
8+
var options1 = new ParserOptions { Tolerant = false };
9+
var options2 = options1 with { Tolerant = true };
10+
11+
Assert.NotSame(options1.GetScannerOptions(), options2.GetScannerOptions());
12+
Assert.True(options2.Tolerant);
13+
Assert.False(options1.Tolerant);
14+
}
15+
16+
[Fact]
17+
public void EqualsShouldCheckStructuralEquality()
18+
{
19+
var options1 = new ParserOptions { Tolerant = false };
20+
var options2 = options1 with { Tolerant = false };
21+
22+
Assert.NotSame(options1.GetScannerOptions(), options2.GetScannerOptions());
23+
Assert.Equal(options1.GetScannerOptions(), options2.GetScannerOptions());
24+
Assert.Equal(options1, options2);
25+
}
26+
}

0 commit comments

Comments
 (0)