Dispatches on the segment's language, which the extractor set. This is a separate axis from the source's type: a help file and an R script both yield R segments, and one literate file can yield several languages.
Arguments
- segment
A
pkgaudit_segmentfromnew_segment().- rules
Named list of rules as returned by
load_rules().
Value
A list of patterns, matches, coverage and errors data
frames, each with the columns audit_package() documents, less the phase
columns.
Method contract
A method must build its return value with new_findings(), which holds every
analyzer to the same frame shape. UseMethod() ends the generic, so this
cannot be enforced after dispatch.
A method must not evaluate the code it is given. It reads untrusted text and reports what it finds; nothing in a scan is ever run.
See also
extract_segments(), the other axis of dispatch, new_findings(),
and vignette("internals").
Examples
# Adding a language outside pkgaudit: a method for segments the extractor
# labeled "python", reporting each line that calls eval().
analyze_segment.python <- function(segment, rules) {
at <- grep("\\beval\\(", segment$lines)
if (length(at) == 0L) return(new_findings())
new_findings(matches = data.frame(
rule = "py_eval", file_context = segment$file_context,
line_number = at, column_number = NA_integer_,
preview = trimws(segment$lines[at]),
message = "eval() in Python code", attck = NA_character_))
}
registerS3method("analyze_segment", "python", analyze_segment.python)