-
Notifications
You must be signed in to change notification settings - Fork 195
361 cyclocomp linter #396
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
Merged
Merged
361 cyclocomp linter #396
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
23f84cf
[WIP] linter works
fabian-s 8927a06
adds linting for cyclomatic complexity.
fabian-s 8fcf7fb
updates NEWS for cyclocomp_linter
fabian-s 3c4af7a
less verbose message for cyclocomp_linter
fabian-s f636ba3
update tests to new message
fabian-s 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
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,34 @@ | ||
| #' @describeIn linters Check for overly complicated expressions. See | ||
| #' \code{\link[cyclocomp]{cyclocomp}}. | ||
| #' @param complexity_limit expressions with a cyclomatic complexity higher than | ||
| #' this are linted, defaults to 25. See \code{\link[cyclocomp]{cyclocomp}}. | ||
| #' @importFrom cyclocomp cyclocomp | ||
| #' @export | ||
| cyclocomp_linter <- function(complexity_limit = 25) { | ||
| function(source_file) { | ||
| if (!is.null(source_file[["file_lines"]])) { | ||
| # abort if source_file is entire file, not a top level expression. | ||
| return(NULL) | ||
| } | ||
| complexity <- try_silently( | ||
| cyclocomp::cyclocomp(parse(text = source_file$content)) | ||
| ) | ||
| if (inherits(complexity, "try-error")) return(NULL) | ||
| if (complexity <= complexity_limit) return(NULL) | ||
| Lint( | ||
| filename = source_file[["filename"]], | ||
| line_number = source_file[["line"]][1], | ||
| column_number = source_file[["column"]][1], | ||
| type = "style", | ||
| message = paste0( | ||
| "Write short and simple functions and avoid complex conditionals,", | ||
| "e.g. by encapsulating distinct steps into subfunctions.", | ||
| "The expression starting on this line has a cyclomatic complexity of ", | ||
| complexity, ", should be at most ", complexity_limit, "." | ||
| ), | ||
| ranges = list(c(NA, NA)), | ||
| line = source_file$lines[1], | ||
| linter = "cyclocomp_linter" | ||
| ) | ||
| } | ||
| } | ||
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,24 @@ | ||
| complexity <- function(x) { | ||
| if (x > 0) { | ||
| if (x > 10) { | ||
| if (x > 20) { | ||
| x <- x / 2 | ||
| } else { | ||
| return(x) | ||
| } | ||
| } else { | ||
| return(x) | ||
| } | ||
| } else { | ||
| if (x < -10) { | ||
| if (x < -20) { | ||
| x <- x * 2 | ||
| } else { | ||
| return(x) | ||
| } | ||
| } else { | ||
| return(x) | ||
| } | ||
| } | ||
| x | ||
| } |
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.
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,26 @@ | ||
| context("cyclocomp_linter") | ||
|
|
||
| test_that("returns the correct linting", { | ||
| cc_linter_1 <- cyclocomp_linter(1) | ||
| cc_linter_2 <- cyclocomp_linter(2) | ||
| msg <- rex("Write short and simple functions") | ||
|
|
||
| expect_lint("if(TRUE) 1 else 2", NULL, cc_linter_2) | ||
| expect_lint("if(TRUE) 1 else 2", msg, cc_linter_1) | ||
|
|
||
| expect_lint( | ||
| "function(x) {not parsing}", | ||
| "unexpected symbol", cc_linter_2 | ||
| ) | ||
|
|
||
| complexity <- readLines( | ||
| system.file("example/complexity.R", package = "lintr") | ||
| ) | ||
| expect_lint(complexity, msg, cc_linter_2) | ||
| expect_lint( | ||
| complexity, | ||
| "cyclomatic complexity of 10, should be at most 2", | ||
| cc_linter_2 | ||
| ) | ||
| expect_lint(complexity, NULL, cyclocomp_linter(10)) | ||
| }) |
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 think this is too long, maybe
"functions should have cyclomatic complexity of less than", complexity_limit, "."