mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
perf(codegen): improve printing of statement comments (#7857)
closes #7814
This commit is contained in:
parent
71a40a222f
commit
4b2433539b
2 changed files with 108 additions and 27 deletions
|
|
@ -105,7 +105,11 @@ impl Codegen<'_> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A statement comment also includes legal comments
|
/// A statement comment also includes legal comments
|
||||||
|
#[inline]
|
||||||
pub(crate) fn print_statement_comments(&mut self, start: u32) {
|
pub(crate) fn print_statement_comments(&mut self, start: u32) {
|
||||||
|
if !self.print_comments {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if let Some((comments, unused)) = self.get_statement_comments(start) {
|
if let Some((comments, unused)) = self.get_statement_comments(start) {
|
||||||
self.print_comments(start, &comments, unused);
|
self.print_comments(start, &comments, unused);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -98,72 +98,149 @@ impl Gen for Directive<'_> {
|
||||||
|
|
||||||
impl Gen for Statement<'_> {
|
impl Gen for Statement<'_> {
|
||||||
fn gen(&self, p: &mut Codegen, ctx: Context) {
|
fn gen(&self, p: &mut Codegen, ctx: Context) {
|
||||||
if p.print_comments {
|
|
||||||
p.print_statement_comments(self.span().start);
|
|
||||||
}
|
|
||||||
match self {
|
match self {
|
||||||
Self::BlockStatement(stmt) => stmt.print(p, ctx),
|
Self::BlockStatement(stmt) => {
|
||||||
Self::BreakStatement(stmt) => stmt.print(p, ctx),
|
p.print_statement_comments(stmt.span.start);
|
||||||
Self::ContinueStatement(stmt) => stmt.print(p, ctx),
|
stmt.print(p, ctx);
|
||||||
Self::DebuggerStatement(stmt) => stmt.print(p, ctx),
|
}
|
||||||
Self::DoWhileStatement(stmt) => stmt.print(p, ctx),
|
Self::BreakStatement(stmt) => {
|
||||||
Self::EmptyStatement(stmt) => stmt.print(p, ctx),
|
p.print_statement_comments(stmt.span.start);
|
||||||
Self::ExpressionStatement(stmt) => stmt.print(p, ctx),
|
stmt.print(p, ctx);
|
||||||
Self::ForInStatement(stmt) => stmt.print(p, ctx),
|
}
|
||||||
Self::ForOfStatement(stmt) => stmt.print(p, ctx),
|
Self::ContinueStatement(stmt) => {
|
||||||
Self::ForStatement(stmt) => stmt.print(p, ctx),
|
p.print_statement_comments(stmt.span.start);
|
||||||
Self::IfStatement(stmt) => stmt.print(p, ctx),
|
stmt.print(p, ctx);
|
||||||
Self::LabeledStatement(stmt) => stmt.print(p, ctx),
|
}
|
||||||
Self::ReturnStatement(stmt) => stmt.print(p, ctx),
|
Self::DebuggerStatement(stmt) => {
|
||||||
Self::SwitchStatement(stmt) => stmt.print(p, ctx),
|
p.print_statement_comments(stmt.span.start);
|
||||||
Self::ThrowStatement(stmt) => stmt.print(p, ctx),
|
stmt.print(p, ctx);
|
||||||
Self::TryStatement(stmt) => stmt.print(p, ctx),
|
}
|
||||||
Self::WhileStatement(stmt) => stmt.print(p, ctx),
|
Self::DoWhileStatement(stmt) => {
|
||||||
Self::WithStatement(stmt) => stmt.print(p, ctx),
|
p.print_statement_comments(stmt.span.start);
|
||||||
|
stmt.print(p, ctx);
|
||||||
|
}
|
||||||
|
Self::EmptyStatement(stmt) => {
|
||||||
|
p.print_statement_comments(stmt.span.start);
|
||||||
|
stmt.print(p, ctx);
|
||||||
|
}
|
||||||
|
Self::ExpressionStatement(stmt) => {
|
||||||
|
p.print_statement_comments(stmt.span.start);
|
||||||
|
stmt.print(p, ctx);
|
||||||
|
}
|
||||||
|
Self::ForInStatement(stmt) => {
|
||||||
|
p.print_statement_comments(stmt.span.start);
|
||||||
|
stmt.print(p, ctx);
|
||||||
|
}
|
||||||
|
Self::ForOfStatement(stmt) => {
|
||||||
|
p.print_statement_comments(stmt.span.start);
|
||||||
|
stmt.print(p, ctx);
|
||||||
|
}
|
||||||
|
Self::ForStatement(stmt) => {
|
||||||
|
p.print_statement_comments(stmt.span.start);
|
||||||
|
stmt.print(p, ctx);
|
||||||
|
}
|
||||||
|
Self::IfStatement(stmt) => {
|
||||||
|
p.print_statement_comments(stmt.span.start);
|
||||||
|
stmt.print(p, ctx);
|
||||||
|
}
|
||||||
|
Self::LabeledStatement(stmt) => {
|
||||||
|
p.print_statement_comments(stmt.span.start);
|
||||||
|
stmt.print(p, ctx);
|
||||||
|
}
|
||||||
|
Self::ReturnStatement(stmt) => {
|
||||||
|
p.print_statement_comments(stmt.span.start);
|
||||||
|
stmt.print(p, ctx);
|
||||||
|
}
|
||||||
|
Self::SwitchStatement(stmt) => {
|
||||||
|
p.print_statement_comments(stmt.span.start);
|
||||||
|
stmt.print(p, ctx);
|
||||||
|
}
|
||||||
|
Self::ThrowStatement(stmt) => {
|
||||||
|
p.print_statement_comments(stmt.span.start);
|
||||||
|
stmt.print(p, ctx);
|
||||||
|
}
|
||||||
|
Self::TryStatement(stmt) => {
|
||||||
|
p.print_statement_comments(stmt.span.start);
|
||||||
|
stmt.print(p, ctx);
|
||||||
|
}
|
||||||
|
Self::WhileStatement(stmt) => {
|
||||||
|
p.print_statement_comments(stmt.span.start);
|
||||||
|
stmt.print(p, ctx);
|
||||||
|
}
|
||||||
|
Self::WithStatement(stmt) => {
|
||||||
|
p.print_statement_comments(stmt.span.start);
|
||||||
|
stmt.print(p, ctx);
|
||||||
|
}
|
||||||
|
|
||||||
Self::ImportDeclaration(decl) => decl.print(p, ctx),
|
Self::ImportDeclaration(decl) => {
|
||||||
Self::ExportAllDeclaration(decl) => decl.print(p, ctx),
|
p.print_statement_comments(decl.span.start);
|
||||||
Self::ExportDefaultDeclaration(decl) => decl.print(p, ctx),
|
decl.print(p, ctx);
|
||||||
Self::ExportNamedDeclaration(decl) => decl.print(p, ctx),
|
}
|
||||||
Self::TSExportAssignment(decl) => decl.print(p, ctx),
|
Self::ExportAllDeclaration(decl) => {
|
||||||
Self::TSNamespaceExportDeclaration(decl) => decl.print(p, ctx),
|
p.print_statement_comments(decl.span.start);
|
||||||
|
decl.print(p, ctx);
|
||||||
|
}
|
||||||
|
Self::ExportDefaultDeclaration(decl) => {
|
||||||
|
p.print_statement_comments(decl.span.start);
|
||||||
|
decl.print(p, ctx);
|
||||||
|
}
|
||||||
|
Self::ExportNamedDeclaration(decl) => {
|
||||||
|
p.print_statement_comments(decl.span.start);
|
||||||
|
decl.print(p, ctx);
|
||||||
|
}
|
||||||
|
Self::TSExportAssignment(decl) => {
|
||||||
|
p.print_statement_comments(decl.span.start);
|
||||||
|
decl.print(p, ctx);
|
||||||
|
}
|
||||||
|
Self::TSNamespaceExportDeclaration(decl) => {
|
||||||
|
p.print_statement_comments(decl.span.start);
|
||||||
|
decl.print(p, ctx);
|
||||||
|
}
|
||||||
|
|
||||||
Self::VariableDeclaration(decl) => {
|
Self::VariableDeclaration(decl) => {
|
||||||
|
p.print_statement_comments(decl.span.start);
|
||||||
p.print_indent();
|
p.print_indent();
|
||||||
decl.print(p, ctx);
|
decl.print(p, ctx);
|
||||||
p.print_semicolon_after_statement();
|
p.print_semicolon_after_statement();
|
||||||
}
|
}
|
||||||
Self::FunctionDeclaration(decl) => {
|
Self::FunctionDeclaration(decl) => {
|
||||||
|
p.print_statement_comments(decl.span.start);
|
||||||
p.print_indent();
|
p.print_indent();
|
||||||
decl.print(p, ctx);
|
decl.print(p, ctx);
|
||||||
p.print_soft_newline();
|
p.print_soft_newline();
|
||||||
}
|
}
|
||||||
Self::ClassDeclaration(decl) => {
|
Self::ClassDeclaration(decl) => {
|
||||||
|
p.print_statement_comments(decl.span.start);
|
||||||
p.print_indent();
|
p.print_indent();
|
||||||
decl.print(p, ctx);
|
decl.print(p, ctx);
|
||||||
p.print_soft_newline();
|
p.print_soft_newline();
|
||||||
}
|
}
|
||||||
Self::TSModuleDeclaration(decl) => {
|
Self::TSModuleDeclaration(decl) => {
|
||||||
|
p.print_statement_comments(decl.span.start);
|
||||||
p.print_indent();
|
p.print_indent();
|
||||||
decl.print(p, ctx);
|
decl.print(p, ctx);
|
||||||
p.print_soft_newline();
|
p.print_soft_newline();
|
||||||
}
|
}
|
||||||
Self::TSTypeAliasDeclaration(decl) => {
|
Self::TSTypeAliasDeclaration(decl) => {
|
||||||
|
p.print_statement_comments(decl.span.start);
|
||||||
p.print_indent();
|
p.print_indent();
|
||||||
decl.print(p, ctx);
|
decl.print(p, ctx);
|
||||||
p.print_semicolon_after_statement();
|
p.print_semicolon_after_statement();
|
||||||
}
|
}
|
||||||
Self::TSInterfaceDeclaration(decl) => {
|
Self::TSInterfaceDeclaration(decl) => {
|
||||||
|
p.print_statement_comments(decl.span.start);
|
||||||
p.print_indent();
|
p.print_indent();
|
||||||
decl.print(p, ctx);
|
decl.print(p, ctx);
|
||||||
p.print_soft_newline();
|
p.print_soft_newline();
|
||||||
}
|
}
|
||||||
Self::TSEnumDeclaration(decl) => {
|
Self::TSEnumDeclaration(decl) => {
|
||||||
|
p.print_statement_comments(decl.span.start);
|
||||||
p.print_indent();
|
p.print_indent();
|
||||||
decl.print(p, ctx);
|
decl.print(p, ctx);
|
||||||
p.print_soft_newline();
|
p.print_soft_newline();
|
||||||
}
|
}
|
||||||
Self::TSImportEqualsDeclaration(decl) => {
|
Self::TSImportEqualsDeclaration(decl) => {
|
||||||
|
p.print_statement_comments(decl.span.start);
|
||||||
p.print_indent();
|
p.print_indent();
|
||||||
decl.print(p, ctx);
|
decl.print(p, ctx);
|
||||||
p.print_semicolon_after_statement();
|
p.print_semicolon_after_statement();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue