Skip to content
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

New quotes_linter() to replace single_quotes_linter() #1931

Merged
merged 16 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ Collate:
'path_utils.R'
'pipe_call_linter.R'
'pipe_continuation_linter.R'
'quotes_linter.R'
'redundant_equals_linter.R'
'redundant_ifelse_linter.R'
'regex_subset_linter.R'
Expand All @@ -147,7 +148,6 @@ Collate:
'seq_linter.R'
'settings.R'
'settings_utils.R'
'single_quotes_linter.R'
'sort_linter.R'
'spaces_inside_linter.R'
'spaces_left_parentheses_linter.R'
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export(paren_brace_linter)
export(paste_linter)
export(pipe_call_linter)
export(pipe_continuation_linter)
export(quotes_linter)
export(redundant_equals_linter)
export(redundant_ifelse_linter)
export(regex_subset_linter)
Expand Down
20 changes: 15 additions & 5 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# lintr (development version)

## Deprecations

* `single_quotes_linter()` is deprecated in favor of the more generalizable `quotes_linter()` (#1729, @MichaelChirico).
* `unneeded_concatentation_linter()` is deprecated in favor of `unnecessary_concatenation_linter()` for naming consistency (#1707, @IndrajeetPatil).

## Bug fixes

* `fixed_regex_linter()` is more robust to errors stemming from unrecognized escapes (#1545, #1845, @IndrajeetPatil).
Expand Down Expand Up @@ -44,14 +49,14 @@
the style guide on handling this case awaits clarification: https://github.com/tidyverse/style/issues/191.
(#1346, @MichaelChirico)

* The new `indentation_linter()` is part of the default linters. See "New linters" for more details.

* For naming consistency, `unneeded_concatenation_linter()` has been deprecated in favor of
`unnecessary_concatenation_linter()` (#1797, @IndrajeetPatil).

* `undesirable_function_linter()` and `undesirable_operator_linter()` now produce an error
if empty vector of undesirable functions or operators is provided (#1867, @IndrajeetPatil).

* New linters which are also included as defaults (see "New linters" for more details):
+ `indentation_linter()`
+ `quotes_linter()`
+ `unnecessary_concatenation_linter()`

## New and improved features

* New `get_r_string()` helper to get the R-equivalent value of a string, especially useful for R-4-style raw strings.
Expand Down Expand Up @@ -131,6 +136,11 @@

* `implicit_assignment_linter()` for checking implicit assignments in function calls (@IndrajeetPatil and @AshesITR, #1777).

* `quotes_linter()` is a generalized version of (now deprecated) `single_quotes_linter()`. It accepts an argument `delimite_char` to specify whether `"` or `'` should be the accepted method for delimiting character literals. The default, `"`, reflects the Tidyverse guide recommendation and matches the behavior of `single_quotes_linter()`.
MichaelChirico marked this conversation as resolved.
Show resolved Hide resolved
* `quotes_linter()` is a generalized version of (now deprecated) `single_quotes_linter()`. It accepts an argument `quote_char` to specify whether `"` or `'` should be the accepted method for quoting character literals. The default, `"`, reflects the Tidyverse style guide recommendation and matches the behavior of `single_quotes_linter()`.
MichaelChirico marked this conversation as resolved.
Show resolved Hide resolved

* `unnecessary_concatenation_linter()` is simply `unneeded_concatenation_linter()`, renamed.

## Notes

* {lintr} now depends on R version 3.5.0, in line with the tidyverse policy for R version compatibility.
Expand Down
13 changes: 13 additions & 0 deletions R/lintr-deprecated.R
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,16 @@ find_column_fun <- function(content, newline_locs) {
line_number - newline_locs[matched_line_number]
}
}

#' Single quotes linter
#' @rdname lintr-deprecated
#' @export
single_quotes_linter <- function() {
lintr_deprecated(
old = "single_quotes_linter",
new = "quotes_linter",
version = "3.1.0",
type = "Linter"
)
quotes_linter()
}
90 changes: 90 additions & 0 deletions R/quotes_linter.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#' Character string quote linter
#'
#' Check that the desired quote delimiter is used for string constants.
#'
#' @param delimiter Which quote delimiter to accept. Defaults to the tidyverse
#' default of `"` (double-quoted strings).
#'
#' @examples
#' # will produce lints
#' lint(
#' text = "c('a', 'b')",
#' linters = quotes_linter()
#' )
#'
#' # okay
#' lint(
#' text = 'c("a", "b")',
#' linters = quotes_linter()
#' )
#'
#' code_lines <- "paste0(x, '\"this is fine\"')"
#' writeLines(code_lines)
#' lint(
#' text = code_lines,
#' linters = quotes_linter()
#' )
#'
#' # okay
#' lint(
#' text = "c('a', 'b')",
#' linters = quotes_linter(delimiter = "'")
#' )
#'
#' @evalRd rd_tags("quotes_linter")
#' @seealso
#' - [linters] for a complete list of linters available in lintr.
#' - <https://style.tidyverse.org/syntax.html#character-vectors>
#' @export
quotes_linter <- function(delimiter = c('"', "'")) {
delimiter <- match.arg(delimiter)
if (delimiter == '"') {
quote_regex <- rex(
start,
zero_or_one(character_class("rR")),
single_quote,
any_non_double_quotes,
single_quote,
end
)
lint_message <- "Only use double-quotes." # nolint: object_usage_linter. An apparent codetools bug.
} else {
quote_regex <- rex(
start,
zero_or_one(character_class("rR")),
double_quote,
any_non_single_quotes,
double_quote,
end
)
lint_message <- "Only use single-quotes."
}
MichaelChirico marked this conversation as resolved.
Show resolved Hide resolved
Linter(function(source_expression) {
if (!is_lint_level(source_expression, "file")) {
return(list())
}

content <- source_expression$full_parsed_content
str_idx <- which(content$token == "STR_CONST")
quote_matches <- which(re_matches(content[str_idx, "text"], quote_regex))

lapply(
quote_matches,
function(id) {
with(content[str_idx[id], ], {
line <- source_expression$file_lines[[line1]]
col2 <- if (line1 == line2) col2 else nchar(line)
Lint(
filename = source_expression$filename,
line_number = line1,
column_number = col1,
type = "style",
message = lint_message,
line = line,
ranges = list(c(col1, col2))
)
})
}
)
})
}
68 changes: 0 additions & 68 deletions R/single_quotes_linter.R

This file was deleted.

2 changes: 1 addition & 1 deletion R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ default_linters <- modify_defaults(
object_usage_linter(),
paren_body_linter(),
pipe_continuation_linter(),
quotes_linter(),
semicolon_linter(),
seq_linter(),
single_quotes_linter(),
spaces_inside_linter(),
spaces_left_parentheses_linter(),
T_and_F_symbol_linter(),
Expand Down
3 changes: 2 additions & 1 deletion inst/lintr/linters.csv
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@ paren_brace_linter,style readability deprecated
paste_linter,best_practices consistency configurable
pipe_call_linter,style readability
pipe_continuation_linter,style readability default
quotes_linter,style consistency readability default configurable
redundant_equals_linter,best_practices readability efficiency common_mistakes
redundant_ifelse_linter,best_practices efficiency consistency configurable
regex_subset_linter,best_practices efficiency
routine_registration_linter,best_practices efficiency robustness
semicolon_linter,style readability default configurable
semicolon_terminator_linter,style readability deprecated configurable
seq_linter,robustness efficiency consistency best_practices default
single_quotes_linter,style consistency readability default
single_quotes_linter,style consistency readability deprecated
sort_linter,readability best_practices efficiency
spaces_inside_linter,style readability default
spaces_left_parentheses_linter,style readability default
Expand Down
1 change: 1 addition & 0 deletions man/configurable_linters.Rd

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

1 change: 1 addition & 0 deletions man/consistency_linters.Rd

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

2 changes: 1 addition & 1 deletion man/default_linters.Rd

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

1 change: 1 addition & 0 deletions man/deprecated_linters.Rd

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

13 changes: 7 additions & 6 deletions man/linters.Rd

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

3 changes: 3 additions & 0 deletions man/lintr-deprecated.Rd

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

Loading