Skip to contents

Checks method and attribute calls within an R6 class. Covers public, private, and active objects. All internal calls should exist. All private methods and attributes should be used.

Usage

r6_usage_linter()

Value

A custom linter function for use with r-lib/lintr.

Details

For use in rhino, see the Explanation: Rhino style guide to learn about the details.

Examples

# will produce lints
code = "
box::use(
  R6[R6Class],
)

badClass <- R6Class('badClass',
  public = list(
    initialize = function() {
      private$not_exists()
    }
  ),
  private = list(
    unused_attribute = 'private data',
    unused_method = function() {
      self$attribute_not_exists
      self$function_not_exists()
    }
  )
)
"

lintr::lint(
  text = code,
  linters = r6_usage_linter()
)
#> ::warning file=<text>,line=9,col=7::file=<text>,line=9,col=7,[r6_usage_linter] Internal object call not found.
#> ::warning file=<text>,line=13,col=5::file=<text>,line=13,col=5,[r6_usage_linter] Private object not used.
#> ::warning file=<text>,line=14,col=5::file=<text>,line=14,col=5,[r6_usage_linter] Private object not used.
#> ::warning file=<text>,line=15,col=7::file=<text>,line=15,col=7,[r6_usage_linter] Internal object call not found.
#> ::warning file=<text>,line=16,col=7::file=<text>,line=16,col=7,[r6_usage_linter] Internal object call not found.

# okay
code = "
box::use(
  R6[R6Class],
)

goodClass <- R6Class('goodClass',
  public = list(
    public_attr = NULL,
    initialize = function() {
      private$private_func()
    },
    some_function = function () {
      private$private_attr
    }
  ),
  private = list(
    private_attr = 'private data',
    private_func = function() {
      self$public_attr
    }
  )
)
"

lintr::lint(
  text = code,
  linters = r6_usage_linter()
)