Skip to contents

rule() gives one entry of a registry, the named list of rules that folder_findings() runs. The name of the entry in the registry is the name of the rule.

Usage

rule(fn, pass_detail, fix, check_params = NULL, locked_reason = NULL)

Arguments

fn

The function of the rule, as described above.

pass_detail

The detail of the PASS line. It is a string, or a function of the entry of the rule in the rule set. The entry is a list with level and the parameters.

fix

The fix of each WARN and FAIL: a string, or a function of the detail of the finding that returns a string. The runner adds ". Fix: <fix>." to the detail.

check_params

NULL, or a function check_params(entry, name, config). The config reader calls it once for each parameter that the config file sets, other than level. entry is a named list with that one parameter, name is the name of the rule and config is the name of the config file. It returns entry with the checked value, or it stops with the config error text.

locked_reason

NULL, or one sentence that tells why the rule is locked. A rule set marks a rule locked with locked: true. The config file cannot change the level of a locked rule or allow a path for it. The config reader adds this sentence to that error.

Value

A list of class checkit_rule with the elements fn, pass_detail, fix, check_params and locked_reason.

Details

The runner calls fn(path, files, ...). path is the project folder, and files is the list that the scope argument of folder_findings() gives. The parameters of the entry of the rule set, such as limit, arrive as named arguments. When fn has an argument set, set is the name of the rule set of the project. fn returns a data.frame with the columns rule, status, path and detail, one row for each finding. Zero rows means PASS. A row MAY have the status "SKIP". At the levels fail and warn, the runner keeps it as SKIP, with no fix. The runner gives each other row that is not PASS the level of the rule.

Examples

no_capitals <- checkit::rule(
  fn = function(path, files) {
    bad <- files$entries[grepl("[A-Z]", basename(files$entries))]
    return(data.frame(
      rule = rep("no_capitals", length(bad)),
      status = rep("FAIL", length(bad)),
      path = bad,
      detail = paste0(bad, ": a capital letter in the name", recycle0 = TRUE)
    ))
  },
  pass_detail = "no capital letter in a name",
  fix = "rename it in lower case"
)
names(no_capitals)
#> [1] "fn"            "pass_detail"   "fix"           "check_params" 
#> [5] "locked_reason"