Skip to content

Commit

Permalink
cleanup(ops): match variations with regexes (denoland#14888)
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronO authored Jun 16, 2022
1 parent 231b8fe commit fa6274c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions ops/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ path = "./lib.rs"
proc-macro = true

[dependencies]
once_cell = "1.10.0"
proc-macro-crate = "1.1.3"
proc-macro2 = "1"
quote = "1"
regex = "1.5.6"
syn = { version = "1", features = ["full", "extra-traits"] }
25 changes: 14 additions & 11 deletions ops/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
use once_cell::sync::Lazy;
use proc_macro::TokenStream;
use proc_macro2::Span;
use proc_macro2::TokenStream as TokenStream2;
use proc_macro_crate::crate_name;
use proc_macro_crate::FoundCrate;
use quote::quote;
use quote::ToTokens;
use regex::Regex;
use syn::punctuated::Punctuated;
use syn::token::Comma;
use syn::FnArg;
Expand Down Expand Up @@ -412,23 +414,24 @@ fn is_unit_result(ty: impl ToTokens) -> bool {
}

fn is_mut_ref_opstate(arg: &syn::FnArg) -> bool {
tokens(arg).ends_with(": & mut OpState")
|| tokens(arg).ends_with(": & mut deno_core :: OpState")
|| tokens(arg).ends_with("mut OpState")
static RE: Lazy<Regex> =
Lazy::new(|| Regex::new(r#": & mut (?:deno_core :: )?OpState$"#).unwrap());
RE.is_match(&tokens(arg))
}

fn is_rc_refcell_opstate(arg: &syn::FnArg) -> bool {
tokens(arg).ends_with(": Rc < RefCell < OpState > >")
|| tokens(arg).ends_with(": Rc < RefCell < deno_core :: OpState > >")
static RE: Lazy<Regex> = Lazy::new(|| {
Regex::new(r#": Rc < RefCell < (?:deno_core :: )?OpState > >$"#).unwrap()
});
RE.is_match(&tokens(arg))
}

fn is_handle_scope(arg: &syn::FnArg) -> bool {
tokens(arg).ends_with(": & mut v8 :: HandleScope")
|| tokens(arg).ends_with(": & mut v8 :: HandleScope < 'a >")
|| tokens(arg).ends_with(": & mut deno_core :: v8 :: HandleScope")
|| tokens(arg).ends_with(": & mut deno_core :: v8 :: HandleScope < 'a >")
|| tokens(arg).contains("mut v8 :: HandleScope")
|| tokens(arg).ends_with(": & mut v8 :: HandeScope < 'scope >")
static RE: Lazy<Regex> = Lazy::new(|| {
Regex::new(r#": & mut (?:deno_core :: )?v8 :: HandleScope(?: < '\w+ >)?$"#)
.unwrap()
});
RE.is_match(&tokens(arg))
}

fn is_future(ty: impl ToTokens) -> bool {
Expand Down

0 comments on commit fa6274c

Please sign in to comment.