-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #148 from gaborcsardi/trailing-semicolon
trailing_semicolon_linter #147
- Loading branch information
Showing
5 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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,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" | ||
) | ||
} | ||
) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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,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) | ||
}) |