Skip to content

Commit

Permalink
Move path linters to their own files (#1723)
Browse files Browse the repository at this point in the history
* Move path linters to their own files

To have a one-to-one correspondence between linter and its test file

* redocument

* Update collate

* address review comments
  • Loading branch information
IndrajeetPatil authored Oct 17, 2022
1 parent 49e935b commit dd11338
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 60 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Collate:
'T_and_F_symbol_linter.R'
'utils.R'
'aaa.R'
'absolute_path_linter.R'
'actions.R'
'addins.R'
'any_duplicated_linter.R'
Expand Down Expand Up @@ -125,6 +126,7 @@ Collate:
'namespace_linter.R'
'nested_ifelse_linter.R'
'no_tab_linter.R'
'nonportable_path_linter.R'
'numeric_leading_zero_linter.R'
'object_length_linter.R'
'object_name_linter.R'
Expand All @@ -133,7 +135,7 @@ Collate:
'package_hooks_linter.R'
'paren_body_linter.R'
'paste_linter.R'
'path_linters.R'
'path_utils.R'
'pipe_call_linter.R'
'pipe_continuation_linter.R'
'redundant_equals_linter.R'
Expand Down
37 changes: 37 additions & 0 deletions R/absolute_path_linter.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#' Absolute path linter
#'
#' Check that no absolute paths are used (e.g. "/var", "C:\\System", "~/docs").
#'
#' @param lax Less stringent linting, leading to fewer false positives.
#' If `TRUE`, only lint path strings, which
#'
#' * contain at least two path elements, with one having at least two characters and
#' * contain only alphanumeric chars (including UTF-8), spaces, and win32-allowed punctuation
#'
#' @examplesIf getRversion() >= "4.0"
#' # Following examples use raw character constant syntax introduced in R 4.0.
#'
#' # will produce lints
#' lint(
#' text = 'R"--[/blah/file.txt]--"',
#' linters = absolute_path_linter()
#' )
#'
#' # okay
#' lint(
#' text = 'R"(./blah)"',
#' linters = absolute_path_linter()
#' )
#'
#' @evalRd rd_tags("absolute_path_linter")
#' @seealso [linters] for a complete list of linters available in lintr. \cr
#' [nonportable_path_linter()]
#' @export
absolute_path_linter <- function(lax = TRUE) {
path_linter_factory(
path_function = function(path) {
is_absolute_path(path) && is_valid_long_path(path, lax)
},
message = "Do not use absolute paths."
)
}
18 changes: 18 additions & 0 deletions R/nonportable_path_linter.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#' Non-portable path linter
#'
#' Check that [file.path()] is used to construct safe and portable paths.
#'
#' @inheritParams absolute_path_linter
#' @evalRd rd_tags("nonportable_path_linter")
#' @seealso [linters] for a complete list of linters available in lintr. \cr
#' [absolute_path_linter()]
#' @export
nonportable_path_linter <- function(lax = TRUE) {
path_linter_factory(
path_function = function(path) {
is_path(path) && is_valid_long_path(path, lax) && path != "/" &&
re_matches(path, rex(one_of("/", "\\")))
},
message = "Use file.path() to construct portable file paths."
)
}
57 changes: 0 additions & 57 deletions R/path_linters.R → R/path_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -159,60 +159,3 @@ path_linter_factory <- function(path_function, message, linter, name = linter_au
)
}, name = name)
}

#' Absolute path linter
#'
#' Check that no absolute paths are used (e.g. "/var", "C:\\System", "~/docs").
#'
#' @param lax Less stringent linting, leading to fewer false positives.
#' If `TRUE`, only lint path strings, which
#'
#' * contain at least two path elements, with one having at least two characters and
#' * contain only alphanumeric chars (including UTF-8), spaces, and win32-allowed punctuation
#'
#' @examplesIf getRversion() >= "4.0"
#' # Following examples use raw character constant syntax introduced in R 4.0.
#'
#' # will produce lints
#' lint(
#' text = 'R"--[/blah/file.txt]--"',
#' linters = absolute_path_linter()
#' )
#'
#' # okay
#' lint(
#' text = 'R"(./blah)"',
#' linters = absolute_path_linter()
#' )
#'
#' @evalRd rd_tags("absolute_path_linter")
#' @seealso [linters] for a complete list of linters available in lintr. \cr
#' [nonportable_path_linter()]
#' @export
absolute_path_linter <- function(lax = TRUE) {
path_linter_factory(
path_function = function(path) {
is_absolute_path(path) && is_valid_long_path(path, lax)
},
message = "Do not use absolute paths."
)
}

#' Non-portable path linter
#'
#' Check that [file.path()] is used to construct safe and portable paths.
#'
#' @inheritParams absolute_path_linter
#' @evalRd rd_tags("nonportable_path_linter")
#' @seealso [linters] for a complete list of linters available in lintr. \cr
#' [absolute_path_linter()]
#' @export
nonportable_path_linter <- function(lax = TRUE) {
path_linter_factory(
path_function = function(path) {
is_path(path) && is_valid_long_path(path, lax) && path != "/" &&
re_matches(path, rex(one_of("/", "\\")))
},
message = "Use file.path() to construct portable file paths."
)
}
2 changes: 1 addition & 1 deletion man/absolute_path_linter.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/nonportable_path_linter.Rd

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

0 comments on commit dd11338

Please sign in to comment.