@@ -8,76 +8,81 @@ public enum LocationMembersPlacement
88 Start
99}
1010
11- public static class AstToJson
11+ internal enum AstToJsonTestCompatibilityMode
1212{
13- public record class Options
14- {
15- public static readonly Options Default = new ( ) ;
16-
17- public bool IncludingLineColumn { get ; init ; }
18- public bool IncludingRange { get ; init ; }
19- public LocationMembersPlacement LocationMembersPlacement { get ; init ; }
20- /// <summary>
21- /// This switch is intended for enabling a compatibility mode for <see cref="AstToJsonConverter"/> to build a JSON output
22- /// which matches the format of the test fixtures of the original Esprima project.
23- /// </summary>
24- internal TestCompatibilityMode TestCompatibilityMode { get ; init ; }
25- }
13+ None ,
14+ EsprimaOrg ,
15+ }
2616
27- internal enum TestCompatibilityMode
28- {
29- None ,
30- EsprimaOrg ,
31- }
17+ public record class AstToJsonOptions
18+ {
19+ public static readonly AstToJsonOptions Default = new ( ) ;
3220
33- public static string ToJsonString ( this Node node , AstToJsonConverter . Factory ? converterFactory = null )
21+ public bool IncludingLineColumn { get ; init ; }
22+ public bool IncludingRange { get ; init ; }
23+ public LocationMembersPlacement LocationMembersPlacement { get ; init ; }
24+ /// <summary>
25+ /// This switch is intended for enabling a compatibility mode for <see cref="AstToJsonConverter"/> to build a JSON output
26+ /// which matches the format of the test fixtures of the original Esprima project.
27+ /// </summary>
28+ internal AstToJsonTestCompatibilityMode TestCompatibilityMode { get ; init ; }
29+
30+ protected internal virtual AstToJsonConverter CreateConverter ( JsonWriter writer ) => new AstToJsonConverter ( writer , this ) ;
31+ }
32+
33+ public static class AstToJson
34+ {
35+ public static string ToJsonString ( this Node node )
3436 {
35- return ToJsonString ( node , indent : null , converterFactory ) ;
37+ return ToJsonString ( node , indent : null ) ;
3638 }
3739
38- public static string ToJsonString ( this Node node , string ? indent , AstToJsonConverter . Factory ? converterFactory = null )
40+ public static string ToJsonString ( this Node node , string ? indent )
3941 {
40- return ToJsonString ( node , Options . Default , indent , converterFactory ) ;
42+ return ToJsonString ( node , AstToJsonOptions . Default , indent ) ;
4143 }
4244
43- public static string ToJsonString ( this Node node , Options options , AstToJsonConverter . Factory ? converterFactory = null )
45+ public static string ToJsonString ( this Node node , AstToJsonOptions options )
4446 {
45- return ToJsonString ( node , options , indent : null , converterFactory ) ;
47+ return ToJsonString ( node , options , indent : null ) ;
4648 }
4749
48- public static string ToJsonString ( this Node node , Options options , string ? indent , AstToJsonConverter . Factory ? converterFactory = null )
50+ public static string ToJsonString ( this Node node , AstToJsonOptions options , string ? indent )
4951 {
5052 using ( var writer = new StringWriter ( ) )
5153 {
52- WriteJson ( node , writer , options , indent , converterFactory ) ;
54+ WriteJson ( node , writer , options , indent ) ;
5355 return writer . ToString ( ) ;
5456 }
5557 }
5658
57- public static void WriteJson ( this Node node , TextWriter writer , AstToJsonConverter . Factory ? converterFactory = null )
59+ public static void WriteJson ( this Node node , TextWriter writer )
5860 {
59- WriteJson ( node , writer , indent : null , converterFactory ) ;
61+ WriteJson ( node , writer , indent : null ) ;
6062 }
6163
62- public static void WriteJson ( this Node node , TextWriter writer , string ? indent , AstToJsonConverter . Factory ? converterFactory = null )
64+ public static void WriteJson ( this Node node , TextWriter writer , string ? indent )
6365 {
64- WriteJson ( node , writer , Options . Default , indent , converterFactory ) ;
66+ WriteJson ( node , writer , AstToJsonOptions . Default , indent ) ;
6567 }
6668
67- public static void WriteJson ( this Node node , TextWriter writer , Options options , AstToJsonConverter . Factory ? converterFactory = null )
69+ public static void WriteJson ( this Node node , TextWriter writer , AstToJsonOptions options )
6870 {
69- WriteJson ( node , writer , options , indent : null , converterFactory ) ;
71+ WriteJson ( node , writer , options , indent : null ) ;
7072 }
7173
72- public static void WriteJson ( this Node node , TextWriter writer , Options options , string ? indent , AstToJsonConverter . Factory ? converterFactory = null )
74+ public static void WriteJson ( this Node node , TextWriter writer , AstToJsonOptions options , string ? indent )
7375 {
74- WriteJson ( node , new JsonTextWriter ( writer , indent ) , options , converterFactory ) ;
76+ WriteJson ( node , new JsonTextWriter ( writer , indent ) , options ) ;
7577 }
7678
77- public static void WriteJson ( this Node node , JsonWriter writer , Options options , AstToJsonConverter . Factory ? converterFactory = null )
79+ public static void WriteJson ( this Node node , JsonWriter writer , AstToJsonOptions options )
7880 {
79- converterFactory ??= static ( writer , options ) => new AstToJsonConverter ( writer , options ) ;
81+ if ( options is null )
82+ {
83+ throw new ArgumentNullException ( nameof ( options ) ) ;
84+ }
8085
81- converterFactory ( writer , options ) . Convert ( node ) ;
86+ options . CreateConverter ( writer ) . Convert ( node ) ;
8287 }
8388}
0 commit comments