1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-28 16:20:57 -05:00

refactor(cli,ext,ops): cleanup regex with lazy-regex (#17296)

- 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 <biwanczuk@gmail.com>
This commit is contained in:
Yiyu Lin 2023-04-13 09:08:01 +08:00 committed by GitHub
parent 19c3e4f6dc
commit d790ea7d53
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 96 additions and 91 deletions

28
Cargo.lock generated
View file

@ -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",

View file

@ -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"] }

View file

@ -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

View file

@ -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<Value, AnyError> {
let op_crate_libs = state.borrow::<HashMap<&str, PathBuf>>();
let path_dts = state.borrow::<PathBuf>();
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.

View file

@ -53,7 +53,7 @@ static PREFERRED_FIXES: Lazy<HashMap<&'static str, (u32, bool)>> =
});
static IMPORT_SPECIFIER_RE: Lazy<Regex> =
Lazy::new(|| Regex::new(r#"\sfrom\s+["']([^"']*)["']"#).unwrap());
lazy_regex::lazy_regex!(r#"\sfrom\s+["']([^"']*)["']"#);
const SUPPORTED_EXTENSIONS: &[&str] = &[".ts", ".tsx", ".js", ".jsx", ".mjs"];

View file

@ -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<Regex> =
Lazy::new(|| Regex::new(r"\babstract\b").unwrap());
static ABSTRACT_MODIFIER: Lazy<Regex> = lazy_regex!(r"\babstract\b");
static EXPORT_MODIFIER: Lazy<Regex> =
Lazy::new(|| Regex::new(r"\bexport\b").unwrap());
static EXPORT_MODIFIER: Lazy<Regex> = lazy_regex!(r"\bexport\b");
#[derive(Debug, Deserialize, Serialize)]
pub enum CodeLensSource {

View file

@ -28,7 +28,7 @@ use std::sync::Arc;
use tower_lsp::lsp_types as lsp;
static FILE_PROTO_RE: Lazy<Regex> =
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 = "..";

View file

@ -37,7 +37,7 @@ use std::fmt::Write as _;
use std::iter::Peekable;
static ESCAPE_STRING_RE: Lazy<Regex> =
Lazy::new(|| Regex::new(r"([.+*?=^!:${}()\[\]|/\\])").unwrap());
lazy_regex::lazy_regex!(r"([.+*?=^!:${}()\[\]|/\\])");
#[derive(Debug, PartialEq, Eq)]
enum TokenType {

View file

@ -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<Regex> =
Lazy::new(|| Regex::new(r"\$\{\{?(\w+)\}?\}").unwrap());
static REPLACEMENT_VARIABLE_RE: Lazy<regex::Regex> =
lazy_regex::lazy_regex!(r"\$\{\{?(\w+)\}?\}");
fn base_url(url: &Url) -> String {
url.origin().ascii_serialization()

View file

@ -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<Regex> =
Lazy::new(|| Regex::new(r#"^\[['"](.+)[\['"]\]$"#).unwrap());
static CAPTION_RE: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"<caption>(.*?)</caption>\s*\r?\n((?:\s|\S)*)").unwrap()
});
static CODEBLOCK_RE: Lazy<Regex> =
Lazy::new(|| Regex::new(r"^\s*[~`]{3}").unwrap());
static EMAIL_MATCH_RE: Lazy<Regex> =
Lazy::new(|| Regex::new(r"(.+)\s<([-.\w]+@[-.\w]+)>").unwrap());
static HTTP_RE: Lazy<Regex> =
Lazy::new(|| Regex::new(r#"(?i)^https?:"#).unwrap());
static JSDOC_LINKS_RE: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"(?i)\{@(link|linkplain|linkcode) (https?://[^ |}]+?)(?:[| ]([^{}\n]+?))?\}").unwrap()
});
static PART_KIND_MODIFIER_RE: Lazy<Regex> =
Lazy::new(|| Regex::new(r",|\s+").unwrap());
static PART_RE: Lazy<Regex> =
Lazy::new(|| Regex::new(r"^(\S+)\s*-?\s*").unwrap());
static SCOPE_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"scope_(\d)").unwrap());
lazy_regex!(r#"^\[['"](.+)[\['"]\]$"#);
static CAPTION_RE: Lazy<Regex> =
lazy_regex!(r"<caption>(.*?)</caption>\s*\r?\n((?:\s|\S)*)");
static CODEBLOCK_RE: Lazy<Regex> = lazy_regex!(r"^\s*[~`]{3}");
static EMAIL_MATCH_RE: Lazy<Regex> = lazy_regex!(r"(.+)\s<([-.\w]+@[-.\w]+)>");
static HTTP_RE: Lazy<Regex> = lazy_regex!(r#"(?i)^https?:"#);
static JSDOC_LINKS_RE: Lazy<Regex> = lazy_regex!(
r"(?i)\{@(link|linkplain|linkcode) (https?://[^ |}]+?)(?:[| ]([^{}\n]+?))?\}"
);
static PART_KIND_MODIFIER_RE: Lazy<Regex> = lazy_regex!(r",|\s+");
static PART_RE: Lazy<Regex> = lazy_regex!(r"^(\S+)\s*-?\s*");
static SCOPE_RE: Lazy<Regex> = lazy_regex!(r"scope_(\d)");
const FILE_EXTENSION_KIND_MODIFIERS: &[&str] =
&[".d.ts", ".ts", ".tsx", ".js", ".jsx", ".json"];

View file

@ -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<ModuleSpecifier, AnyError> {
// 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(

View file

@ -321,7 +321,7 @@ fn get_tsc_roots(
/// Matches the `@ts-check` pragma.
static TS_CHECK_RE: Lazy<Regex> =
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 {

View file

@ -35,7 +35,7 @@ static EXEC_NAME_RE: Lazy<Regex> = Lazy::new(|| {
RegexBuilder::new(r"^[a-z][\w-]*$")
.case_insensitive(true)
.build()
.unwrap()
.expect("invalid regex")
});
fn validate_name(exec_name: &str) -> Result<(), AnyError> {

View file

@ -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,
)
}

View file

@ -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()

View file

@ -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<Regex> = Lazy::new(|| {
Regex::new(r#"Property '([^']+)' does not exist on type 'typeof Deno'"#)
.unwrap()
});
static MSG_MISSING_PROPERTY_DENO: Lazy<Regex> =
lazy_regex!(r#"Property '([^']+)' does not exist on type 'typeof Deno'"#);
static MSG_SUGGESTION: Lazy<Regex> =
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.

View file

@ -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"

View file

@ -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<Fs: NodeFs>(
));
}
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();

View file

@ -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"

View file

@ -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<Regex> =
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<Regex> =
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<Regex> = 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<Regex> = 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 {

View file

@ -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

View file

@ -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<HttpServerCount> =
Mutex::new(HttpServerCount::default());
}
static GUARD: Lazy<Mutex<HttpServerCount>> =
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<Regex> =
lazy_regex::lazy_regex!(r"Requests/sec:\s+(\d+)");
static LATENCY_RX: Lazy<Regex> =
lazy_regex::lazy_regex!(r"\s+99%(?:\s+(\d+.\d+)([a-z]+))");
let mut requests = None;
let mut latency = None;

View file

@ -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<Regex> =
lazy_regex::lazy_regex!(r"(?i)^content-length:\s+(\d+)");
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct LspResponseError {