Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve on current regexes #371

Merged
merged 8 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/providers/clojure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl ClojureProvider {

fn parse_custom_version(custom_version: String) -> Result<String> {
// Regex for reading JDK versions (e.g. 8 or 11 or latest)
let jdk_regex = Regex::new(r"(^[0-9][0-9]?$)|(^latest$)")?;
let jdk_regex = Regex::new(r"^([0-9][0-9]?|latest)$")?;

// Capture matches
let matches = jdk_regex.captures(custom_version.as_str().trim());
Expand All @@ -91,11 +91,7 @@ impl ClojureProvider {
}

let matches = matches.unwrap();
let matched_value = if matches.get(0).is_some() {
matches.get(0)
} else {
matches.get(1)
};
let matched_value = matches.get(0);

let value = match matched_value {
Some(m) => m.as_str(),
Expand Down
7 changes: 5 additions & 2 deletions src/providers/deno.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ impl Provider for DenoProvider {
}

fn detect(&self, app: &App, _env: &Environment) -> Result<bool> {
let re = Regex::new(r##"(?m)^import .+ from "https://deno.land/[^"]+\.ts";?$"##).unwrap();
let re = Regex::new(
r##"import .+ from (?:"|'|`)https://deno.land/[^"`']+\.(?:ts|js|tsx|jsx)(?:"|'|`);?"##,
)
.unwrap();
Ok(app.includes_file("deno.json")
|| app.includes_file("deno.jsonc")
|| app.find_match(&re, "**/*.ts")?)
|| app.find_match(&re, "**/*.{tsx,ts,js,jsx}")?)
}

fn setup(&self, _app: &App, _env: &Environment) -> Result<Option<SetupPhase>> {
Expand Down
11 changes: 3 additions & 8 deletions src/providers/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl NodeProvider {

// Parse `18` or `18.x` into nodejs-18_x
// This also supports 18.x.x, or any number in place of the x.
let re = Regex::new(r"^(\d*)\.?([x|X]|[0-9]+)\.?([x|X]|[0-9]+)?$").unwrap();
let re = Regex::new(r"^(\d*)(?:\.?(?:\d*|[xX]?)?)(?:\.?(?:\d*|[xX]?)?)").unwrap();
if let Some(node_pkg) = parse_regex_into_pkg(&re, node_version.clone()) {
return Ok(Pkg::new(node_pkg.as_str()));
}
Expand Down Expand Up @@ -379,13 +379,8 @@ fn version_number_to_pkg(version: &u32) -> String {

fn parse_regex_into_pkg(re: &Regex, node_version: String) -> Option<String> {
let matches: Vec<_> = re.captures_iter(node_version.as_str()).collect();
if let Some(m) = matches.get(0) {
let capture = if node_version.contains('.') {
&m[1]
} else {
&m[0]
};
match capture.parse::<u32>() {
if let Some(captures) = matches.get(0) {
match captures[1].parse::<u32>() {
Ok(version) => return Some(version_number_to_pkg(&version)),
Err(_e) => {}
}
Expand Down