lintr icon indicating copy to clipboard operation
lintr copied to clipboard

trailing_blank_lines_linter for individual Rmd chunks?

Open MichaelChirico opened this issue 4 years ago • 5 comments

writeLines("
```{r code}
some_code





```

```{r code_2}
more_code
```
", tmp <- tempfile(fileext = ".Rmd"))

lintr::lint(tmp, lintr::trailing_blank_lines_linter())

Shouldn't we be linting all of those blank lines at the end of the first chunk as well?

MichaelChirico avatar Feb 08 '21 18:02 MichaelChirico

Definitely, these should be linted.

Thankfully, simply running {styler} should help clean up these lints.

tmp <- tempfile(fileext = ".Rmd")
cat(file = tmp, "
```{r chunk}
x <- 1



```
")

writeLines(readLines(tmp))
#> ```{r chunk}
#> x <- 1
#> 
#> 
#> 
#> ```

styler::style_file(tmp)
#> Styling  1  files:
#>  /var/folders/xr/v_vddzvs33q5wg7jv9mr__4h0000gn/T//RtmpKRn4HF/file7f5c2cf055dd.Rmd ℹ 
#> ────────────────────────────────────────────────────────────────────────────────
#> Status   Count   Legend 
#> ✔    0   File unchanged.
#> ℹ    1   File changed.
#> ✖    0   Styling threw an error.
#> ────────────────────────────────────────────────────────────────────────────────
#> Please review the changes carefully!

writeLines(readLines(tmp))
#> 
#> ```{r chunk}
#> x <- 1
#> ```

Created on 2022-10-04 with reprex v2.0.2

IndrajeetPatil avatar Oct 04 '22 09:10 IndrajeetPatil

Actually, this already works! I guess it's just a matter of adding tests?

library(lintr)

tmp <- tempfile(fileext = ".Rmd")
cat(
  file = tmp,
  "```{r chunk}
   x <- 1



   ```"
)

lint(tmp, linters = trailing_blank_lines_linter())
#> /private/var/folders/xr/v_vddzvs33q5wg7jv9mr__4h0000gn/T/Rtmp6B4mcl/file88023cfc3d1d.Rmd:3:1: style: [trailing_blank_lines_linter] Trailing blank lines are superfluous.
#> 
#> ^
#> /private/var/folders/xr/v_vddzvs33q5wg7jv9mr__4h0000gn/T/Rtmp6B4mcl/file88023cfc3d1d.Rmd:4:1: style: [trailing_blank_lines_linter] Trailing blank lines are superfluous.
#> 
#> ^
#> /private/var/folders/xr/v_vddzvs33q5wg7jv9mr__4h0000gn/T/Rtmp6B4mcl/file88023cfc3d1d.Rmd:5:1: style: [trailing_blank_lines_linter] Trailing blank lines are superfluous.
#> 
#> ^
#> /private/var/folders/xr/v_vddzvs33q5wg7jv9mr__4h0000gn/T/Rtmp6B4mcl/file88023cfc3d1d.Rmd:6:1: style: [trailing_blank_lines_linter] Missing terminal newline.
#> NA
#> ^

Created on 2022-10-04 with reprex v2.0.2

IndrajeetPatil avatar Oct 04 '22 11:10 IndrajeetPatil

the issue is referring to when internal chunks have trailing blank lines, say chunk 2 out of 3

MichaelChirico avatar Oct 04 '22 15:10 MichaelChirico

I am not sure if I still get it. 😢

In your example code, are you referring to these blank lines?

Screenshot 2022-10-04 at 17 47 56

IndrajeetPatil avatar Oct 04 '22 15:10 IndrajeetPatil

no, but your example works. chunk 1 has trailing blank lines after some code, chunk 2 does not

MichaelChirico avatar Oct 04 '22 16:10 MichaelChirico