refactor(prettier): improve comment handling API (#1435)

This commit is contained in:
Boshen 2023-11-19 23:08:23 +08:00 committed by GitHub
parent 0218ae8641
commit 18f19ee164
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 10 deletions

View file

@ -68,8 +68,12 @@ impl<'a> Prettier<'a> {
parts.push(hardline!());
}
let end = self.get_comment_end(kind, end);
if self.skip_newline(self.skip_spaces(end)).is_some_and(|i| self.has_newline(i)) {
if self
.get_comment_end(kind, end)
.map(|end| self.skip_spaces(end))
.and_then(|idx| self.skip_newline(idx))
.is_some_and(|i| self.has_newline(i))
{
parts.push(hardline!());
}
} else {

View file

@ -110,27 +110,26 @@ impl<'a> Prettier<'a> {
}
#[allow(clippy::cast_possible_truncation)]
fn skip_newline(&self, start_index: Option<u32>) -> Option<u32> {
let start_index = start_index?;
fn skip_newline(&self, start_index: u32) -> Option<u32> {
let c = self.source_text[start_index as usize..].chars().next()?;
is_line_terminator(c).then(|| start_index + c.len_utf8() as u32)
}
fn skip_spaces(&self, start_index: Option<u32>) -> Option<u32> {
let mut start_index = start_index?;
fn skip_spaces(&self, start_index: u32) -> u32 {
let mut index = start_index;
for c in self.source_text[start_index as usize..].chars() {
if matches!(c, ' ' | '\t') {
start_index += 1;
index += 1;
} else {
break;
}
}
Some(start_index)
index
}
fn has_newline(&self, start_index: u32) -> bool {
let idx = self.skip_spaces(Some(start_index));
let idx = self.skip_spaces(start_index);
let idx2 = self.skip_newline(idx);
idx != idx2
Some(idx) != idx2
}
}