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

Add regex stuff #529

Merged
merged 12 commits into from
Jul 11, 2023
Prev Previous commit
Next Next commit
Update on "Add regex stuff"
[ghstack-poisoned]
  • Loading branch information
ketkarameya committed Jul 6, 2023
commit 80adac885670b1d0249b305be06cfbe498f5b40c
8 changes: 3 additions & 5 deletions src/models/capture_group_patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,16 @@ impl CGPattern {
}

pub(crate) fn extract_regex(&self) -> String {
let mut _val = self.pattern();
_val.replace_range(..4, "rgx ");
let mut _val = &self.pattern()[4..];
_val.to_string()
}
}

impl Validator for CGPattern {
fn validate(&self) -> Result<(), String> {
if self.pattern().starts_with("rgx ") {
let mut _val = self.pattern();
_val.replace_range(..4, "rgx ");
return Regex::new(_val.as_str())
let mut _val = &self.pattern()[4..];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let mut _val = &self.pattern()[4..];
let mut _val = &self.extract_regex();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have this logic here and in extract_regex() both? This probably should not be duplicated in case we ever change the identifier, and also should be something like len(REGEX_QUERY_PREFIX) and that prefix used elsewhere to match "rgx "

return Regex::new(_val)
.map(|_| Ok(()))
.unwrap_or(Err(format!("Cannot parse the regex - {_val}")));
}
Expand Down
1 change: 1 addition & 0 deletions src/tests/test_piranha_java.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ create_rewrite_tests! {
test_new_line_character_used_in_string_literal: "new_line_character_used_in_string_literal", 1;
test_java_delete_method_invocation_argument: "delete_method_invocation_argument", 1;
test_java_delete_method_invocation_argument_no_op: "delete_method_invocation_argument_no_op", 0;
test_regex_based_matcher: "regex_based_matcher", 1;
}

create_match_tests! {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[[rules]]
name = "replace_call"
query = """rgx (?P<n>foo\\(\\))"""
replace_node = "n"
replace = "Foo"
10 changes: 10 additions & 0 deletions test-resources/java/regex_based_matcher/expected/Sample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.uber.piranha;

class A {

void foobar() {
int total = Foo;
System.out.println(total);
}

}
10 changes: 10 additions & 0 deletions test-resources/java/regex_based_matcher/input/Sample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.uber.piranha;

class A {

void foobar() {
int total = foo();
System.out.println(total);
}

}