@@ -47,62 +47,62 @@ const JSON_CAPACITY_RATIO_PRETTY: usize = 80;
4747
4848impl Program < ' _ > {
4949 /// Serialize AST to ESTree JSON, including TypeScript fields.
50- pub fn to_estree_ts_json ( & self ) -> String {
50+ pub fn to_estree_ts_json ( & self , ranges : bool ) -> String {
5151 let capacity = self . source_text . len ( ) * JSON_CAPACITY_RATIO_COMPACT ;
52- let mut serializer = CompactTSSerializer :: with_capacity ( capacity) ;
52+ let mut serializer = CompactTSSerializer :: with_capacity ( capacity, ranges ) ;
5353 self . serialize ( & mut serializer) ;
5454 serializer. into_string ( )
5555 }
5656
5757 /// Serialize AST to ESTree JSON, without TypeScript fields.
58- pub fn to_estree_js_json ( & self ) -> String {
58+ pub fn to_estree_js_json ( & self , ranges : bool ) -> String {
5959 let capacity = self . source_text . len ( ) * JSON_CAPACITY_RATIO_COMPACT ;
60- let mut serializer = CompactJSSerializer :: with_capacity ( capacity) ;
60+ let mut serializer = CompactJSSerializer :: with_capacity ( capacity, ranges ) ;
6161 self . serialize ( & mut serializer) ;
6262 serializer. into_string ( )
6363 }
6464
6565 /// Serialize AST to pretty-printed ESTree JSON, including TypeScript fields.
66- pub fn to_pretty_estree_ts_json ( & self ) -> String {
66+ pub fn to_pretty_estree_ts_json ( & self , ranges : bool ) -> String {
6767 let capacity = self . source_text . len ( ) * JSON_CAPACITY_RATIO_PRETTY ;
68- let mut serializer = PrettyTSSerializer :: with_capacity ( capacity) ;
68+ let mut serializer = PrettyTSSerializer :: with_capacity ( capacity, ranges ) ;
6969 self . serialize ( & mut serializer) ;
7070 serializer. into_string ( )
7171 }
7272
7373 /// Serialize AST to pretty-printed ESTree JSON, without TypeScript fields.
74- pub fn to_pretty_estree_js_json ( & self ) -> String {
74+ pub fn to_pretty_estree_js_json ( & self , ranges : bool ) -> String {
7575 let capacity = self . source_text . len ( ) * JSON_CAPACITY_RATIO_PRETTY ;
76- let mut serializer = PrettyJSSerializer :: with_capacity ( capacity) ;
76+ let mut serializer = PrettyJSSerializer :: with_capacity ( capacity, ranges ) ;
7777 self . serialize ( & mut serializer) ;
7878 serializer. into_string ( )
7979 }
8080
8181 /// Serialize AST to ESTree JSON, including TypeScript fields, with list of fixes.
82- pub fn to_estree_ts_json_with_fixes ( & self ) -> String {
82+ pub fn to_estree_ts_json_with_fixes ( & self , ranges : bool ) -> String {
8383 let capacity = self . source_text . len ( ) * JSON_CAPACITY_RATIO_COMPACT ;
84- let serializer = CompactFixesTSSerializer :: with_capacity ( capacity) ;
84+ let serializer = CompactFixesTSSerializer :: with_capacity ( capacity, ranges ) ;
8585 serializer. serialize_with_fixes ( self )
8686 }
8787
8888 /// Serialize AST to ESTree JSON, without TypeScript fields, with list of fixes.
89- pub fn to_estree_js_json_with_fixes ( & self ) -> String {
89+ pub fn to_estree_js_json_with_fixes ( & self , ranges : bool ) -> String {
9090 let capacity = self . source_text . len ( ) * JSON_CAPACITY_RATIO_COMPACT ;
91- let serializer = CompactFixesJSSerializer :: with_capacity ( capacity) ;
91+ let serializer = CompactFixesJSSerializer :: with_capacity ( capacity, ranges ) ;
9292 serializer. serialize_with_fixes ( self )
9393 }
9494
9595 /// Serialize AST to pretty-printed ESTree JSON, including TypeScript fields, with list of fixes.
96- pub fn to_pretty_estree_ts_json_with_fixes ( & self ) -> String {
96+ pub fn to_pretty_estree_ts_json_with_fixes ( & self , ranges : bool ) -> String {
9797 let capacity = self . source_text . len ( ) * JSON_CAPACITY_RATIO_PRETTY ;
98- let serializer = PrettyFixesTSSerializer :: with_capacity ( capacity) ;
98+ let serializer = PrettyFixesTSSerializer :: with_capacity ( capacity, ranges ) ;
9999 serializer. serialize_with_fixes ( self )
100100 }
101101
102102 /// Serialize AST to pretty-printed ESTree JSON, without TypeScript fields, with list of fixes.
103- pub fn to_pretty_estree_js_json_with_fixes ( & self ) -> String {
103+ pub fn to_pretty_estree_js_json_with_fixes ( & self , ranges : bool ) -> String {
104104 let capacity = self . source_text . len ( ) * JSON_CAPACITY_RATIO_PRETTY ;
105- let serializer = PrettyFixesJSSerializer :: with_capacity ( capacity) ;
105+ let serializer = PrettyFixesJSSerializer :: with_capacity ( capacity, ranges ) ;
106106 serializer. serialize_with_fixes ( self )
107107 }
108108}
@@ -168,13 +168,17 @@ impl ESTree for ProgramConverter<'_, '_> {
168168 let span_start =
169169 if S :: INCLUDE_TS_FIELDS { get_ts_start_span ( program) } else { program. span . start } ;
170170
171+ let ranges = serializer. ranges ( ) ;
171172 let mut state = serializer. serialize_struct ( ) ;
172173 state. serialize_field ( "type" , & JsonSafeString ( "Program" ) ) ;
173174 state. serialize_field ( "start" , & span_start) ;
174175 state. serialize_field ( "end" , & program. span . end ) ;
175176 state. serialize_field ( "body" , & Concat2 ( & program. directives , & program. body ) ) ;
176177 state. serialize_field ( "sourceType" , & program. source_type . module_kind ( ) ) ;
177178 state. serialize_field ( "hashbang" , & program. hashbang ) ;
179+ if ranges {
180+ state. serialize_field ( "range" , & [ span_start, program. span . end ] ) ;
181+ }
178182 state. end ( ) ;
179183 }
180184}
0 commit comments