Skip to content

Commit

Permalink
refactor(codegen): replace daachorse with string match for annotati…
Browse files Browse the repository at this point in the history
…on comment (#7064)
  • Loading branch information
Boshen authored Nov 2, 2024
1 parent 413973d commit dd79c1b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 28 deletions.
8 changes: 0 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 0 additions & 2 deletions crates/oxc_codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
30 changes: 15 additions & 15 deletions crates/oxc_codegen/src/comment.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
use daachorse::DoubleArrayAhoCorasick;
use once_cell::sync::Lazy;
use rustc_hash::FxHashMap;

use oxc_ast::{Comment, CommentKind};
use oxc_syntax::identifier::is_line_terminator;

use crate::{Codegen, LegalComment};

static ANNOTATION_MATCHER: Lazy<DoubleArrayAhoCorasick<usize>> = Lazy::new(|| {
let patterns = vec!["#__NO_SIDE_EFFECTS__", "@__NO_SIDE_EFFECTS__", "@__PURE__", "#__PURE__"];

DoubleArrayAhoCorasick::new(patterns).unwrap()
});

pub(crate) type CommentsMap = FxHashMap</* attached_to */ u32, Vec<Comment>>;

impl<'a> Codegen<'a> {
Expand All @@ -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
///
/// <https://github.com/javascript-compiler-hints/compiler-notations-spec/blob/main/pure-notation-spec.md>
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.
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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| {
Expand Down

0 comments on commit dd79c1b

Please sign in to comment.