Skip to content

Commit 6d620fb

Browse files
lorenzwalthertjonmcalder
authored andcommitted
rename set-args to set-assert-args, add documentation.
1 parent 992849b commit 6d620fb

12 files changed

+151
-86
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Collate:
5454
'rules-spacing.R'
5555
'serialize.R'
5656
'serialized_tests.R'
57-
'set-args.R'
57+
'set-assert-args.R'
5858
'style_guides.R'
5959
'styler.R'
6060
'token-create.R'

R/set-args.R

Lines changed: 0 additions & 33 deletions
This file was deleted.

R/set-assert-args.R

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#' Set the write_tree argument
2+
#'
3+
#' Sets the argument `write_tree` in [test_collection()] to be `TRUE` for R
4+
#' versions higher or equal to 3.2, and `FALSE` otherwise since the second-level
5+
#' dependency `DiagrammeR` from `data.table` is not available for R < 3.2.
6+
#' @param write_tree Whether or not to write tree.
7+
set_arg_write_tree <- function(write_tree) {
8+
sufficient_version <- getRversion() >= 3.2
9+
if (is.na(write_tree)) {
10+
write_tree <- ifelse(sufficient_version, TRUE, FALSE)
11+
} else if (!sufficient_version & write_tree) {
12+
stop_insufficient_r_version()
13+
}
14+
write_tree
15+
}
16+
17+
#' Set the file type argument
18+
#'
19+
#' Sets and asserts the file type argument to a standard format for further internal
20+
#' processing.
21+
#' @param filetype A character vector with file types to convert to the internal
22+
#' standard format.
23+
#' @examples
24+
#' styler:::set_and_assert_arg_filetype("rMd")
25+
#' \dontrun{
26+
#' styler:::set_and_assert_arg_filetype("xyz")
27+
#' }
28+
set_and_assert_arg_filetype <- function(filetype) {
29+
without_dot <- gsub("^\\.", "", tolower(filetype))
30+
assert_filetype(without_dot)
31+
paste0("\\.", without_dot)
32+
}
33+
34+
#' Make sure all supplied file types are allowed
35+
#'
36+
#' @param lowercase_filetype A vector with file types to check, all lower case.
37+
assert_filetype <- function(lowercase_filetype) {
38+
if (!all(lowercase_filetype %in% c("r", "rmd"))) {
39+
stop(
40+
"filetype must not contain other values than 'R'",
41+
"or 'Rmd' (case is ignored).", call. = FALSE
42+
)
43+
}
44+
}
45+
46+
47+
#' Assert text to be of positive length and replace it with the empty
48+
#' string otherwise.
49+
#' @param text The input to style.
50+
assert_text <- function(text) {
51+
if (length(text) < 1) {
52+
text <- ""
53+
}
54+
text
55+
}
56+
57+
58+
#' Check token validity
59+
#'
60+
#' Check whether one or more tokens exist and have a unique token-text mapping
61+
#' @param tokens Tokens to check.
62+
assert_tokens <- function(tokens) {
63+
invalid_tokens <- tokens[!(tokens %in% lookup_tokens()$token)]
64+
if (length(invalid_tokens) > 0) {
65+
stop(
66+
"Token(s) ", paste0(invalid_tokens, collapse = ", "), " are invalid. ",
67+
"You can lookup all valid tokens and their text ",
68+
"with styler:::looup_tokens(). Make sure you supply the values of ",
69+
"the column 'token', not 'text'."
70+
)
71+
}
72+
}
73+

R/style_guides.R

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -285,19 +285,3 @@ specify_math_token_spacing <-
285285
tidyverse_math_token_spacing <- function() {
286286
specify_math_token_spacing(one = c("'+'", "'-'", "'*'", "'/'", "'^'"))
287287
}
288-
289-
#' Check token validity
290-
#'
291-
#' Check whether one or more tokens exist and have a unique token-text mapping
292-
#' @param tokens Tokens to check.
293-
assert_tokens <- function(tokens) {
294-
invalid_tokens <- tokens[!(tokens %in% lookup_tokens()$token)]
295-
if (length(invalid_tokens) > 0) {
296-
stop(
297-
"Token(s) ", paste0(invalid_tokens, collapse = ", "), " are invalid. ",
298-
"You can lookup all valid tokens and their text ",
299-
"with styler:::looup_tokens(). Make sure you supply the values of ",
300-
"the column 'token', not 'text'."
301-
)
302-
}
303-
}

R/utils.R

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,6 @@ calls_sys <- function(sys_call, ...) {
4040
}
4141
}
4242

43-
#' Assert text to be of positive length and replace it with the empty
44-
#' string otherwise.
45-
#' @param text The input to style.
46-
assert_text <- function(text) {
47-
if (length(text) < 1) {
48-
text <- ""
49-
}
50-
text
51-
}
52-
5343
is_plain_r_file <- function(path) {
5444
grepl("\\.R$", path, ignore.case = TRUE)
5545
}
@@ -86,6 +76,11 @@ extend_if_comment <- function(pd, pos) {
8676
}
8777
}
8878

79+
#' Map the file type to a corresponding regular expression
80+
#'
81+
#' @param filetype The file type to map to a regex.
82+
#' @examples
83+
#' styler:::map_filetype_to_pattern(c(".rMd", "R"))
8984
map_filetype_to_pattern <- function(filetype) {
90-
paste0("(", paste(set_and_assert_filetype(filetype), collapse = "|"), ")$")
85+
paste0("(", paste(set_and_assert_arg_filetype(filetype), collapse = "|"), ")$")
9186
}

man/assert_filetype.Rd

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/assert_text.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/assert_tokens.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/map_filetype_to_pattern.Rd

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/set_and_assert_arg_filetype.Rd

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)