mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 12:19:15 +00:00
feat(ast): add Comment::real_span (#5764)
This commit is contained in:
parent
5bace1ad55
commit
e8bf30a125
4 changed files with 24 additions and 30 deletions
|
|
@ -23,6 +23,10 @@ impl Comment {
|
|||
Self { kind, span }
|
||||
}
|
||||
|
||||
pub fn real_span(&self) -> Span {
|
||||
Span::new(self.real_span_start(), self.real_span_end())
|
||||
}
|
||||
|
||||
pub fn real_span_end(&self) -> u32 {
|
||||
match self.kind {
|
||||
CommentKind::SingleLine => self.span.end,
|
||||
|
|
|
|||
|
|
@ -27,12 +27,10 @@ fn main() -> Result<(), String> {
|
|||
println!("{}", serde_json::to_string_pretty(&ret.program).unwrap());
|
||||
|
||||
println!("Comments:");
|
||||
let comments = ret
|
||||
.trivias
|
||||
.comments()
|
||||
.map(|comment| comment.span.source_text(&source_text))
|
||||
.collect::<Vec<_>>();
|
||||
println!("{comments:?}");
|
||||
for comment in ret.trivias.comments() {
|
||||
let s = comment.real_span().source_text(&source_text);
|
||||
println!("{s}");
|
||||
}
|
||||
|
||||
if ret.errors.is_empty() {
|
||||
println!("Parsed Successfully.");
|
||||
|
|
|
|||
|
|
@ -1,24 +1,26 @@
|
|||
mod print;
|
||||
|
||||
use bitflags::bitflags;
|
||||
use oxc_ast::CommentKind;
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Comment {
|
||||
/// Span start including `//` and `/*`
|
||||
pub start: u32,
|
||||
/// Span end including `*?`
|
||||
pub end: u32,
|
||||
pub is_block: bool,
|
||||
pub has_line_suffix: bool,
|
||||
}
|
||||
|
||||
impl Comment {
|
||||
pub fn new(start: u32, end: u32, kind: CommentKind) -> Self {
|
||||
// The comment span is for the comment value
|
||||
// -2 for `//` and `/*`
|
||||
let start = start - 2;
|
||||
// +2 for `/*`
|
||||
let end = if kind.is_multi_line() { end + 2 } else { end };
|
||||
Self { start, end, is_block: kind.is_multi_line(), has_line_suffix: false }
|
||||
pub fn new(comment: oxc_ast::Comment) -> Self {
|
||||
let span = comment.real_span();
|
||||
Self {
|
||||
start: span.start,
|
||||
end: span.end,
|
||||
is_block: comment.kind.is_multi_line(),
|
||||
has_line_suffix: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_line_suffix(mut self, yes: bool) -> Self {
|
||||
|
|
|
|||
|
|
@ -34,10 +34,8 @@ impl<'a> Prettier<'a> {
|
|||
let mut peekable_trivias = self.trivias.clone();
|
||||
|
||||
while let Some(comment) = peekable_trivias.peek().copied() {
|
||||
let start = comment.span.start;
|
||||
let end = comment.span.end;
|
||||
let mut should_break = true;
|
||||
let comment = Comment::new(start, end, comment.kind);
|
||||
let comment = Comment::new(comment);
|
||||
|
||||
if range.end < comment.start
|
||||
&& self.source_text[range.end as usize..comment.start as usize]
|
||||
|
|
@ -71,11 +69,9 @@ impl<'a> Prettier<'a> {
|
|||
pub(crate) fn print_leading_comments(&mut self, range: Span) -> Option<Doc<'a>> {
|
||||
let mut parts = self.vec();
|
||||
while let Some(comment) = self.trivias.peek().copied() {
|
||||
let start = comment.span.start;
|
||||
let end = comment.span.end;
|
||||
let comment = Comment::new(start, end, comment.kind);
|
||||
let comment = Comment::new(comment);
|
||||
// Comment before the span
|
||||
if end <= range.start {
|
||||
if comment.end <= range.start {
|
||||
self.trivias.next();
|
||||
self.print_leading_comment(&mut parts, comment);
|
||||
} else {
|
||||
|
|
@ -120,9 +116,7 @@ impl<'a> Prettier<'a> {
|
|||
let mut parts = self.vec();
|
||||
let mut previous_comment: Option<Comment> = None;
|
||||
while let Some(comment) = self.trivias.peek().copied() {
|
||||
let start = comment.span.start;
|
||||
let end = comment.span.end;
|
||||
let comment = Comment::new(start, end, comment.kind);
|
||||
let comment = Comment::new(comment);
|
||||
// Trailing comment if there is nothing in between.
|
||||
if range.end < comment.start
|
||||
&& self.source_text[range.end as usize..comment.start as usize]
|
||||
|
|
@ -187,9 +181,7 @@ impl<'a> Prettier<'a> {
|
|||
pub(crate) fn print_inner_comment(&mut self, range: Span) -> Vec<'a, Doc<'a>> {
|
||||
let mut parts = self.vec();
|
||||
while let Some(comment) = self.trivias.peek().copied() {
|
||||
let start = comment.span.start;
|
||||
let end = comment.span.end;
|
||||
let comment = Comment::new(start, end, comment.kind);
|
||||
let comment = Comment::new(comment);
|
||||
// Comment within the span
|
||||
if comment.start >= range.start && comment.end <= range.end {
|
||||
self.trivias.next();
|
||||
|
|
@ -210,9 +202,7 @@ impl<'a> Prettier<'a> {
|
|||
) -> Option<Doc<'a>> {
|
||||
let mut parts = vec![];
|
||||
while let Some(comment) = self.trivias.peek().copied() {
|
||||
let start = comment.span.start;
|
||||
let end = comment.span.end;
|
||||
let comment = Comment::new(start, end, comment.kind);
|
||||
let comment = Comment::new(comment);
|
||||
// Comment within the span
|
||||
if comment.end <= range.end {
|
||||
parts.push(self.print_comment(comment));
|
||||
|
|
|
|||
Loading…
Reference in a new issue