diff options
author | Yiyu Lin <linyiyu1992@gmail.com> | 2023-04-13 09:08:01 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-13 03:08:01 +0200 |
commit | d790ea7d533c3c48b09a2f16f3fef549aa96be78 (patch) | |
tree | b31fc35baf1f634054f52e94626f0399d187b99b /cli | |
parent | 19c3e4f6dc31fd78e2793d0596d6a9cc3a30580a (diff) |
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>
Diffstat (limited to 'cli')
-rw-r--r-- | cli/Cargo.toml | 3 | ||||
-rw-r--r-- | cli/build.rs | 4 | ||||
-rw-r--r-- | cli/lsp/analysis.rs | 2 | ||||
-rw-r--r-- | cli/lsp/code_lens.rs | 7 | ||||
-rw-r--r-- | cli/lsp/completions.rs | 2 | ||||
-rw-r--r-- | cli/lsp/path_to_regex.rs | 2 | ||||
-rw-r--r-- | cli/lsp/registries.rs | 5 | ||||
-rw-r--r-- | cli/lsp/tsc.rs | 31 | ||||
-rw-r--r-- | cli/node/mod.rs | 4 | ||||
-rw-r--r-- | cli/tools/check.rs | 2 | ||||
-rw-r--r-- | cli/tools/installer.rs | 2 | ||||
-rw-r--r-- | cli/tools/test.rs | 16 | ||||
-rw-r--r-- | cli/tools/upgrade.rs | 6 | ||||
-rw-r--r-- | cli/tsc/diagnostics.rs | 9 |
14 files changed, 42 insertions, 53 deletions
diff --git a/cli/Cargo.toml b/cli/Cargo.toml index ac8f85bac..ca4137c0d 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 6d13f9a4d..9be441bcc 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<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. diff --git a/cli/lsp/analysis.rs b/cli/lsp/analysis.rs index 23bfeccd1..a5ebbbbb8 100644 --- a/cli/lsp/analysis.rs +++ b/cli/lsp/analysis.rs @@ -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"]; diff --git a/cli/lsp/code_lens.rs b/cli/lsp/code_lens.rs index 650e5e241..6327b7a9c 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<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 { diff --git a/cli/lsp/completions.rs b/cli/lsp/completions.rs index c40d6f238..d91fc29c2 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<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 = ".."; diff --git a/cli/lsp/path_to_regex.rs b/cli/lsp/path_to_regex.rs index dcad6b039..01dbc0287 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<Regex> = - 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 d5bdd22ca..f46aba44f 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<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() diff --git a/cli/lsp/tsc.rs b/cli/lsp/tsc.rs index e236eee0a..eba3c5b3e 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<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"]; diff --git a/cli/node/mod.rs b/cli/node/mod.rs index 28fd180da..2207ce04e 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<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( diff --git a/cli/tools/check.rs b/cli/tools/check.rs index 804519e84..91643496a 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<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 { diff --git a/cli/tools/installer.rs b/cli/tools/installer.rs index 3bffbacad..ec397f78a 100644 --- a/cli/tools/installer.rs +++ b/cli/tools/installer.rs @@ -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> { diff --git a/cli/tools/test.rs b/cli/tools/test.rs index ce99a6edc..7d6a6baa4 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 310d60d99..f16923bf8 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 a865daa9d..1e9819309 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<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. |