From c8d398ba0bf44c5977fe80b96c29e4c27dace2bf Mon Sep 17 00:00:00 2001 From: Chris Black Date: Mon, 13 Aug 2018 16:15:07 +0200 Subject: [PATCH] allow multiple naming styles if user really wants them (#341) --- NEWS.md | 1 + R/object_name_linters.R | 4 ++-- tests/testthat/test-object_name_linter.R | 11 +++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index cb6fcd8e6..ce1b4bd5d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,5 @@ # lintr 1.0.2.9000 # +* Fixed error when object_name_linter is passed multiple styles (#341, @infotroph) * Config files are now also searched for in the users' home directory (#266, @randy3k) * Fixed crash caused by ambiguous cache file paths (#212, @fangly). * RStudio addins to lint current source and project (fixes #264, @JhossePaul) diff --git a/R/object_name_linters.R b/R/object_name_linters.R index e2ea93093..6abae0ff5 100644 --- a/R/object_name_linters.R +++ b/R/object_name_linters.R @@ -132,11 +132,11 @@ object_name_linter <- function(style = "snake_case") { make_object_linter( function(source_file, token) { name <- unquote(token[["text"]]) - if (!matches_styles(name, style)) { + if (!any(matches_styles(name, style))) { object_lint( source_file, token, - sprintf("Variable or function name should be %s.", style), + sprintf("Variable or function name should be %s.", paste(style, collapse = " or ")), "object_name_linter" ) } diff --git a/tests/testthat/test-object_name_linter.R b/tests/testthat/test-object_name_linter.R index 7e9721095..27185ebb4 100644 --- a/tests/testthat/test-object_name_linter.R +++ b/tests/testthat/test-object_name_linter.R @@ -79,3 +79,14 @@ test_that("linter returns correct linting", { expect_lint("pack:::camelCase", NULL, linter) expect_lint("a(camelCase = 1)", NULL, linter) }) + +test_that("linter accepts vectors of styles", { + msg <- "Variable or function name should be lowerCamelCase or dotted.case." + linter <- object_name_linter(style=c("lowerCamelCase", "dotted.case")) + + expect_lint( + c("var.one <- 1", "varTwo <- 2", "var_three <- 3"), + list(message=msg, line_number=3L, column_number=1L), + linter + ) +})