Determine the code context of each pattern occurrence
Source:R/determine_code_contexts.R
determine_code_contexts.RdAssigns every pattern found by find_patterns() the code context it would
execute in.
Arguments
- tree
The
xml_documentparse tree for one script (fromparse_code()).- patterns
Data frame from
find_patterns(), carrying its matched nodes in the"nodes"attribute aligned to the rows.- rules
Loaded rules; only
rules$code_contexts(columnsname,xpath) is used.
Value
patterns with an added code_context column – a named context,
"top_level" or "in_function", never NA. The "nodes" attribute is
dropped from the result.
Details
A pattern occurs in a named context iff its node is a descendant of that
context's node; where it sits inside more than one, the innermost wins.
Containment is tested by exact XML path identity, never by path-prefix
comparison, since sibling paths such as expr[1] and expr[12] would
false-match.
"top_level" and "in_function" are not rule-matched. They are computed
here for a pattern in no named context: "in_function" when it has a
function-definition ancestor, "top_level" when it does not.