Repository navigation
Metrics: robustness lints #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dominik-appsilon
wants to merge
22
commits into
main
Choose a base branch
from
metrics-38-robustness-lints
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
0ba3deb
feat: first iteration of robustness lints metric
dominik-appsilon 7454e04
refactor: simplify selection of metrics and wrap result
dominik-appsilon 923c2b9
refactor: adjust metrics for consistency
dominik-appsilon 38b1f35
test: add basic tests for lint metric
dominik-appsilon 5d3f14c
feat: expand scope of robustness lints to correctness
dominik-appsilon a7a7e4f
docs: rebuild docs
dominik-appsilon 68b3869
refactor: change correctness lints to overall lints
dominik-appsilon 581bf89
feat: add option for configuring lints
dominik-appsilon 7667d4b
test: update tests for lints
dominik-appsilon 61ca4f7
docs: recompile documentation
dominik-appsilon b2dcd9e
feat: remove namespace and missing package linters
dominik-appsilon b2b8c99
test: remove unneeded test
dominik-appsilon 1336cfa
docs: update lints and opts documentation
dominik-appsilon a21bfaf
feat: improve the way of counting R lines
dominik-appsilon 4e9e42d
feat: make pkg dirs and pattern a parameter
dominik-appsilon 2182cd0
refactor: split function intwo two subfuncitons
dominik-appsilon 68b3128
refactor: use functions instead of loops
dominik-appsilon 6ec58b8
test: update data lints tests
dominik-appsilon 3b3c54c
test: apply minor style fixes
dominik-appsilon 1d9efc5
docs: add explanation of linter loading
dominik-appsilon 857ac0d
docs: recompile documentation
dominik-appsilon 18e36dd
style: fix linting issue (ironic)
dominik-appsilon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| #' @include impl_data.R | ||
|
|
||
| # Lint metric implementation | ||
|
|
||
| #' Find lintable files in a package | ||
| #' | ||
| #' @param path Package source directory. | ||
| #' @param pkg_dirs Subdirectories to search. | ||
| #' @param file_pattern Regex for file extensions. | ||
| #' @return Character vector of file paths. | ||
| #' @noRd | ||
| find_lintable_files <- function( | ||
| path, | ||
| pkg_dirs = c("R", "tests", "inst", "vignettes", "data-raw", "demo", "exec"), | ||
| file_pattern = "(?i)[.](r|rmd|qmd|rnw|rhtml|rrst|rtex|rtxt)$" | ||
| ) { | ||
| unlist(lapply(pkg_dirs, function(dir) { | ||
| dir_path <- file.path(path, dir) | ||
| if (dir.exists(dir_path)) { | ||
| list.files( | ||
| dir_path, | ||
| pattern = file_pattern, | ||
| recursive = TRUE, | ||
| full.names = TRUE | ||
| ) | ||
| } else { | ||
| character(0) | ||
| } | ||
| }), use.names = FALSE) | ||
| } | ||
|
|
||
| #' Count token-bearing, non-comment lines in a single file | ||
| #' | ||
| #' @param file Path to an R or Rmd file. | ||
| #' @return Integer count of code lines, or 0L on parse error. | ||
| #' @noRd | ||
| count_file_code_lines <- function(file) { | ||
| tryCatch( | ||
| { | ||
| se <- lintr::get_source_expressions(file) | ||
| full_expr <- se$expressions[[length(se$expressions)]] | ||
| pd <- full_expr$full_parsed_content | ||
|
|
||
| if (!is.null(pd) && nrow(pd) > 0L) { | ||
| length(unique(pd$line1[pd$terminal & pd$token != "COMMENT"])) | ||
| } else { | ||
| 0L | ||
| } | ||
| }, | ||
| error = function(e) 0L | ||
| ) | ||
| } | ||
|
|
||
| #' Count token-bearing, non-comment lines in files that lintr processes | ||
| #' | ||
| #' Uses lintr's own parsing frontend to ensure the denominator matches | ||
| #' the scope of lint_package(). A "relevant code line" is a physical | ||
| #' line containing at least one terminal token that is not a comment. | ||
| #' | ||
| #' @param path Path to package source directory. | ||
| #' @param pkg_dirs Character vector of subdirectories to search. | ||
| #' @param file_pattern Regex pattern for file extensions. | ||
| #' @return Integer count of code lines. | ||
| #' @noRd | ||
| count_lintable_code_lines <- function( | ||
| path, | ||
| pkg_dirs = c("R", "tests", "inst", "vignettes", "data-raw", "demo", "exec"), | ||
| file_pattern = "(?i)[.](r|rmd|qmd|rnw|rhtml|rrst|rtex|rtxt)$" | ||
| ) { | ||
| r_files <- find_lintable_files(path, pkg_dirs, file_pattern) | ||
|
|
||
| if (length(r_files) == 0L) { | ||
| return(0L) | ||
| } | ||
|
|
||
| sum(viapply(r_files, count_file_code_lines)) | ||
| } | ||
|
|
||
| # Helper: count unique (filename, line_number) pairs across all lints | ||
| count_linted_lines <- function(lints) { | ||
| if (length(lints) == 0L) return(0L) | ||
| pairs <- vcapply(lints, function(l) paste0(l$filename, ":", l$line_number)) | ||
| length(unique(pairs)) | ||
| } | ||
|
|
||
| # Auxiliary data: full lint results | ||
| impl_data( | ||
| "lints", | ||
| class = S7::new_S3_class("lints"), | ||
| metric = FALSE, | ||
| tags = c("execution"), | ||
| suggests = "lintr", | ||
| permissions = "execution", | ||
| title = "Lint Results", | ||
| description = paste( | ||
| "Full \\pkg{lintr} lint results for the package.", | ||
| "Contains detailed information about each lint including line numbers,", | ||
| "column positions, and lint messages. This is auxiliary data used to", | ||
| "derive the \\code{lint_count} and \\code{lint_free_fraction} metrics.", | ||
| "The set of linters applied is controlled by the \\code{lint_linters}", | ||
| "option." | ||
| ) | ||
| ) | ||
|
|
||
| impl_data( | ||
| "lints", | ||
| for_resource = source_code_resource, | ||
| function(pkg, resource, field, ..., quiet = opt("quiet")) { | ||
| linter_names <- opt("lint_linters") | ||
| # We are loading linter objects by name here to avoid hard dependencies | ||
| # on lintr in the rest of the code. | ||
| linters <- setNames( | ||
| lapply(linter_names, function(nm) utils::getFromNamespace(nm, "lintr")()), | ||
| linter_names | ||
| ) | ||
| wrapper <- if (quiet) { | ||
| function(...) capture.output(..., type = "message") | ||
| } else { | ||
| identity | ||
| } | ||
|
|
||
| wrapper({ | ||
| result <- lintr::lint_package( | ||
| path = resource@path, | ||
| linters = linters, | ||
| parse_settings = FALSE # Don't read .lintr config for consistency | ||
| ) | ||
| }) | ||
|
|
||
| result | ||
| } | ||
| ) | ||
|
|
||
| # Primary metric: count of lints | ||
| impl_data( | ||
| "lint_count", | ||
| metric = TRUE, | ||
| class = class_integer, | ||
| tags = c("best practice"), | ||
| title = "Lint Count", | ||
| description = paste( | ||
| "Count of lints identified by \\pkg{lintr}.", | ||
| "The set of linters applied is controlled by the \\code{lint_linters}", | ||
| "option, which defaults to linters from the \\emph{correctness},", | ||
| "\\emph{common_mistakes}, and \\emph{robustness} tags excluding", | ||
| "those that require configuration or are environment-dependent.", | ||
| "Lower counts indicate higher code quality." | ||
| ) | ||
| ) | ||
|
|
||
| impl_data( | ||
| "lint_count", | ||
| for_resource = source_code_resource, | ||
| function(pkg, resource, field, ...) { | ||
| length(pkg$lints) | ||
| } | ||
| ) | ||
|
|
||
| impl_data( | ||
| "lint_count", | ||
| for_resource = mock_resource, | ||
| function(pkg, resource, field, ...) rpois(1, 10) | ||
| ) | ||
|
|
||
| # Secondary metric: fraction of lintable lines that are lint-free | ||
| impl_data( | ||
| "lint_free_fraction", | ||
| metric = TRUE, | ||
| class = class_double, | ||
| tags = c("best practice"), | ||
| title = "Lint-Free Fraction", | ||
| description = paste( | ||
| "Fraction of R code lines that are free of lints.", | ||
| "Code lines are defined as lines containing executable R tokens", | ||
| "(excluding comments and blank lines) across the standard package", | ||
| "directories (\\code{R/}, \\code{tests/}, \\code{inst/},", | ||
| "\\code{vignettes/}, \\code{data-raw/}, \\code{demo/}, \\code{exec/}).", | ||
| "A value of 1 means no linted lines; lower values indicate more", | ||
| "pervasive lint issues. The set of linters applied is controlled by", | ||
| "the \\code{lint_linters} option." | ||
| ) | ||
| ) | ||
|
|
||
| impl_data( | ||
| "lint_free_fraction", | ||
| for_resource = source_code_resource, | ||
| function(pkg, resource, field, ...) { | ||
| loc <- count_lintable_code_lines(resource@path) | ||
| if (loc == 0L) return(1.0) | ||
| linted <- count_linted_lines(pkg$lints) | ||
| max(0.0, 1.0 - linted / loc) | ||
| } | ||
| ) | ||
|
|
||
| impl_data( | ||
| "lint_free_fraction", | ||
| for_resource = mock_resource, | ||
| function(pkg, resource, field, ...) { | ||
| runif(1, min = 0.7, max = 1.0) | ||
| } | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would protect in case lintr is not available with
if (!requireNamespace("lintr", quietly = TRUE)) return(NULL).