Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(codegen)!: remove CommentOptions API
Browse files Browse the repository at this point in the history
Boshen committed Oct 11, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent e811812 commit 74b31cc
Showing 14 changed files with 100 additions and 149 deletions.
8 changes: 2 additions & 6 deletions crates/oxc/src/compiler.rs
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ use std::{mem, ops::ControlFlow, path::Path};

use oxc_allocator::Allocator;
use oxc_ast::ast::Program;
use oxc_codegen::{CodeGenerator, CodegenOptions, CodegenReturn, CommentOptions};
use oxc_codegen::{CodeGenerator, CodegenOptions, CodegenReturn};
use oxc_diagnostics::OxcDiagnostic;
use oxc_isolated_declarations::{IsolatedDeclarations, IsolatedDeclarationsOptions};
use oxc_mangler::{MangleOptions, Mangler};
@@ -283,11 +283,7 @@ pub trait CompilerInterface {
mangler: Option<Mangler>,
options: CodegenOptions,
) -> CodegenReturn {
let comment_options = CommentOptions { preserve_annotate_comments: true };
let mut codegen = CodeGenerator::new()
.with_options(options)
.with_mangler(mangler)
.enable_comment(program, comment_options);
let mut codegen = CodeGenerator::new().with_options(options).with_mangler(mangler);
if self.enable_sourcemap() {
codegen = codegen
.enable_source_map(source_path.to_string_lossy().as_ref(), program.source_text);
3 changes: 1 addition & 2 deletions crates/oxc_codegen/examples/codegen.rs
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
use std::{env, path::Path};

use oxc_allocator::Allocator;
use oxc_codegen::{CodeGenerator, CodegenOptions, CommentOptions};
use oxc_codegen::{CodeGenerator, CodegenOptions};
use oxc_parser::{Parser, ParserReturn};
use oxc_span::SourceType;
use pico_args::Arguments;
@@ -61,7 +61,6 @@ fn parse<'a>(

fn codegen(ret: &ParserReturn<'_>, minify: bool) -> String {
CodeGenerator::new()
.enable_comment(&ret.program, CommentOptions { preserve_annotate_comments: true })
.with_options(CodegenOptions { minify, ..CodegenOptions::default() })
.build(&ret.program)
.code
53 changes: 21 additions & 32 deletions crates/oxc_codegen/src/comment.rs
Original file line number Diff line number Diff line change
@@ -16,10 +16,6 @@ static ANNOTATION_MATCHER: Lazy<DoubleArrayAhoCorasick<usize>> = Lazy::new(|| {
pub(crate) type CommentsMap = FxHashMap</* attached_to */ u32, Vec<Comment>>;

impl<'a> Codegen<'a> {
pub(crate) fn preserve_annotate_comments(&self) -> bool {
self.comment_options.preserve_annotate_comments && !self.options.minify
}

pub(crate) fn build_comments(&mut self, comments: &[Comment]) {
for comment in comments {
self.comments.entry(comment.attached_to).or_default().push(*comment);
@@ -31,35 +27,33 @@ impl<'a> Codegen<'a> {
}

pub(crate) fn has_annotation_comment(&self, start: u32) -> bool {
if !self.preserve_annotate_comments() {
if !self.options.print_annotation_comments() {
return false;
}
let Some(source_text) = self.source_text else { return false };
self.comments.get(&start).is_some_and(|comments| {
comments.iter().any(|comment| Self::is_annotation_comment(comment, source_text))
comments.iter().any(|comment| self.is_annotation_comment(comment))
})
}

pub(crate) fn has_non_annotation_comment(&self, start: u32) -> bool {
if !self.preserve_annotate_comments() {
if !self.options.print_annotation_comments() {
return self.has_comment(start);
}
let Some(source_text) = self.source_text else { return false };
self.comments.get(&start).is_some_and(|comments| {
comments.iter().any(|comment| !Self::is_annotation_comment(comment, source_text))
comments.iter().any(|comment| !self.is_annotation_comment(comment))
})
}

/// Weather to keep leading comments.
fn is_leading_comments(comment: &Comment, source_text: &str) -> bool {
(comment.is_jsdoc(source_text) || (comment.is_line() && Self::is_annotation_comment(comment, source_text)))
fn is_leading_comments(&self, comment: &Comment) -> bool {
(comment.is_jsdoc(self.source_text) || (comment.is_line() && self.is_annotation_comment(comment)))
&& comment.preceded_by_newline
// webpack comment `/*****/`
&& !comment.span.source_text(source_text).chars().all(|c| c == '*')
&& !comment.span.source_text(self.source_text).chars().all(|c| c == '*')
}

fn print_comment(&mut self, comment: &Comment, source_text: &str) {
let comment_source = comment.real_span().source_text(source_text);
fn print_comment(&mut self, comment: &Comment) {
let comment_source = comment.real_span().source_text(self.source_text);
match comment.kind {
CommentKind::Line => {
self.print_str(comment_source);
@@ -84,14 +78,12 @@ impl<'a> Codegen<'a> {
if self.options.minify {
return;
}
let Some(source_text) = self.source_text else { return };
let Some(comments) = self.comments.remove(&start) else {
return;
};

let (comments, unused_comments): (Vec<_>, Vec<_>) = comments
.into_iter()
.partition(|comment| Self::is_leading_comments(comment, source_text));
let (comments, unused_comments): (Vec<_>, Vec<_>) =
comments.into_iter().partition(|comment| self.is_leading_comments(comment));

if comments.first().is_some_and(|c| c.preceded_by_newline) {
// Skip printing newline if this comment is already on a newline.
@@ -107,7 +99,7 @@ impl<'a> Codegen<'a> {
self.print_indent();
}

self.print_comment(comment, source_text);
self.print_comment(comment);
}

if comments.last().is_some_and(|c| c.is_line() || c.followed_by_newline) {
@@ -120,32 +112,31 @@ impl<'a> Codegen<'a> {
}
}

fn is_annotation_comment(comment: &Comment, source_text: &str) -> bool {
let comment_content = comment.span.source_text(source_text);
fn is_annotation_comment(&self, comment: &Comment) -> bool {
let comment_content = comment.span.source_text(self.source_text);
ANNOTATION_MATCHER.find_iter(comment_content).count() != 0
}

pub(crate) fn print_annotation_comments(&mut self, node_start: u32) {
if !self.preserve_annotate_comments() {
if !self.options.print_annotation_comments() {
return;
}

// If there is has annotation comments awaiting move to here, print them.
let start = self.start_of_annotation_comment.take().unwrap_or(node_start);

let Some(source_text) = self.source_text else { return };
let Some(comments) = self.comments.remove(&start) else { return };

for comment in comments {
if !Self::is_annotation_comment(&comment, source_text) {
if !self.is_annotation_comment(&comment) {
continue;
}
if comment.is_line() {
self.print_str("/*");
self.print_str(comment.span.source_text(source_text));
self.print_str(comment.span.source_text(self.source_text));
self.print_str("*/");
} else {
self.print_str(comment.real_span().source_text(source_text));
self.print_str(comment.real_span().source_text(self.source_text));
}
self.print_hard_space();
}
@@ -155,12 +146,10 @@ impl<'a> Codegen<'a> {
if self.options.minify {
return false;
}
let Some(source_text) = self.source_text else { return false };
let Some(comments) = self.comments.remove(&start) else { return false };

let (annotation_comments, comments): (Vec<_>, Vec<_>) = comments
.into_iter()
.partition(|comment| Self::is_annotation_comment(comment, source_text));
let (annotation_comments, comments): (Vec<_>, Vec<_>) =
comments.into_iter().partition(|comment| self.is_annotation_comment(comment));

if !annotation_comments.is_empty() {
self.comments.insert(start, annotation_comments);
@@ -169,7 +158,7 @@ impl<'a> Codegen<'a> {
for comment in &comments {
self.print_hard_newline();
self.print_indent();
self.print_comment(comment, source_text);
self.print_comment(comment);
}

if comments.is_empty() {
11 changes: 4 additions & 7 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{borrow::Cow, ops::Not};
use std::ops::Not;

use cow_utils::CowUtils;
#[allow(clippy::wildcard_imports)]
@@ -558,7 +558,7 @@ impl<'a> Gen for VariableDeclaration<'a> {
p.print_str("declare ");
}

if p.preserve_annotate_comments()
if p.options.print_annotation_comments()
&& p.start_of_annotation_comment.is_none()
&& matches!(self.kind, VariableDeclarationKind::Const)
&& matches!(self.declarations.first(), Some(VariableDeclarator { init: Some(init), .. }) if init.is_function())
@@ -825,7 +825,7 @@ impl<'a> Gen for ExportNamedDeclaration<'a> {
p.add_source_mapping(self.span.start);
p.print_indent();

if p.preserve_annotate_comments() {
if p.options.print_annotation_comments() {
match &self.declaration {
Some(Declaration::FunctionDeclaration(_)) => {
p.print_annotation_comments(self.span.start);
@@ -1195,10 +1195,7 @@ impl<'a> Gen for RegExpLiteral<'a> {
fn gen(&self, p: &mut Codegen, _ctx: Context) {
p.add_source_mapping(self.span.start);
let last = p.peek_nth(0);
let pattern_text = p.source_text.map_or_else(
|| Cow::Owned(self.regex.pattern.to_string()),
|src| self.regex.pattern.source_text(src),
);
let pattern_text = self.regex.pattern.source_text(p.source_text);
// Avoid forming a single-line comment or "</script" sequence
if Some('/') == last
|| (Some('<') == last && pattern_text.cow_to_lowercase().starts_with("script"))
Loading

0 comments on commit 74b31cc

Please sign in to comment.