⭐️ A friendly language for building type-safe, scalable systems!

Start tracking position of trailing commas

authored by giacomocavalieri.me and committed by Louis Pilfold 776251d1 8f8a000b

+4
compiler-core/src/format.rs
··· 44 44 module_comments: Vec<Comment<'a>>, 45 45 empty_lines: &'a [u32], 46 46 new_lines: &'a [u32], 47 + trailing_commas: &'a [u32], 47 48 } 48 49 49 50 impl<'a> Intermediate<'a> { ··· 66 67 .map(|span| Comment::from((span, src))) 67 68 .collect(), 68 69 new_lines: &extra.new_lines, 70 + trailing_commas: &extra.trailing_commas, 69 71 } 70 72 } 71 73 } ··· 102 104 module_comments: &'a [Comment<'a>], 103 105 empty_lines: &'a [u32], 104 106 new_lines: &'a [u32], 107 + trailing_commas: &'a [u32], 105 108 } 106 109 107 110 impl<'comments> Formatter<'comments> { ··· 116 119 module_comments: &extra.module_comments, 117 120 empty_lines: extra.empty_lines, 118 121 new_lines: extra.new_lines, 122 + trailing_commas: extra.trailing_commas, 119 123 } 120 124 } 121 125
+13 -6
compiler-core/src/parse.rs
··· 3668 3668 sep: Option<&Token>, 3669 3669 ) -> Result<(Vec<A>, bool), ParseError> { 3670 3670 let mut results = vec![]; 3671 - let mut ends_with_sep = false; 3671 + let mut final_separator = None; 3672 3672 while let Some(result) = parser(self)? { 3673 3673 results.push(result); 3674 3674 if let Some(sep) = sep { 3675 - if self.maybe_one(sep).is_none() { 3676 - ends_with_sep = false; 3677 - break; 3675 + if let Some(separator) = self.maybe_one(sep) { 3676 + final_separator = Some(separator); 3678 3677 } else { 3679 - ends_with_sep = true; 3678 + final_separator = None; 3679 + break; 3680 3680 } 3681 + 3681 3682 // Helpful error if extra separator 3682 3683 if let Some((start, end)) = self.maybe_one(sep) { 3683 3684 return parse_error(ParseErrorType::ExtraSeparator, SrcSpan { start, end }); ··· 3685 3686 } 3686 3687 } 3687 3688 3688 - Ok((results, ends_with_sep)) 3689 + // If the sequence ends with a trailing comma we want to keep track of 3690 + // its position. 3691 + if let (Some(Token::Comma), Some((_, end))) = (sep, final_separator) { 3692 + self.extra.trailing_commas.push(end) 3693 + }; 3694 + 3695 + Ok((results, final_separator.is_some())) 3689 3696 } 3690 3697 3691 3698 // If next token is a Name, consume it and return relevant info, otherwise, return none
+1
compiler-core/src/parse/extra.rs
··· 11 11 pub comments: Vec<SrcSpan>, 12 12 pub empty_lines: Vec<u32>, 13 13 pub new_lines: Vec<u32>, 14 + pub trailing_commas: Vec<u32>, 14 15 } 15 16 16 17 impl ModuleExtra {
+1
compiler-core/src/parse/snapshots/gleam_core__parse__tests__const_string_concat.snap
··· 113 113 20, 114 114 51, 115 115 ], 116 + trailing_commas: [], 116 117 }, 117 118 }
+1
compiler-core/src/parse/snapshots/gleam_core__parse__tests__deprecation_attribute_on_type_variant.snap
··· 85 85 59, 86 86 61, 87 87 ], 88 + trailing_commas: [], 88 89 }, 89 90 }
+1
compiler-core/src/parse/snapshots/gleam_core__parse__tests__import_type.snap
··· 78 78 comments: [], 79 79 empty_lines: [], 80 80 new_lines: [], 81 + trailing_commas: [], 81 82 }, 82 83 }
+1
compiler-core/src/parse/snapshots/gleam_core__parse__tests__record_access_no_label.snap
··· 188 188 73, 189 189 75, 190 190 ], 191 + trailing_commas: [], 191 192 }, 192 193 }