Skip to content
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

codegen: clean up API #6122

Closed
Boshen opened this issue Sep 27, 2024 · 2 comments
Closed

codegen: clean up API #6122

Boshen opened this issue Sep 27, 2024 · 2 comments
Assignees
Labels
C-enhancement Category - New feature or request

Comments

@Boshen
Copy link
Member

Boshen commented Sep 27, 2024

/// Initialize the output code buffer to reduce memory reallocation.
/// Minification will reduce by at least half of the original size.
#[must_use]
pub fn with_capacity(mut self, source_text_len: usize) -> Self {
let capacity = if self.options.minify { source_text_len / 2 } else { source_text_len };
// ensure space for at least `capacity` additional bytes without clobbering existing
// allocations.
self.code.reserve(capacity);
self
}
#[must_use]
pub fn with_options(mut self, options: CodegenOptions) -> Self {
self.options = options;
self.quote = if options.single_quote { b'\'' } else { b'"' };
self
}
/// Adds the source text of the original AST.
///
/// The source code will be used with comments or for improving the generated output. It also
/// pre-allocates memory for the output code using [`Codegen::with_capacity`]. Note that if you
/// use this method alongside your own call to [`Codegen::with_capacity`], the larger of the
/// two will be used.
#[must_use]
pub fn with_source_text(mut self, source_text: &'a str) -> Self {
self.source_text = Some(source_text);
self.with_capacity(source_text.len())
}
/// Also sets the [Self::with_source_text]
#[must_use]
pub fn enable_comment(
mut self,
source_text: &'a str,
trivias: Trivias,
options: CommentOptions,
) -> Self {
self.comment_options = options;
self.build_comments(&trivias);
self.trivias = trivias;
self.with_source_text(source_text)
}
#[must_use]
pub fn enable_source_map(mut self, source_name: &str, source_text: &str) -> Self {
let mut sourcemap_builder = SourcemapBuilder::default();
sourcemap_builder.with_name_and_source(source_name, source_text);
self.sourcemap_builder = Some(sourcemap_builder);
self
}
#[must_use]
pub fn with_mangler(mut self, mangler: Option<Mangler>) -> Self {
self.mangler = mangler;
self
}

I find these APIs confusing to use.

The intention was to only pass in comments if you want to enable comments, but now we have 2 options:

#[derive(Default, Clone, Copy)]
pub struct CodegenOptions {
/// Use single quotes instead of double quotes.
///
/// Default is `false`.
pub single_quote: bool,
/// Remove whitespace.
///
/// Default is `false`.
pub minify: bool,
}
#[derive(Default, Clone, Copy)]
pub struct CommentOptions {
/// Enable preserve annotate comments, like `/* #__PURE__ */` and `/* #__NO_SIDE_EFFECTS__ */`.
pub preserve_annotate_comments: bool,
}

These builder patterns don't scale well. I'm stuck trying to add a enable_comments API.

Enabling and disabling features, and passing in their required data are confusingly mixed together :-/

@Boshen Boshen added the C-enhancement Category - New feature or request label Sep 27, 2024
@Boshen Boshen self-assigned this Sep 28, 2024
@Boshen
Copy link
Member Author

Boshen commented Oct 10, 2024

It would be easier to make the change if #6426

@Boshen Boshen closed this as completed Oct 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category - New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant