Skip to content

Commit 342713a

Browse files
authored
Preserve multi-line content (#1869)
This ensures that we don't change existing behaviour, just give better warnings. Fixes #1867
1 parent ac0d90d commit 342713a

4 files changed

Lines changed: 29 additions & 60 deletions

File tree

R/rd-r6-external.R

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ roxy_tag_parse.roxy_tag_R6method <- function(x) {
55
warn_roxy_tag(x, "requires a value like {.code Class$method}")
66
return(NULL)
77
}
8-
if (warn_if_multiline(x, raw)) {
9-
return(NULL)
10-
}
8+
warn_if_multiline(x, raw, multiline = FALSE)
119

1210
pieces <- strsplit(raw, "\\$")[[1]]
1311
if (length(pieces) != 2 || pieces[1] == "" || pieces[2] == "") {

R/tag-parser.R

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ tag_value <- function(x, multiline = FALSE) {
2727
return(NULL)
2828
}
2929

30-
if (!multiline && warn_if_multiline(x, x$val)) {
31-
return(NULL)
32-
}
30+
warn_if_multiline(x, x$val, multiline)
3331

3432
if (!rdComplete(x$raw, is_code = FALSE)) {
3533
warn_roxy_tag(x, "has mismatched braces or quotes")
@@ -134,12 +132,11 @@ tag_two_part <- function(
134132
warn_roxy_tag(x, "requires two parts: {first} and {second}")
135133
}
136134
NULL
137-
} else if (!multiline && warn_if_multiline(x, trimws(x$raw))) {
138-
NULL
139135
} else if (!rdComplete(x$raw, is_code = FALSE)) {
140136
warn_roxy_tag(x, "has mismatched braces or quotes")
141137
NULL
142138
} else {
139+
warn_if_multiline(x, trimws(x$raw), multiline)
143140
pieces <- split_two_part(trimws(x$raw))
144141

145142
if (required && pieces[[2]] == "") {
@@ -192,9 +189,7 @@ tag_name_description <- function(x) {
192189
tag_words <- function(x, min = 0, max = Inf, multiline = FALSE) {
193190
val <- trimws(x$raw)
194191

195-
if (!multiline && warn_if_multiline(x, val)) {
196-
return(NULL)
197-
}
192+
warn_if_multiline(x, val, multiline)
198193

199194
if (!rdComplete(x$raw, is_code = FALSE)) {
200195
warn_roxy_tag(x, "has mismatched braces or quotes")
@@ -221,8 +216,11 @@ tag_words_line <- function(x) {
221216
tag_words(x)
222217
}
223218

224-
# Returns TRUE (and warns) if val contains multiple lines, FALSE otherwise.
225-
warn_if_multiline <- function(x, val) {
219+
# Warns if multiline is FALSE and val contains multiple lines.
220+
warn_if_multiline <- function(x, val, multiline) {
221+
if (multiline) {
222+
return(invisible())
223+
}
226224
n_lines <- re_count(val, "\n")
227225
if (n_lines >= 1) {
228226
first_line <- re_split_half(val, "\n")[[1]]
@@ -233,10 +231,8 @@ warn_if_multiline <- function(x, val) {
233231
i = "The first line is {.str {first_line}}"
234232
)
235233
)
236-
TRUE
237-
} else {
238-
FALSE
239234
}
235+
invisible()
240236
}
241237

242238
#' @export

tests/testthat/_snaps/tag-parser.md

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -163,54 +163,32 @@
163163
Message:
164164
x test.R:1: @test must have at most 1 word, not 2.
165165

166-
# tag_words() warns on multi-line content
166+
# tag_words() warns on multi-line content and preserves value
167167

168168
Code
169169
tag <- roxy_test_tag("a\nb")
170-
expect_parse_failure(tag_words(tag))
171-
Output
172-
<message/rlang_message>
173-
Message:
170+
out <- tag_words(tag)
171+
Message
174172
x test.R:1: @test must be only 1 line long, not 2.
175173
i The first line is "a"
176-
Code
177-
tag <- roxy_test_tag("a\nb\nc")
178-
expect_parse_failure(tag_words(tag))
179-
Output
180-
<message/rlang_message>
181-
Message:
182-
x test.R:1: @test must be only 1 line long, not 3.
183-
i The first line is "a"
184174

185-
# tag_two_part() warns on multi-line content by default
175+
# tag_two_part() warns on multi-line content and preserves value
186176

187177
Code
188178
tag <- roxy_test_tag("foo bar\nbaz")
189-
expect_parse_failure(tag_two_part(tag, "a name", "a value"))
190-
Output
191-
<message/rlang_message>
192-
Message:
179+
out <- tag_two_part(tag, "a name", "a value")
180+
Message
193181
x test.R:1: @test must be only 1 line long, not 2.
194182
i The first line is "foo bar"
195183

196-
# tag_value() warns on multi-line content
184+
# tag_value() warns on multi-line content and preserves value
197185

198186
Code
199187
tag <- roxy_test_tag("a\nb")
200-
expect_parse_failure(tag_value(tag))
201-
Output
202-
<message/rlang_message>
203-
Message:
188+
out <- tag_value(tag)
189+
Message
204190
x test.R:1: @test must be only 1 line long, not 2.
205191
i The first line is "a"
206-
Code
207-
tag <- roxy_test_tag("a\nb\nc")
208-
expect_parse_failure(tag_value(tag))
209-
Output
210-
<message/rlang_message>
211-
Message:
212-
x test.R:1: @test must be only 1 line long, not 3.
213-
i The first line is "a"
214192

215193
# tag_words_line() is deprecated
216194

tests/testthat/test-tag-parser.R

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,35 +77,32 @@ test_that("tag_words() gives useful warnings", {
7777
})
7878
})
7979

80-
test_that("tag_words() warns on multi-line content", {
80+
test_that("tag_words() warns on multi-line content and preserves value", {
8181
expect_snapshot({
8282
tag <- roxy_test_tag("a\nb")
83-
expect_parse_failure(tag_words(tag))
84-
85-
tag <- roxy_test_tag("a\nb\nc")
86-
expect_parse_failure(tag_words(tag))
83+
out <- tag_words(tag)
8784
})
85+
expect_equal(out$val, c("a", "b"))
8886
})
8987

90-
test_that("tag_two_part() warns on multi-line content by default", {
88+
test_that("tag_two_part() warns on multi-line content and preserves value", {
9189
expect_snapshot({
9290
tag <- roxy_test_tag("foo bar\nbaz")
93-
expect_parse_failure(tag_two_part(tag, "a name", "a value"))
91+
out <- tag_two_part(tag, "a name", "a value")
9492
})
93+
expect_equal(out$val, list(name = "foo", description = "bar\nbaz"))
9594

9695
tag <- roxy_test_tag("foo bar\nbaz")
97-
out <- tag_two_part(tag, "a name", "a value", multiline = TRUE)
96+
out <- expect_silent(tag_two_part(tag, "a name", "a value", multiline = TRUE))
9897
expect_equal(out$val, list(name = "foo", description = "bar\nbaz"))
9998
})
10099

101-
test_that("tag_value() warns on multi-line content", {
100+
test_that("tag_value() warns on multi-line content and preserves value", {
102101
expect_snapshot({
103102
tag <- roxy_test_tag("a\nb")
104-
expect_parse_failure(tag_value(tag))
105-
106-
tag <- roxy_test_tag("a\nb\nc")
107-
expect_parse_failure(tag_value(tag))
103+
out <- tag_value(tag)
108104
})
105+
expect_equal(out$val, "a\nb")
109106
})
110107

111108
test_that("tag_words_line() is deprecated", {

0 commit comments

Comments
 (0)