@@ -18,6 +18,7 @@ const COSMETIC_FIELDS: &[&str] = &[
1818 "comments" ,
1919 "leading_comments" ,
2020 "pre_alias_comments" ,
21+ "post_select_comments" ,
2122 "left_comments" ,
2223 "operator_comments" ,
2324 "right_comments" ,
@@ -194,13 +195,65 @@ pub(crate) fn integer_literal(node: &Value) -> Option<u64> {
194195
195196/// The tree conversion reads, positions stripped and quoted identifiers folded if case is ignored.
196197pub ( crate ) fn session_tree ( positioned : & Value , ignore_quoted_case : bool ) -> Result < Value , String > {
197- let mut tree = strip_positions ( positioned) ;
198+ let mut tree = without_comment_wrappers ( & strip_positions ( positioned) ) ;
198199 if ignore_quoted_case {
199200 fold_quoted ( & mut tree) ?;
200201 }
201202 Ok ( tree)
202203}
203204
205+ /// `value` with each node that only wrapped a comment replaced by the node it wrapped.
206+ fn without_comment_wrappers ( value : & Value ) -> Value {
207+ unwrap_comments ( value, false )
208+ }
209+
210+ /// `value` with its comment wrappers unwrapped, its own kept if `keep_wrapper`.
211+ fn unwrap_comments ( value : & Value , keep_wrapper : bool ) -> Value {
212+ match value {
213+ Value :: Object ( fields) => match fields. get ( "annotated" ) . and_then ( Value :: as_object) {
214+ Some ( annotated) if !keep_wrapper && fields. len ( ) == 1 && annotated. len ( ) == 1 => {
215+ annotated
216+ . get ( "this" )
217+ . map_or_else ( || value. clone ( ) , without_comment_wrappers)
218+ }
219+ _ => Value :: Object (
220+ fields
221+ . iter ( )
222+ . map ( |( key, field) | ( key. clone ( ) , unwrap_field ( key, field) ) )
223+ . collect ( ) ,
224+ ) ,
225+ } ,
226+ Value :: Array ( items) => Value :: Array ( items. iter ( ) . map ( without_comment_wrappers) . collect ( ) ) ,
227+ other => other. clone ( ) ,
228+ }
229+ }
230+
231+ /// A node's field unwrapped; a SELECT keeps its select items' wrappers, which name their columns.
232+ fn unwrap_field ( key : & str , field : & Value ) -> Value {
233+ match ( key, field) {
234+ ( "select" , Value :: Object ( clauses) ) => Value :: Object (
235+ clauses
236+ . iter ( )
237+ . map ( |( name, clause) | ( name. clone ( ) , unwrap_clause ( name, clause) ) )
238+ . collect ( ) ,
239+ ) ,
240+ _ => without_comment_wrappers ( field) ,
241+ }
242+ }
243+
244+ /// A SELECT clause with comment wrappers unwrapped, except around the select items themselves.
245+ fn unwrap_clause ( name : & str , clause : & Value ) -> Value {
246+ match ( name, clause) {
247+ ( "expressions" , Value :: Array ( items) ) => Value :: Array (
248+ items
249+ . iter ( )
250+ . map ( |item| unwrap_comments ( item, true ) )
251+ . collect ( ) ,
252+ ) ,
253+ _ => without_comment_wrappers ( clause) ,
254+ }
255+ }
256+
204257/// Upper-cases the name of each quoted identifier under `value`.
205258fn fold_quoted ( value : & mut Value ) -> Result < ( ) , String > {
206259 match value {
0 commit comments