Skip to content

Commit

Permalink
add paren_brace_linter (#380)
Browse files Browse the repository at this point in the history
* cp R/equals_na_lintr.R R/paren_brace_linter.R

* cp tests/testthat/test-equals_na_linter.R tests/testthat/test-paren_brace_linter.R

* add paren_brace_linter; closes #242

* document

* add paren_brace_linter to NEWS.md and README.md

* tweak paren_brace_linter docs

* add paren_brace_linter to default_linters

* improve NEWS entry for paren_brace_linter
  • Loading branch information
bfgray3 authored and jimhester committed Sep 26, 2019
1 parent 8288366 commit b6c79de
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 1 deletion.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Collate:
'no_tab_linter.R'
'object_usage_linter.R'
'open_curly_linter.R'
'paren_brace_linter.R'
'path_linters.R'
'pipe_continuation_linter.R'
'semicolon_terminator_linter.R'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export(object_length_linter)
export(object_name_linter)
export(object_usage_linter)
export(open_curly_linter)
export(paren_brace_linter)
export(pipe_continuation_linter)
export(semicolon_terminator_linter)
export(seq_linter)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# lintr 1.0.3.9000 #
* Added the new `paren_brace_linter()` to the default linters; it checks that there is a space between right parenthesis and an opening curly brace (@bfgray3, #242).
* Now lintr only tries to generate comments if running in wercker or travis CI (#166)
* Top level calls that contain function definitions are not properly ignored by
the object usage linter (#26).
Expand Down
39 changes: 39 additions & 0 deletions R/paren_brace_linter.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#' @describeIn linters check that there is a space between right
#' parenthesis and an opening curly brace.
#' @export
paren_brace_linter <- function(source_file) {
all_matches <- re_matches(
source_file[["lines"]],
rex("){"),
locations = TRUE,
global = TRUE
)
line_numbers <- as.integer(names(source_file[["lines"]]))

Map(
function(line_matches, line_number) {
lapply(
split(line_matches, seq_len(nrow(line_matches))),
function(.match) {
start <- .match[["start"]]
if (is.na(start)) {
return()
}
end <- .match[["end"]]
Lint(
filename = source_file[["filename"]],
line_number = line_number,
column_number = start,
type = "style",
message = "There should be a space between right parenthesis and an opening curly brace.",
line = source_file[["lines"]][[as.character(line_number)]],
ranges = list(c(start, end)),
linter = "paren_brace_linter"
)
}
)
},
all_matches,
line_numbers
)
}
1 change: 1 addition & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ default_linters <- with_defaults(default = list(),
object_name_linter("snake_case"),
object_usage_linter,
open_curly_linter(),
paren_brace_linter,
pipe_continuation_linter,
single_quotes_linter,
spaces_inside_linter,
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ If you need a bit automatic help for re-styling your code, have a look at [the `
or UPPERCASE.
* `open_curly_linter`: check that opening curly braces are never on their own
line and are always followed by a newline.
* `paren_brace_linter`: check that there is a space between right parenthesis and an opening curly brace.
* `semicolon_terminator_linter`: check that no semicolons terminate statements.
* `seq_linter`: check for `1:length(...)`, `1:nrow(...)`, `1:ncol(...)`,
`1:NROW(...)`, and `1:NCOL(...)` expressions. These often cause bugs when the
Expand Down
8 changes: 7 additions & 1 deletion man/linters.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions tests/testthat/test-paren_brace_linter.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
context("paren_brace_linter")

test_that("returns the correct linting", {
msg <- rex("There should be a space between right parenthesis and an opening curly brace.")

expect_lint("blah", NULL, paren_brace_linter)
expect_lint("blah <- function() {}", NULL, paren_brace_linter)
expect_lint("blah <- function() {\n}", NULL, paren_brace_linter)

expect_lint(
"blah <- function(){}",
msg,
paren_brace_linter
)

expect_lint(
"\nblah <- function(){\n\n\n}",
msg,
paren_brace_linter
)
})

0 comments on commit b6c79de

Please sign in to comment.