Skip to content

Commit

Permalink
Merge pull request #148 from gaborcsardi/trailing-semicolon
Browse files Browse the repository at this point in the history
trailing_semicolon_linter #147
  • Loading branch information
jimhester committed May 12, 2016
2 parents 600cb82 + e540755 commit 2b091a8
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export(snake_case_linter)
export(spaces_inside_linter)
export(spaces_left_parentheses_linter)
export(trailing_blank_lines_linter)
export(trailing_semicolons_linter)
export(trailing_whitespace_linter)
export(with_defaults)
import(rex)
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.0.9000 #
* trailing_semicolon_linter (#147, @gaborcsardi)

* Commas linter handles missing arguments calls properly (#145)

Expand Down
39 changes: 39 additions & 0 deletions R/trailing_semicolons_linter.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#' @describeIn linters check there are no trailing semicolons.
#' @export
trailing_semicolons_linter <- function(source_file) {

allsc <- which(source_file$parsed_content$token == "';'")

if (!length(allsc)) return(list())

## Check that it is the last token in the line
## Note that we need to restrict the search to terminal
## nodes, because parse is buggy and sometimes reports false,
## too large end positions for non-terminal nodes.
badsc <- Filter(
x = allsc,
f = function(line) {
with(
source_file$parsed_content,
all(! terminal | line1 != line1[line] | col2 <= col2[line])
)
}
)

lapply(
badsc,
function(line) {
parsed <- source_file$parsed_content[line, ]
Lint(
filename = source_file$filename,
line_number = parsed$line1,
column_number = parsed$col1,
type = "style",
message = "Avoid trailing semicolons, they are not needed.",
line = source_file$lines[as.character(parsed$line1)],
ranges = list(c(parsed$col1, parsed$col2)),
linter = "trailing_semicolons_linter"
)
}
)
}
7 changes: 6 additions & 1 deletion man/linters.Rd

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

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

test_that("finds trailing semicolons", {
expect_lint("function() { 42 }\n",
NULL,
trailing_semicolons_linter)

expect_lint("function() { \"But here\";\n\" and here\"; }",
rex("trailing semicolons"),
trailing_semicolons_linter)
})

test_that("finds trailing semicolons even with trailing whitespace", {
expect_lint("function() { \"Here, too!\"; \n }\n",
rex("trailing semicolons"),
trailing_semicolons_linter)
})

test_that("ignores non-trailing semicolons", {
expect_lint("function() { 1 ; 2 ; \"is good\" }\n",
NULL,
trailing_semicolons_linter)
})

0 comments on commit 2b091a8

Please sign in to comment.