diff --git a/Cargo.lock b/Cargo.lock index 4fe54bce3..d9f881c3e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -441,12 +441,6 @@ dependencies = [ "syn", ] -[[package]] -name = "daachorse" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "63b7ef7a4be509357f4804d0a22e830daddb48f19fd604e4ad32ddce04a94c36" - [[package]] name = "dashmap" version = "5.5.3" @@ -1541,10 +1535,8 @@ dependencies = [ "base64", "bitflags 2.6.0", "cow-utils", - "daachorse", "insta", "nonmax", - "once_cell", "oxc_allocator", "oxc_ast", "oxc_index", diff --git a/Cargo.toml b/Cargo.toml index 51a27aa6e..281a8d654 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -136,7 +136,6 @@ console_error_panic_hook = "0.1.7" convert_case = "0.6.0" cow-utils = "0.1.3" criterion2 = { version = "1.1.1", default-features = false } -daachorse = { version = "1.0.0" } dashmap = "6.1.0" encoding_rs = "0.8.34" encoding_rs_io = "0.1.7" diff --git a/crates/oxc_codegen/Cargo.toml b/crates/oxc_codegen/Cargo.toml index 7ccae5885..206da14fc 100644 --- a/crates/oxc_codegen/Cargo.toml +++ b/crates/oxc_codegen/Cargo.toml @@ -31,9 +31,7 @@ oxc_syntax = { workspace = true, features = ["to_js_string"] } assert-unchecked = { workspace = true } bitflags = { workspace = true } cow-utils = { workspace = true } -daachorse = { workspace = true } nonmax = { workspace = true } -once_cell = { workspace = true } rustc-hash = { workspace = true } [dev-dependencies] diff --git a/crates/oxc_codegen/src/comment.rs b/crates/oxc_codegen/src/comment.rs index d204c8dd8..1069bb6b5 100644 --- a/crates/oxc_codegen/src/comment.rs +++ b/crates/oxc_codegen/src/comment.rs @@ -1,5 +1,3 @@ -use daachorse::DoubleArrayAhoCorasick; -use once_cell::sync::Lazy; use rustc_hash::FxHashMap; use oxc_ast::{Comment, CommentKind}; @@ -7,12 +5,6 @@ use oxc_syntax::identifier::is_line_terminator; use crate::{Codegen, LegalComment}; -static ANNOTATION_MATCHER: Lazy> = Lazy::new(|| { - let patterns = vec!["#__NO_SIDE_EFFECTS__", "@__NO_SIDE_EFFECTS__", "@__PURE__", "#__PURE__"]; - - DoubleArrayAhoCorasick::new(patterns).unwrap() -}); - pub(crate) type CommentsMap = FxHashMap>; impl<'a> Codegen<'a> { @@ -36,17 +28,25 @@ impl<'a> Codegen<'a> { } pub(crate) fn has_non_annotation_comment(&self, start: u32) -> bool { - if !self.options.print_annotation_comments() { - return self.has_comment(start); + if self.options.print_annotation_comments() { + self.comments.get(&start).is_some_and(|comments| { + comments.iter().any(|comment| !self.is_annotation_comment(comment)) + }) + } else { + self.has_comment(start) } - self.comments.get(&start).is_some_and(|comments| { - comments.iter().any(|comment| !self.is_annotation_comment(comment)) - }) } + /// `#__PURE__` Notation Specification + /// + /// 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 + let s = comment.span.source_text(self.source_text).trim_start(); + if let Some(s) = s.strip_prefix(['@', '#']) { + s.starts_with("__PURE__") || s.starts_with("__NO_SIDE_EFFECTS__") + } else { + false + } } /// Whether to keep leading comments. diff --git a/crates/oxc_codegen/src/gen.rs b/crates/oxc_codegen/src/gen.rs index e9d60de5a..a7c81cc86 100644 --- a/crates/oxc_codegen/src/gen.rs +++ b/crates/oxc_codegen/src/gen.rs @@ -1329,7 +1329,7 @@ impl<'a> GenExpr for CallExpression<'a> { fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, ctx: Context) { let is_export_default = p.start_of_default_export == p.code_len(); let mut wrap = precedence >= Precedence::New || ctx.intersects(Context::FORBID_CALL); - if p.has_annotation_comment(self.span.start) && precedence >= Precedence::Postfix { + if precedence >= Precedence::Postfix && p.has_annotation_comment(self.span.start) { wrap = true; } @@ -2040,7 +2040,7 @@ impl<'a> GenExpr for ChainExpression<'a> { impl<'a> GenExpr for NewExpression<'a> { fn gen_expr(&self, p: &mut Codegen, precedence: Precedence, ctx: Context) { let mut wrap = precedence >= self.precedence(); - if p.has_annotation_comment(self.span.start) && precedence >= Precedence::Postfix { + if precedence >= Precedence::Postfix && p.has_annotation_comment(self.span.start) { wrap = true; } p.wrap(wrap, |p| {