-
-
Notifications
You must be signed in to change notification settings - Fork 394
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(parser,codegen): parse and print leading comments #5779
Conversation
Your org has enabled the Graphite merge queue for merging into mainAdd the label “0-merge” to the PR and Graphite will automatically add it to the merge queue when it’s ready to merge. Or use the label “hotfix” to add to the merge queue as a hot fix. You must have a Graphite account and log in to Graphite in order to use the merge queue. Sign up using this link. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This snapshot is kind of useless.
Unresolved reference IDs mismatch for "LongNameModule": | ||
after transform: [ReferenceId(1), ReferenceId(5)] | ||
rebuilt : [ReferenceId(1)] | ||
x Output mismatch |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to move the spans ... cc @Dunqing
return; | ||
} | ||
let Some(source_text) = self.source_text else { return }; | ||
let Some(comments) = self.leading_comments.remove(&start) else { return }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Performance killer :-/ we need to somehow carry over whether a token has leading comments to the ast node, for a fast path.
.filter(|comment| { | ||
let s = comment.span.source_text(source_text); | ||
// only print jsdoc and `@__PURE__`, `@license`, `@preserve` etc | ||
(comment.is_multi_line() && s.starts_with('*')) || s.trim_start().starts_with('@') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to find all the special comments :-/
pub preceded_by_newline: Option<bool>, | ||
pub followed_by_newline: Option<bool>, | ||
|
||
pub attached_to: u32, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to study what information is required by prettier, cc @leaysgur
I don't remember where this came from
oxc/crates/oxc_prettier/src/comments/mod.rs
Lines 54 to 65 in 91bb06e
bitflags! { | |
#[derive(Debug, Clone, Copy)] | |
pub struct CommentFlags: u8 { | |
const Leading = 1 << 0; // Check comment is a leading comment | |
const Trailing = 1 << 1; // Check comment is a trailing comment | |
const Dangling = 1 << 2; // Check comment is a dangling comment | |
const Block = 1 << 3; // Check comment is a block comment | |
const Line = 1 << 4; // Check comment is a line comment | |
const PrettierIgnore = 1 << 5; // Check comment is a `prettier-ignore` comment | |
const First = 1 << 6; // Check comment is the first attached comment | |
const Last = 1 << 7; // Check comment is the last attached comment | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still on the way to read through these area, but as far as I can tell are:
- Prettier performs 3 phases to collect comment information
- 1: Find
preceding
,enclosing
and|orfollowing
node for each comment- Relatively intuitive, but there is exception
- 2: Classify comments into
ownLine
,endOfLine
andremaining
group - 3: Attach comments to each node with 1 + 2
- This one using so many utils...
- 1: Find
- Final AST nodes contain
comments
field which areESTree.Comment[]
andComment
is extended withleading
andtrailing
flag.- I found a few more extra properties but not understand yet
😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't remember where this came from
Maybe this?
Uh, only the leading
and trailing
flags are extended in the preparation phase, but other of CommentFlags
are dynamically retrieved at runtime...
CodSpeed Performance ReportMerging #5779 will degrade performances by 29.62%Comparing Summary
Benchmarks breakdown
|
@@ -8,19 +8,39 @@ use std::{ | |||
|
|||
use oxc_span::Span; | |||
|
|||
#[derive(Debug, Clone, Copy, Eq, PartialEq)] | |||
pub enum CommentPosition { | |||
Leading, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Leading comments are all comments before a token.
#[derive(Debug, Clone, Copy, Eq, PartialEq)] | ||
pub enum CommentPosition { | ||
Leading, | ||
Trailing, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing comments are all comments tailing a token and before the newline.
Got a prototype working ... I am going to break this apart into stacked PRs, with proper testing.
NOTE: only special leading comments such as jsdoc,
/* @... */
are printed.