Skip to content

Commit b77932c

Browse files
respect shebangs
1 parent cb2e0f5 commit b77932c

File tree

10 files changed

+133
-2
lines changed

10 files changed

+133
-2
lines changed

R/expr-is.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,27 @@ is_function_dec <- function(pd) {
2929
pd$token[1] == "FUNCTION"
3030
}
3131

32+
is_comment <- function(pd) {
33+
if (is.null(pd)) return(FALSE)
34+
pd$token == "COMMENT"
35+
}
36+
37+
#' Identify comments that are shebangs
38+
#'
39+
#' Shebangs should be preserved and no space should be inserted between
40+
#' \# and !. A comment is a shebang if it is the first top level token
41+
#' (identified with `pos_id`) and if it starts with `#!`.
42+
#' @param pd A parse table.
43+
#' @examples
44+
#' style_text("#!/usr/bin/env Rscript")
45+
is_shebang <- function(pd) {
46+
if (is.null(pd)) return(rep(FALSE, nrow(pd)))
47+
is_first_comment <- is_comment(pd) & (pd$pos_id == 1L)
48+
is_first_comment[is_first_comment] <- grepl(
49+
"^#!", pd$text[is_first_comment], perl = TRUE
50+
)
51+
is_first_comment
52+
}
3253

3354
contains_else_expr <- function(pd) {
3455
any(pd$token == "ELSE")

R/rules-spacing.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,13 @@ set_space_between_levels <- function(pd_flat) {
181181
#' after the regex "^#+'*".
182182
#' @importFrom purrr map_chr
183183
start_comments_with_space <- function(pd, force_one = FALSE) {
184-
comment_pos <- pd$token == "COMMENT"
184+
comment_pos <- is_comment(pd) & !is_shebang(pd)
185185
if (!any(comment_pos)) return(pd)
186186

187187
comments <- rematch2::re_match(
188188
pd$text[comment_pos],
189189
"^(?<prefix>#+['\\*]*)(?<space_after_prefix> *)(?<text>.*)$"
190190
)
191-
192191
comments$space_after_prefix <- nchar(
193192
comments$space_after_prefix, type = "width"
194193
)

man/is_shebang.Rd

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#A comment
2+
#!/usr/bin/env Rscript
3+
#!/usr/bin/env Rscript
4+
a <- 3
5+
6+
#!/usr/bin /env Rscript -m --set "W"
7+
dd <- 33
8+
#!/usr/bin\ /env Rscript -m --set "W"
9+
c()
10+
#!NEED TO REMOVE THIS

tests/testthat/parse_comments/shebang_1-in_tree

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# A comment
2+
# !/usr/bin/env Rscript
3+
# !/usr/bin/env Rscript
4+
a <- 3
5+
6+
# !/usr/bin /env Rscript -m --set "W"
7+
dd <- 33
8+
# !/usr/bin\ /env Rscript -m --set "W"
9+
c()
10+
# !NEED TO REMOVE THIS
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env Rscript
2+
#!/usr/bin/env Rscript
3+
a <- 3
4+
5+
#!/usr/bin /env Rscript -m --set "W"
6+
dd <- 33
7+
#!/usr/bin\ /env Rscript -m --set "W"
8+
c()
9+
#!NEED TO REMOVE THIS

tests/testthat/parse_comments/shebang_2-in_tree

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env Rscript
2+
# !/usr/bin/env Rscript
3+
a <- 3
4+
5+
# !/usr/bin /env Rscript -m --set "W"
6+
dd <- 33
7+
# !/usr/bin\ /env Rscript -m --set "W"
8+
c()
9+
# !NEED TO REMOVE THIS

tests/testthat/test-parse_comments.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,10 @@ test_that("rplumber tags / syntax is handled properly", {
4141
"rplumber",
4242
transformer = style_text), NA)
4343
})
44+
45+
46+
test_that("hashbangs are respected", {
47+
expect_warning(test_collection("parse_comments",
48+
"shebang",
49+
transformer = style_text), NA)
50+
})

0 commit comments

Comments
 (0)