r-lib / downlit

Syntax Highlighting and Automatic Linking
https://downlit.r-lib.org
Other
90 stars 22 forks source link

highlight failure #63

Closed kevinushey closed 4 years ago

kevinushey commented 4 years ago

Bumped into this while trying to build renv's pkgdown site via pkgdown::build_site().

x <-
  structure(
    list(src = "# get the path to the project library\npath <- renv::paths$library()\n"),
    class = "source"
  )

downlit::highlight(x)
#> Error in `$<-.data.frame`(`*tmp*`, "href", value = c(NA, NA, NA, NA, NA, : replacement has 16 rows, data has 15

Created on 2020-10-15 by the reprex package (v0.3.0)

kevinushey commented 4 years ago

Error occurs here:

https://github.com/r-lib/downlit/blob/df73cf308245f66dd20097f9a40a8f0a3263c566/R/highlight.R#L266-L271

The call indexes out of bounds here. The parse data looks like this:

Browse[2]> out
   line1 col1 line2 col2 id parent                token terminal                                  text
1      1    1     1   37  1    -19              COMMENT     TRUE # get the path to the project library
19     2    1     2   29 19      0                 expr    FALSE                                      
4      2    1     2    4  4      6               SYMBOL     TRUE                                  path
6      2    1     2    4  6     19                 expr    FALSE                                      
5      2    6     2    7  5     19          LEFT_ASSIGN     TRUE                                    <-
17     2    9     2   29 17     19                 expr    FALSE                                      
13     2    9     2   27 13     17                 expr    FALSE                                      
10     2    9     2   19 10     13                 expr    FALSE                                      
7      2    9     2   12  7     10       SYMBOL_PACKAGE     TRUE                                  renv
8      2   13     2   14  8     10               NS_GET     TRUE                                    ::
9      2   15     2   19  9     10               SYMBOL     TRUE                                 paths
11     2   20     2   20 11     13                  '$'     TRUE                                     $
12     2   21     2   27 12     13 SYMBOL_FUNCTION_CALL     TRUE                               library
14     2   28     2   28 14     17                  '('     TRUE                                     (
15     2   29     2   29 15     17                  ')'     TRUE                                     )

This occurs because I'm referencing a symbol called library(), not actually calling library().

hadley commented 4 years ago

Minimal reprex:

downlit::highlight("foo$library()")