Skip to content

Commit

Permalink
Support searching for raw text patterns
Browse files Browse the repository at this point in the history
Summary: We can use this to match functions of a specific name, or to match specific operators (such as by matching binary expressions that have their operator's raw text be `+`).

Reviewed By: pittsw

Differential Revision: D7760889

fbshipit-source-id: d7248c1f0a0f16def365a87ab9ff6bca58bd04fb
  • Loading branch information
Waleed Khan authored and hhvm-bot committed May 19, 2018
1 parent 1416e7f commit 403c206
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 0 deletions.
22 changes: 22 additions & 0 deletions hphp/hack/src/server/cstSearchService.ml
Expand Up @@ -85,6 +85,14 @@ type pattern =
pattern: pattern;
}

(**
* Matches a given node if its raw text is exactly the specified string. The
* "raw" text doesn't include trivia.
*)
| RawTextPattern of {
raw_text: string;
}

type matched_node = {
match_name: match_name;
node: Syntax.t;
Expand Down Expand Up @@ -168,6 +176,11 @@ let rec search_node
| DescendantPattern { pattern } ->
search_descendants ~env ~pattern ~node

| RawTextPattern { raw_text } ->
if Syntax.text node = raw_text
then (env, empty_result)
else (env, None)

(* TODO: this will likely have to become more intelligent *)
and search_descendants
~(env: env)
Expand Down Expand Up @@ -213,6 +226,8 @@ let compile_pattern (json: Hh_json.json): (pattern, string) Core_result.t =
compile_match_pattern ~json ~keytrace
| "descendant_pattern" ->
compile_descendant_pattern ~json ~keytrace
| "raw_text_pattern" ->
compile_raw_text_pattern ~json ~keytrace
| pattern_type ->
error_at_keytrace ~keytrace:pattern_type_keytrace
(Printf.sprintf "Unknown pattern type '%s'" pattern_type)
Expand Down Expand Up @@ -293,6 +308,13 @@ let compile_pattern (json: Hh_json.json): (pattern, string) Core_result.t =
pattern;
}

and compile_raw_text_pattern ~json ~keytrace =
get_string "raw_text" (json, keytrace)
>>| fun (raw_text, _raw_text_keytrace) ->
RawTextPattern {
raw_text;
}

in
compile_pattern ~json ~keytrace:[]

Expand Down
1 change: 1 addition & 0 deletions hphp/hack/test/cst_search/raw_text_match.flags
@@ -0,0 +1 @@
--cst-search basic_class.php
19 changes: 19 additions & 0 deletions hphp/hack/test/cst_search/raw_text_match.json
@@ -0,0 +1,19 @@
{
"pattern_type": "descendant_pattern",
"pattern": {
"pattern_type": "node_pattern",
"kind": "methodish_declaration",
"children": {
"methodish_function_decl_header": {
"pattern_type": "node_pattern",
"kind": "function_declaration_header",
"children": {
"function_name": {
"pattern_type": "raw_text_pattern",
"raw_text": "bar"
}
}
}
}
}
}
1 change: 1 addition & 0 deletions hphp/hack/test/cst_search/raw_text_match.json.exp
@@ -0,0 +1 @@
{"matched_nodes":[]}
1 change: 1 addition & 0 deletions hphp/hack/test/cst_search/raw_text_no_match.flags
@@ -0,0 +1 @@
--cst-search basic_class.php
19 changes: 19 additions & 0 deletions hphp/hack/test/cst_search/raw_text_no_match.json
@@ -0,0 +1,19 @@
{
"pattern_type": "descendant_pattern",
"pattern": {
"pattern_type": "node_pattern",
"kind": "methodish_declaration",
"children": {
"methodish_function_decl_header": {
"pattern_type": "node_pattern",
"kind": "function_declaration_header",
"children": {
"function_name": {
"pattern_type": "raw_text_pattern",
"raw_text": "foo"
}
}
}
}
}
}
1 change: 1 addition & 0 deletions hphp/hack/test/cst_search/raw_text_no_match.json.exp
@@ -0,0 +1 @@
null

0 comments on commit 403c206

Please sign in to comment.