mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
refactor(codegen): replace daachorse with string match for annotation comment (#7064)
This commit is contained in:
parent
413973df66
commit
dd79c1bfb3
5 changed files with 17 additions and 28 deletions
8
Cargo.lock
generated
8
Cargo.lock
generated
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -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<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> {
|
||||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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| {
|
||||
|
|
|
|||
Loading…
Reference in a new issue