Skip to content

Commit 4274781

Browse files
committed
Further improve PHP define regex
1 parent 46c1d26 commit 4274781

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

src/query_regex.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub fn get_regex_for_query(query: &str, file_type: &FileType) -> Regex {
77
r"(?:\b(?:function|var|let|const|class|interface|type)\s+{query}\b|\b{query}\([^)]*\)\s*(?::[^\{{]+)?\{{|\b{query}:|@typedef\s*(?:\{{[^\}}]+\}})?\s*{query}\b)"
88
),
99
FileType::PHP => &format!(
10-
r#"\b(?:function|class|trait|interface|enum|const) {query}\b|define\s*\(\s*['"]{query}\b"#
10+
r#"\b(?:function|class|trait|interface|enum|const) {query}\b|\bdefine\s*\(\s*['"]{query}['"]"#
1111
),
1212
FileType::RS => &format!(r"\b(?:fn|trait|enum|struct|mod) {query}\b"),
1313
};

tests/fixtures/by-language/php-fixture.php

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public function doSomethingAbsolute(): \Home\Foo {
3131
define( 'GLOBALDEFINESINGLE', 42 );
3232
define( "GLOBALDEFINEDOUBLE", 43 );
3333
define( "GLOBALDEFINEDOUBLE_TWO", 41 ); // This is here to make sure that the other searches do not find it
34+
define('GLOBALDEFINESINGLE_NOSPACE',40);
35+
define ('GLOBALDEFINESINGLE_LEADSPACE',49);
3436

3537
class ClassWithConstant {
3638
public const MYCONSTANT = 5;

tests/integration_test.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,17 @@ fn search_returns_matching_js_function_line_with_filetype_alias(#[case] file_typ
290290
#[case::php_global_constant(String::from("GLOBALCONSTANT"), String::from("php"), 30)]
291291
#[case::php_global_define_single(String::from("GLOBALDEFINESINGLE"), String::from("php"), 31)]
292292
#[case::php_global_define_double(String::from("GLOBALDEFINEDOUBLE"), String::from("php"), 32)]
293-
#[case::php_global_class_constant(String::from("MYCONSTANT"), String::from("php"), 36)]
293+
#[case::php_global_define_nospace(
294+
String::from("GLOBALDEFINESINGLE_NOSPACE"),
295+
String::from("php"),
296+
34
297+
)]
298+
#[case::php_global_define_leadspace(
299+
String::from("GLOBALDEFINESINGLE_LEADSPACE"),
300+
String::from("php"),
301+
35
302+
)]
303+
#[case::php_global_class_constant(String::from("MYCONSTANT"), String::from("php"), 38)]
294304
#[case(String::from("query_db"), String::from("rs"), 1)]
295305
#[case(String::from("public_func"), String::from("rs"), 6)]
296306
#[case(String::from("Wrapper"), String::from("rs"), 4)]
@@ -307,9 +317,13 @@ fn search_returns_expected_line_number_for_file_type(
307317
common::get_default_fixture_for_file_type_string(file_type_string.as_str()).unwrap();
308318
let args = common::make_args(query, Some(file_path), Some(file_type_string));
309319
let actual = common::do_search(args);
310-
assert_eq!(1, actual.len());
320+
assert_eq!(1, actual.len(), "Did not find exactly one match");
311321
let first_actual = actual.get(0).expect("Search failed for test");
312-
assert_eq!(line_number, first_actual.line_number.unwrap());
322+
assert_eq!(
323+
line_number,
324+
first_actual.line_number.unwrap(),
325+
"Match found but it has the wrong line number"
326+
);
313327
}
314328

315329
#[rstest]

0 commit comments

Comments
 (0)