-
Notifications
You must be signed in to change notification settings - Fork 195
feat(new linter): added a new linter #830
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
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
616405a
feat(new linter): added a new linter
kpagacz cfcf93c
Added a new line at the end of paren_body_linter.R
kpagacz d129dfd
Updated after review no 1
kpagacz eaf2f48
Merge branch 'master' into paren_body_linter
kpagacz 7eb25d2
Added `\`
kpagacz 40128c9
Merge remote-tracking branch 'origin/paren_body_linter' into paren_bo…
kpagacz 5bb7b23
Updated NEWS and README
kpagacz 268286a
update roxygen and document()
AshesITR c759017
update roxygen and document()
AshesITR 976e4b5
Added a skip condition to the test with syntax available since R 4.1.0
kpagacz c981ced
Refactored comparing versions
kpagacz 57ef0be
update NEWS to indicate paren_body_linter() is a default linter.
AshesITR 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
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,36 @@ | ||
| #' @describeIn linters check that there is a space between right parenthesis and a body expression. | ||
| #' | ||
| #' @export | ||
| paren_body_linter <- function() { | ||
| Linter(function(source_file) { | ||
| if (is.null(source_file$xml_parsed_content)) return(NULL) | ||
|
|
||
| xpath <- paste( | ||
| "//expr[", | ||
| "@line1 = preceding-sibling::FUNCTION/@line1", | ||
| "|", | ||
| "preceding-sibling::IF/@line1", | ||
| "|", | ||
| "preceding-sibling::WHILE/@line1", | ||
| "|", | ||
| "preceding-sibling::OP-LAMBDA/@line1", | ||
| "and", | ||
| "@col1 = preceding-sibling::OP-RIGHT-PAREN/@col1 + 1", | ||
| "]", | ||
| "|", | ||
| "//expr[", | ||
| "@line1 = preceding-sibling::forcond/@line1", | ||
| "and", | ||
| "@col1 = preceding-sibling::forcond/OP-RIGHT-PAREN/@col1 + 1", | ||
| "]" | ||
| ) | ||
| matched_expressions <- xml2::xml_find_all(source_file$xml_parsed_content, xpath) | ||
|
|
||
| lapply( | ||
| matched_expressions, | ||
| xml_nodes_to_lint, | ||
| source_file = source_file, | ||
| message = "There should be a space between right parenthesis and a body expression." | ||
| ) | ||
| }) | ||
| } | ||
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
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.
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,52 @@ | ||
| testthat::test_that("paren_body_linter returns correct lints", { | ||
| linter <- paren_body_linter() | ||
| msg <- "There should be a space between right parenthesis and a body expression." | ||
|
|
||
| # No space after the closing parenthesis prompts a lint | ||
| expect_lint("function()test", msg, linter) | ||
| expect_lint("print('hello')\nx <- function(x)NULL\nprint('hello')", msg, linter) | ||
| expect_lint("if (TRUE)test", msg, linter) | ||
| expect_lint("while (TRUE)test", msg, linter) | ||
| expect_lint("for (i in seq_along(1))test", msg, linter) | ||
|
|
||
| # A space after the closing parenthesis does not prompt a lint | ||
| expect_lint("function() test", NULL, linter) | ||
|
|
||
| # Symbols after the closing parenthesis of a function call do not prompt a lint | ||
| expect_lint("head(mtcars)$cyl", NULL, linter) | ||
|
|
||
| # paren_body_linter returns the correct line number | ||
| expect_lint( | ||
| "print('hello')\nx <- function(x)NULL\nprint('hello')", | ||
| list(line_number = 2L), | ||
| linter | ||
| ) | ||
|
|
||
| expect_lint( | ||
| "function()test", | ||
| list( | ||
| line_number = 1L, | ||
| column_number = 11L, | ||
| type = "style", | ||
| line = c("1" = "function()test"), | ||
| ranges = list(c(11L, 14L)) | ||
| ), | ||
| linter | ||
| ) | ||
|
|
||
| # paren_body_linter does not lint when the function body is defined on a new line | ||
| expect_lint("function()\n test", NULL, linter) | ||
|
|
||
| # paren_body_linter does not lint comments | ||
| expect_lint("#function()test", NULL, linter) | ||
|
|
||
| # multiple lints on the same line | ||
| expect_lint("function()if(TRUE)while(TRUE)test", list(msg, msg, msg), linter) | ||
|
|
||
| # No space after the closing parenthesis of an anonymous function prompts a lint | ||
| testthat::skip_if( | ||
| getRversion() < "4.1", | ||
| message = "Not run on R version < 4.1.0" | ||
| ) | ||
| expect_lint("\\()test", msg, linter) | ||
| }) |
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.
Uh oh!
There was an error while loading. Please reload this page.