From d790ea7d533c3c48b09a2f16f3fef549aa96be78 Mon Sep 17 00:00:00 2001 From: Yiyu Lin Date: Thu, 13 Apr 2023 09:08:01 +0800 Subject: [PATCH] refactor(cli,ext,ops): cleanup `regex` with `lazy-regex` (#17296) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - bump deps: the newest `lazy-regex` need newer `oncecell` and `regex` - reduce `unwrap` - remove dep `lazy_static` - make more regex cached --------- Co-authored-by: Bartek IwaƄczuk --- Cargo.lock | 28 +++++++++++++++++++++++++++- Cargo.toml | 1 + cli/Cargo.toml | 3 ++- cli/build.rs | 4 +--- cli/lsp/analysis.rs | 2 +- cli/lsp/code_lens.rs | 7 +++---- cli/lsp/completions.rs | 2 +- cli/lsp/path_to_regex.rs | 2 +- cli/lsp/registries.rs | 5 ++--- cli/lsp/tsc.rs | 31 +++++++++++++------------------ cli/node/mod.rs | 4 +--- cli/tools/check.rs | 2 +- cli/tools/installer.rs | 2 +- cli/tools/test.rs | 16 ++++++++-------- cli/tools/upgrade.rs | 6 +++--- cli/tsc/diagnostics.rs | 9 ++++----- ext/node/Cargo.toml | 1 + ext/node/resolution.rs | 5 ++--- ops/Cargo.toml | 1 + ops/lib.rs | 28 +++++++++++----------------- test_util/Cargo.toml | 2 +- test_util/src/lib.rs | 18 +++++++----------- test_util/src/lsp.rs | 8 +++----- 23 files changed, 96 insertions(+), 91 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 132ae4c572..76b7c43884 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -761,6 +761,7 @@ dependencies = [ "indexmap", "jsonc-parser", "junction", + "lazy-regex", "libc", "log", "lsp-types", @@ -1156,6 +1157,7 @@ dependencies = [ "hkdf", "idna 0.3.0", "indexmap", + "lazy-regex", "libz-sys", "md-5", "md4", @@ -1201,6 +1203,7 @@ dependencies = [ name = "deno_ops" version = "0.58.0" dependencies = [ + "lazy-regex", "once_cell", "pmutil", "prettyplease", @@ -2523,6 +2526,29 @@ dependencies = [ "libc", ] +[[package]] +name = "lazy-regex" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff63c423c68ea6814b7da9e88ce585f793c87ddd9e78f646970891769c8235d4" +dependencies = [ + "lazy-regex-proc_macros", + "once_cell", + "regex", +] + +[[package]] +name = "lazy-regex-proc_macros" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8edfc11b8f56ce85e207e62ea21557cfa09bb24a8f6b04ae181b086ff8611c22" +dependencies = [ + "proc-macro2 1.0.56", + "quote 1.0.26", + "regex", + "syn 1.0.109", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -4765,7 +4791,7 @@ dependencies = [ "flate2", "futures", "hyper", - "lazy_static", + "lazy-regex", "lsp-types", "nix", "once_cell", diff --git a/Cargo.toml b/Cargo.toml index 49df110f0b..6d95adf39b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -110,6 +110,7 @@ pin-project = "1.0.11" # don't pin because they yank crates from cargo pretty_assertions = "=1.3.0" rand = "=0.8.5" regex = "^1.7.0" +lazy-regex = "2.5.0" reqwest = { version = "0.11.11", default-features = false, features = ["rustls-tls", "stream", "gzip", "brotli", "socks"] } ring = "=0.16.20" rusqlite = { version = "=0.28.0", features = ["unlock_notify", "bundled"] } diff --git a/cli/Cargo.toml b/cli/Cargo.toml index ac8f85bacc..ca4137c0dd 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -29,7 +29,7 @@ path = "./bench/lsp_bench_standalone.rs" [build-dependencies] deno_runtime = { workspace = true, features = ["snapshot_from_snapshot", "include_js_files_for_snapshotting"] } deno_core = { workspace = true, features = ["include_js_files_for_snapshotting"] } -regex.workspace = true +lazy-regex.workspace = true serde.workspace = true serde_json.workspace = true zstd.workspace = true @@ -78,6 +78,7 @@ http.workspace = true import_map = "=0.15.0" indexmap.workspace = true jsonc-parser = { version = "=0.21.0", features = ["serde"] } +lazy-regex.workspace = true libc.workspace = true log = { workspace = true, features = ["serde"] } lsp-types.workspace = true diff --git a/cli/build.rs b/cli/build.rs index 6d13f9a4d6..9be441bcc4 100644 --- a/cli/build.rs +++ b/cli/build.rs @@ -20,7 +20,6 @@ mod ts { use deno_core::op; use deno_core::OpState; use deno_runtime::deno_node::SUPPORTED_BUILTIN_NODE_MODULES; - use regex::Regex; use serde::Deserialize; use serde_json::json; use serde_json::Value; @@ -69,8 +68,7 @@ mod ts { fn op_load(state: &mut OpState, args: LoadArgs) -> Result { let op_crate_libs = state.borrow::>(); let path_dts = state.borrow::(); - let re_asset = - Regex::new(r"asset:/{3}lib\.(\S+)\.d\.ts").expect("bad regex"); + let re_asset = lazy_regex::regex!(r"asset:/{3}lib\.(\S+)\.d\.ts"); let build_specifier = "asset:///bootstrap.ts"; // we need a basic file to send to tsc to warm it up. diff --git a/cli/lsp/analysis.rs b/cli/lsp/analysis.rs index 23bfeccd12..a5ebbbbb81 100644 --- a/cli/lsp/analysis.rs +++ b/cli/lsp/analysis.rs @@ -53,7 +53,7 @@ static PREFERRED_FIXES: Lazy> = }); static IMPORT_SPECIFIER_RE: Lazy = - Lazy::new(|| Regex::new(r#"\sfrom\s+["']([^"']*)["']"#).unwrap()); + lazy_regex::lazy_regex!(r#"\sfrom\s+["']([^"']*)["']"#); const SUPPORTED_EXTENSIONS: &[&str] = &[".ts", ".tsx", ".js", ".jsx", ".mjs"]; diff --git a/cli/lsp/code_lens.rs b/cli/lsp/code_lens.rs index 650e5e2416..6327b7a9cf 100644 --- a/cli/lsp/code_lens.rs +++ b/cli/lsp/code_lens.rs @@ -21,6 +21,7 @@ use deno_core::serde::Serialize; use deno_core::serde_json; use deno_core::serde_json::json; use deno_core::ModuleSpecifier; +use lazy_regex::lazy_regex; use once_cell::sync::Lazy; use regex::Regex; use std::cell::RefCell; @@ -29,11 +30,9 @@ use std::rc::Rc; use std::sync::Arc; use tower_lsp::lsp_types as lsp; -static ABSTRACT_MODIFIER: Lazy = - Lazy::new(|| Regex::new(r"\babstract\b").unwrap()); +static ABSTRACT_MODIFIER: Lazy = lazy_regex!(r"\babstract\b"); -static EXPORT_MODIFIER: Lazy = - Lazy::new(|| Regex::new(r"\bexport\b").unwrap()); +static EXPORT_MODIFIER: Lazy = lazy_regex!(r"\bexport\b"); #[derive(Debug, Deserialize, Serialize)] pub enum CodeLensSource { diff --git a/cli/lsp/completions.rs b/cli/lsp/completions.rs index c40d6f2386..d91fc29c28 100644 --- a/cli/lsp/completions.rs +++ b/cli/lsp/completions.rs @@ -28,7 +28,7 @@ use std::sync::Arc; use tower_lsp::lsp_types as lsp; static FILE_PROTO_RE: Lazy = - Lazy::new(|| Regex::new(r#"^file:/{2}(?:/[A-Za-z]:)?"#).unwrap()); + lazy_regex::lazy_regex!(r#"^file:/{2}(?:/[A-Za-z]:)?"#); const CURRENT_PATH: &str = "."; const PARENT_PATH: &str = ".."; diff --git a/cli/lsp/path_to_regex.rs b/cli/lsp/path_to_regex.rs index dcad6b0394..01dbc0287a 100644 --- a/cli/lsp/path_to_regex.rs +++ b/cli/lsp/path_to_regex.rs @@ -37,7 +37,7 @@ use std::fmt::Write as _; use std::iter::Peekable; static ESCAPE_STRING_RE: Lazy = - Lazy::new(|| Regex::new(r"([.+*?=^!:${}()\[\]|/\\])").unwrap()); + lazy_regex::lazy_regex!(r"([.+*?=^!:${}()\[\]|/\\])"); #[derive(Debug, PartialEq, Eq)] enum TokenType { diff --git a/cli/lsp/registries.rs b/cli/lsp/registries.rs index d5bdd22ca3..f46aba44f2 100644 --- a/cli/lsp/registries.rs +++ b/cli/lsp/registries.rs @@ -33,7 +33,6 @@ use deno_runtime::deno_web::BlobStore; use deno_runtime::permissions::PermissionsContainer; use log::error; use once_cell::sync::Lazy; -use regex::Regex; use std::collections::HashMap; use std::path::Path; use tower_lsp::lsp_types as lsp; @@ -66,8 +65,8 @@ const COMPONENT: &percent_encoding::AsciiSet = &percent_encoding::CONTROLS const REGISTRY_IMPORT_COMMIT_CHARS: &[&str] = &["\"", "'", "/"]; -static REPLACEMENT_VARIABLE_RE: Lazy = - Lazy::new(|| Regex::new(r"\$\{\{?(\w+)\}?\}").unwrap()); +static REPLACEMENT_VARIABLE_RE: Lazy = + lazy_regex::lazy_regex!(r"\$\{\{?(\w+)\}?\}"); fn base_url(url: &Url) -> String { url.origin().ascii_serialization() diff --git a/cli/lsp/tsc.rs b/cli/lsp/tsc.rs index e236eee0a7..eba3c5b3e6 100644 --- a/cli/lsp/tsc.rs +++ b/cli/lsp/tsc.rs @@ -44,6 +44,7 @@ use deno_core::ModuleSpecifier; use deno_core::OpState; use deno_core::RuntimeOptions; use deno_runtime::tokio_util::create_basic_runtime; +use lazy_regex::lazy_regex; use once_cell::sync::Lazy; use regex::Captures; use regex::Regex; @@ -65,24 +66,18 @@ use tower_lsp::jsonrpc::Result as LspResult; use tower_lsp::lsp_types as lsp; static BRACKET_ACCESSOR_RE: Lazy = - Lazy::new(|| Regex::new(r#"^\[['"](.+)[\['"]\]$"#).unwrap()); -static CAPTION_RE: Lazy = Lazy::new(|| { - Regex::new(r"(.*?)\s*\r?\n((?:\s|\S)*)").unwrap() -}); -static CODEBLOCK_RE: Lazy = - Lazy::new(|| Regex::new(r"^\s*[~`]{3}").unwrap()); -static EMAIL_MATCH_RE: Lazy = - Lazy::new(|| Regex::new(r"(.+)\s<([-.\w]+@[-.\w]+)>").unwrap()); -static HTTP_RE: Lazy = - Lazy::new(|| Regex::new(r#"(?i)^https?:"#).unwrap()); -static JSDOC_LINKS_RE: Lazy = Lazy::new(|| { - Regex::new(r"(?i)\{@(link|linkplain|linkcode) (https?://[^ |}]+?)(?:[| ]([^{}\n]+?))?\}").unwrap() -}); -static PART_KIND_MODIFIER_RE: Lazy = - Lazy::new(|| Regex::new(r",|\s+").unwrap()); -static PART_RE: Lazy = - Lazy::new(|| Regex::new(r"^(\S+)\s*-?\s*").unwrap()); -static SCOPE_RE: Lazy = Lazy::new(|| Regex::new(r"scope_(\d)").unwrap()); + lazy_regex!(r#"^\[['"](.+)[\['"]\]$"#); +static CAPTION_RE: Lazy = + lazy_regex!(r"(.*?)\s*\r?\n((?:\s|\S)*)"); +static CODEBLOCK_RE: Lazy = lazy_regex!(r"^\s*[~`]{3}"); +static EMAIL_MATCH_RE: Lazy = lazy_regex!(r"(.+)\s<([-.\w]+@[-.\w]+)>"); +static HTTP_RE: Lazy = lazy_regex!(r#"(?i)^https?:"#); +static JSDOC_LINKS_RE: Lazy = lazy_regex!( + r"(?i)\{@(link|linkplain|linkcode) (https?://[^ |}]+?)(?:[| ]([^{}\n]+?))?\}" +); +static PART_KIND_MODIFIER_RE: Lazy = lazy_regex!(r",|\s+"); +static PART_RE: Lazy = lazy_regex!(r"^(\S+)\s*-?\s*"); +static SCOPE_RE: Lazy = lazy_regex!(r"scope_(\d)"); const FILE_EXTENSION_KIND_MODIFIERS: &[&str] = &[".d.ts", ".ts", ".tsx", ".js", ".jsx", ".json"]; diff --git a/cli/node/mod.rs b/cli/node/mod.rs index 28fd180da8..2207ce04e3 100644 --- a/cli/node/mod.rs +++ b/cli/node/mod.rs @@ -36,7 +36,6 @@ use deno_runtime::permissions::PermissionsContainer; use deno_semver::npm::NpmPackageNv; use deno_semver::npm::NpmPackageNvReference; use once_cell::sync::Lazy; -use regex::Regex; use crate::cache::NodeAnalysisCache; use crate::file_fetcher::FileFetcher; @@ -500,8 +499,7 @@ fn finalize_resolution( resolved: ModuleSpecifier, base: &ModuleSpecifier, ) -> Result { - // todo(dsherret): cache - let encoded_sep_re = Regex::new(r"%2F|%2C").unwrap(); + let encoded_sep_re = lazy_regex::regex!(r"%2F|%2C"); if encoded_sep_re.is_match(resolved.path()) { return Err(errors::err_invalid_module_specifier( diff --git a/cli/tools/check.rs b/cli/tools/check.rs index 804519e84f..91643496a4 100644 --- a/cli/tools/check.rs +++ b/cli/tools/check.rs @@ -321,7 +321,7 @@ fn get_tsc_roots( /// Matches the `@ts-check` pragma. static TS_CHECK_RE: Lazy = - Lazy::new(|| Regex::new(r#"(?i)^\s*@ts-check(?:\s+|$)"#).unwrap()); + lazy_regex::lazy_regex!(r#"(?i)^\s*@ts-check(?:\s+|$)"#); fn has_ts_check(media_type: MediaType, file_text: &str) -> bool { match &media_type { diff --git a/cli/tools/installer.rs b/cli/tools/installer.rs index 3bffbacada..ec397f78a3 100644 --- a/cli/tools/installer.rs +++ b/cli/tools/installer.rs @@ -35,7 +35,7 @@ static EXEC_NAME_RE: Lazy = Lazy::new(|| { RegexBuilder::new(r"^[a-z][\w-]*$") .case_insensitive(true) .build() - .unwrap() + .expect("invalid regex") }); fn validate_name(exec_name: &str) -> Result<(), AnyError> { diff --git a/cli/tools/test.rs b/cli/tools/test.rs index ce99a6edc9..7d6a6baa4b 100644 --- a/cli/tools/test.rs +++ b/cli/tools/test.rs @@ -1031,8 +1031,8 @@ fn extract_files_from_source_comments( scope_analysis: false, })?; let comments = parsed_source.comments().get_vec(); - let blocks_regex = Regex::new(r"```([^\r\n]*)\r?\n([\S\s]*?)```")?; - let lines_regex = Regex::new(r"(?:\* ?)(?:\# ?)?(.*)")?; + let blocks_regex = lazy_regex::regex!(r"```([^\r\n]*)\r?\n([\S\s]*?)```"); + let lines_regex = lazy_regex::regex!(r"(?:\* ?)(?:\# ?)?(.*)"); let files = comments .iter() @@ -1049,8 +1049,8 @@ fn extract_files_from_source_comments( &comment.text, media_type, parsed_source.text_info().line_index(comment.start()), - &blocks_regex, - &lines_regex, + blocks_regex, + lines_regex, ) }) .flatten() @@ -1069,16 +1069,16 @@ fn extract_files_from_fenced_blocks( // check can be done to see if a block is inside a comment (and skip typechecking) // or not by checking for the presence of capturing groups in the matches. let blocks_regex = - Regex::new(r"(?s)|```([^\r\n]*)\r?\n([\S\s]*?)```")?; - let lines_regex = Regex::new(r"(?:\# ?)?(.*)")?; + lazy_regex::regex!(r"(?s)|```([^\r\n]*)\r?\n([\S\s]*?)```"); + let lines_regex = lazy_regex::regex!(r"(?:\# ?)?(.*)"); extract_files_from_regex_blocks( specifier, source, media_type, /* file line index */ 0, - &blocks_regex, - &lines_regex, + blocks_regex, + lines_regex, ) } diff --git a/cli/tools/upgrade.rs b/cli/tools/upgrade.rs index 310d60d994..f16923bf83 100644 --- a/cli/tools/upgrade.rs +++ b/cli/tools/upgrade.rs @@ -289,9 +289,9 @@ pub async fn upgrade( let install_version = match upgrade_flags.version { Some(passed_version) => { - if upgrade_flags.canary - && !regex::Regex::new("^[0-9a-f]{40}$")?.is_match(&passed_version) - { + let re_hash = lazy_regex::regex!("^[0-9a-f]{40}$"); + + if upgrade_flags.canary && !re_hash.is_match(&passed_version) { bail!("Invalid commit hash passed"); } else if !upgrade_flags.canary && Version::parse_standard(&passed_version).is_err() diff --git a/cli/tsc/diagnostics.rs b/cli/tsc/diagnostics.rs index a865daa9d9..1e9819309e 100644 --- a/cli/tsc/diagnostics.rs +++ b/cli/tsc/diagnostics.rs @@ -6,6 +6,7 @@ use deno_core::serde::Deserialize; use deno_core::serde::Deserializer; use deno_core::serde::Serialize; use deno_core::serde::Serializer; +use lazy_regex::lazy_regex; use once_cell::sync::Lazy; use regex::Regex; use std::error::Error; @@ -36,13 +37,11 @@ const UNSTABLE_DENO_PROPS: &[&str] = &[ "osUptime", ]; -static MSG_MISSING_PROPERTY_DENO: Lazy = Lazy::new(|| { - Regex::new(r#"Property '([^']+)' does not exist on type 'typeof Deno'"#) - .unwrap() -}); +static MSG_MISSING_PROPERTY_DENO: Lazy = + lazy_regex!(r#"Property '([^']+)' does not exist on type 'typeof Deno'"#); static MSG_SUGGESTION: Lazy = - Lazy::new(|| Regex::new(r#" Did you mean '([^']+)'\?"#).unwrap()); + lazy_regex!(r#" Did you mean '([^']+)'\?"#); /// Potentially convert a "raw" diagnostic message from TSC to something that /// provides a more sensible error message given a Deno runtime context. diff --git a/ext/node/Cargo.toml b/ext/node/Cargo.toml index d63956c2ed..75d85ac0bc 100644 --- a/ext/node/Cargo.toml +++ b/ext/node/Cargo.toml @@ -24,6 +24,7 @@ hex.workspace = true hkdf.workspace = true idna = "0.3.0" indexmap.workspace = true +lazy-regex.workspace = true libz-sys = { version = "1.1.8", features = ["static"] } md-5 = "0.10.5" md4 = "0.10.2" diff --git a/ext/node/resolution.rs b/ext/node/resolution.rs index 3f9c5da264..1422ba6b02 100644 --- a/ext/node/resolution.rs +++ b/ext/node/resolution.rs @@ -10,7 +10,6 @@ use deno_core::serde_json::Map; use deno_core::serde_json::Value; use deno_core::url::Url; use deno_core::ModuleSpecifier; -use regex::Regex; use crate::errors; use crate::package_json::PackageJson; @@ -342,8 +341,8 @@ fn resolve_package_target_string( )); } let invalid_segment_re = - Regex::new(r"(^|\\|/)(\.\.?|node_modules)(\\|/|$)").expect("bad regex"); - let pattern_re = Regex::new(r"\*").expect("bad regex"); + lazy_regex::regex!(r"(^|\\|/)(\.\.?|node_modules)(\\|/|$)"); + let pattern_re = lazy_regex::regex!(r"\*"); if !target.starts_with("./") { if internal && !target.starts_with("../") && !target.starts_with('/') { let is_url = Url::parse(&target).is_ok(); diff --git a/ops/Cargo.toml b/ops/Cargo.toml index 55754108a7..b8bbfd07dd 100644 --- a/ops/Cargo.toml +++ b/ops/Cargo.toml @@ -15,6 +15,7 @@ path = "./lib.rs" proc-macro = true [dependencies] +lazy-regex.workspace = true once_cell.workspace = true pmutil = "0.5.3" proc-macro-crate = "1.1.3" diff --git a/ops/lib.rs b/ops/lib.rs index aee6c8c034..41f69d9fc8 100644 --- a/ops/lib.rs +++ b/ops/lib.rs @@ -1,7 +1,6 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. use attrs::Attributes; -use once_cell::sync::Lazy; use optimizer::BailoutReason; use optimizer::Optimizer; use proc_macro::TokenStream; @@ -9,7 +8,6 @@ use proc_macro2::Span; use proc_macro2::TokenStream as TokenStream2; use quote::quote; use quote::ToTokens; -use regex::Regex; use syn::parse; use syn::parse_macro_input; use syn::punctuated::Punctuated; @@ -859,30 +857,26 @@ fn is_unit_result(ty: impl ToTokens) -> bool { } fn is_resource_id(arg: impl ToTokens) -> bool { - static RE: Lazy = - Lazy::new(|| Regex::new(r#": (?:deno_core :: )?ResourceId$"#).unwrap()); - RE.is_match(&tokens(arg)) + let re = lazy_regex::regex!(r#": (?:deno_core :: )?ResourceId$"#); + re.is_match(&tokens(arg)) } fn is_mut_ref_opstate(arg: impl ToTokens) -> bool { - static RE: Lazy = - Lazy::new(|| Regex::new(r#": & mut (?:deno_core :: )?OpState$"#).unwrap()); - RE.is_match(&tokens(arg)) + let re = lazy_regex::regex!(r#": & mut (?:deno_core :: )?OpState$"#); + re.is_match(&tokens(arg)) } fn is_rc_refcell_opstate(arg: &syn::FnArg) -> bool { - static RE: Lazy = Lazy::new(|| { - Regex::new(r#": Rc < RefCell < (?:deno_core :: )?OpState > >$"#).unwrap() - }); - RE.is_match(&tokens(arg)) + let re = + lazy_regex::regex!(r#": Rc < RefCell < (?:deno_core :: )?OpState > >$"#); + re.is_match(&tokens(arg)) } fn is_handle_scope(arg: &syn::FnArg) -> bool { - static RE: Lazy = Lazy::new(|| { - Regex::new(r#": & mut (?:deno_core :: )?v8 :: HandleScope(?: < '\w+ >)?$"#) - .unwrap() - }); - RE.is_match(&tokens(arg)) + let re = lazy_regex::regex!( + r#": & mut (?:deno_core :: )?v8 :: HandleScope(?: < '\w+ >)?$"# + ); + re.is_match(&tokens(arg)) } fn is_future(ty: impl ToTokens) -> bool { diff --git a/test_util/Cargo.toml b/test_util/Cargo.toml index 68443e4921..cb1ea46cc2 100644 --- a/test_util/Cargo.toml +++ b/test_util/Cargo.toml @@ -22,7 +22,7 @@ console_static_text.workspace = true flate2.workspace = true futures.workspace = true hyper = { workspace = true, features = ["server", "http1", "http2", "runtime"] } -lazy_static = "1.4.0" +lazy-regex.workspace = true lsp-types.workspace = true nix.workspace = true once_cell.workspace = true diff --git a/test_util/src/lib.rs b/test_util/src/lib.rs index 0b6ce16156..6a6614ad0a 100644 --- a/test_util/src/lib.rs +++ b/test_util/src/lib.rs @@ -13,8 +13,8 @@ use hyper::Body; use hyper::Request; use hyper::Response; use hyper::StatusCode; -use lazy_static::lazy_static; use npm::CUSTOM_NPM_PACKAGE_CACHE; +use once_cell::sync::Lazy; use pretty_assertions::assert_eq; use pty::Pty; use regex::Regex; @@ -91,10 +91,8 @@ pub const PERMISSION_VARIANTS: [&str; 5] = ["read", "write", "env", "net", "run"]; pub const PERMISSION_DENIED_PATTERN: &str = "PermissionDenied"; -lazy_static! { - static ref GUARD: Mutex = - Mutex::new(HttpServerCount::default()); -} +static GUARD: Lazy> = + Lazy::new(|| Mutex::new(HttpServerCount::default())); pub fn env_vars_for_npm_tests_no_sync_download() -> Vec<(String, String)> { vec![ @@ -2176,12 +2174,10 @@ pub struct WrkOutput { } pub fn parse_wrk_output(output: &str) -> WrkOutput { - lazy_static! { - static ref REQUESTS_RX: Regex = - Regex::new(r"Requests/sec:\s+(\d+)").unwrap(); - static ref LATENCY_RX: Regex = - Regex::new(r"\s+99%(?:\s+(\d+.\d+)([a-z]+))").unwrap(); - } + static REQUESTS_RX: Lazy = + lazy_regex::lazy_regex!(r"Requests/sec:\s+(\d+)"); + static LATENCY_RX: Lazy = + lazy_regex::lazy_regex!(r"\s+99%(?:\s+(\d+.\d+)([a-z]+))"); let mut requests = None; let mut latency = None; diff --git a/test_util/src/lsp.rs b/test_util/src/lsp.rs index 786a2dee5d..3e9d0a80bb 100644 --- a/test_util/src/lsp.rs +++ b/test_util/src/lsp.rs @@ -9,7 +9,6 @@ use super::new_deno_dir; use super::TempDir; use anyhow::Result; -use lazy_static::lazy_static; use lsp_types as lsp; use lsp_types::ClientCapabilities; use lsp_types::ClientInfo; @@ -25,6 +24,7 @@ use lsp_types::TextDocumentClientCapabilities; use lsp_types::TextDocumentSyncClientCapabilities; use lsp_types::Url; use lsp_types::WorkspaceClientCapabilities; +use once_cell::sync::Lazy; use parking_lot::Condvar; use parking_lot::Mutex; use regex::Regex; @@ -48,10 +48,8 @@ use std::sync::Arc; use std::time::Duration; use std::time::Instant; -lazy_static! { - static ref CONTENT_TYPE_REG: Regex = - Regex::new(r"(?i)^content-length:\s+(\d+)").unwrap(); -} +static CONTENT_TYPE_REG: Lazy = + lazy_regex::lazy_regex!(r"(?i)^content-length:\s+(\d+)"); #[derive(Clone, Debug, Deserialize, Serialize)] pub struct LspResponseError {