This object is given as input to each linter.
Value
A list
with three components:
- expressions
a
list
ofn+1
objects. The firstn
elements correspond to each expression infilename
, and consist of a list of 8 elements:filename
(character
) the name of the file.line
(integer
) the line in the file where this expression begins.column
(integer
) the column in the file where this expression begins.lines
(namedcharacter
) vector of all lines spanned by this expression, named with the corresponding line numbers.parsed_content
(data.frame
) as given byutils::getParseData()
for this expression.xml_parsed_content
(xml_document
) the XML parse tree of this expression as given byxmlparsedata::xml_parse_data()
.content
(character
) the same aslines
as a single string (not split across lines).xml_find_function_calls(function_names)
(function
) a function that returns allSYMBOL_FUNCTION_CALL
XML nodes fromxml_parsed_content
with specified function names.
The final element of
expressions
is a list corresponding to the full file consisting of 7 elements:filename
(character
) the name of this file.file_lines
(character
) thereadLines()
output for this file.content
(character
) for .R files, the same asfile_lines
; for .Rmd or .qmd scripts, this is the extracted R source code (as text).full_parsed_content
(data.frame
) as given byutils::getParseData()
for the full content.full_xml_parsed_content
(xml_document
) the XML parse tree of all expressions as given byxmlparsedata::xml_parse_data()
.terminal_newline
(logical
) records whetherfilename
has a terminal newline (as determined byreadLines()
producing a corresponding warning).xml_find_function_calls(function_names)
(function
) a function that returns allSYMBOL_FUNCTION_CALL
XML nodes fromfull_xml_parsed_content
with specified function names.
- error
A
Lint
object describing any parsing error.- lines
The
readLines()
output for this file.
Details
The file is read using the encoding
setting.
This setting is found by taking the first valid result from the following locations
The
encoding
key from the usual lintr configuration settings.The
Encoding
field from a PackageDESCRIPTION
file in a parent directory.The
Encoding
field from an R Project.Rproj
file in a parent directory."UTF-8"
as a fallback.
Examples
tmp <- tempfile()
writeLines(c("x <- 1", "y <- x + 1"), tmp)
get_source_expressions(tmp)
#> $expressions
#> $expressions[[1]]
#> $expressions[[1]]$filename
#> [1] "/tmp/Rtmpo8BiSD/file175525028297"
#>
#> $expressions[[1]]$line
#> [1] 1
#>
#> $expressions[[1]]$column
#> [1] 1
#>
#> $expressions[[1]]$lines
#> 1
#> "x <- 1"
#>
#> $expressions[[1]]$parsed_content
#> line1 col1 line2 col2 id parent token terminal text
#> 7 1 1 1 6 7 0 expr FALSE
#> 1 1 1 1 1 1 3 SYMBOL TRUE x
#> 3 1 1 1 1 3 7 expr FALSE
#> 2 1 3 1 4 2 7 LEFT_ASSIGN TRUE <-
#> 4 1 6 1 6 4 5 NUM_CONST TRUE 1
#> 5 1 6 1 6 5 7 expr FALSE
#>
#> $expressions[[1]]$xml_parsed_content
#> {xml_document}
#> <exprlist>
#> [1] <expr line1="1" col1="1" line2="1" col2="6" start="12" end="17">\n <expr ...
#>
#> $expressions[[1]]$xml_find_function_calls
#> function (function_names, keep_names = FALSE)
#> {
#> if (is.null(function_names)) {
#> res <- function_call_cache
#> }
#> else {
#> res <- function_call_cache[names(function_call_cache) %in%
#> function_names]
#> }
#> if (keep_names)
#> res
#> else unname(res)
#> }
#> <bytecode: 0x557b3fe26b20>
#> <environment: 0x557b37524f50>
#>
#> $expressions[[1]]$content
#> [1] "x <- 1"
#>
#>
#> $expressions[[2]]
#> $expressions[[2]]$filename
#> [1] "/tmp/Rtmpo8BiSD/file175525028297"
#>
#> $expressions[[2]]$line
#> [1] 2
#>
#> $expressions[[2]]$column
#> [1] 1
#>
#> $expressions[[2]]$lines
#> 2
#> "y <- x + 1"
#>
#> $expressions[[2]]$parsed_content
#> line1 col1 line2 col2 id parent token terminal text
#> 20 2 1 2 10 20 0 expr FALSE
#> 10 2 1 2 1 10 12 SYMBOL TRUE y
#> 12 2 1 2 1 12 20 expr FALSE
#> 11 2 3 2 4 11 20 LEFT_ASSIGN TRUE <-
#> 19 2 6 2 10 19 20 expr FALSE
#> 13 2 6 2 6 13 15 SYMBOL TRUE x
#> 15 2 6 2 6 15 19 expr FALSE
#> 14 2 8 2 8 14 19 '+' TRUE +
#> 16 2 10 2 10 16 17 NUM_CONST TRUE 1
#> 17 2 10 2 10 17 19 expr FALSE
#>
#> $expressions[[2]]$xml_parsed_content
#> {xml_document}
#> <exprlist>
#> [1] <expr line1="2" col1="1" line2="2" col2="10" start="23" end="32">\n <exp ...
#>
#> $expressions[[2]]$xml_find_function_calls
#> function (function_names, keep_names = FALSE)
#> {
#> if (is.null(function_names)) {
#> res <- function_call_cache
#> }
#> else {
#> res <- function_call_cache[names(function_call_cache) %in%
#> function_names]
#> }
#> if (keep_names)
#> res
#> else unname(res)
#> }
#> <bytecode: 0x557b3fe26b20>
#> <environment: 0x557b3751a8f8>
#>
#> $expressions[[2]]$content
#> [1] "y <- x + 1"
#>
#>
#> $expressions[[3]]
#> $expressions[[3]]$filename
#> [1] "/tmp/Rtmpo8BiSD/file175525028297"
#>
#> $expressions[[3]]$file_lines
#> 1 2
#> "x <- 1" "y <- x + 1"
#> attr(,"terminal_newline")
#> [1] TRUE
#>
#> $expressions[[3]]$content
#> 1 2
#> "x <- 1" "y <- x + 1"
#> attr(,"terminal_newline")
#> [1] TRUE
#>
#> $expressions[[3]]$full_parsed_content
#> line1 col1 line2 col2 id parent token terminal text
#> 7 1 1 1 6 7 0 expr FALSE
#> 1 1 1 1 1 1 3 SYMBOL TRUE x
#> 3 1 1 1 1 3 7 expr FALSE
#> 2 1 3 1 4 2 7 LEFT_ASSIGN TRUE <-
#> 4 1 6 1 6 4 5 NUM_CONST TRUE 1
#> 5 1 6 1 6 5 7 expr FALSE
#> 20 2 1 2 10 20 0 expr FALSE
#> 10 2 1 2 1 10 12 SYMBOL TRUE y
#> 12 2 1 2 1 12 20 expr FALSE
#> 11 2 3 2 4 11 20 LEFT_ASSIGN TRUE <-
#> 19 2 6 2 10 19 20 expr FALSE
#> 13 2 6 2 6 13 15 SYMBOL TRUE x
#> 15 2 6 2 6 15 19 expr FALSE
#> 14 2 8 2 8 14 19 '+' TRUE +
#> 16 2 10 2 10 16 17 NUM_CONST TRUE 1
#> 17 2 10 2 10 17 19 expr FALSE
#>
#> $expressions[[3]]$full_xml_parsed_content
#> {xml_document}
#> <exprlist>
#> [1] <expr line1="1" col1="1" line2="1" col2="6" start="12" end="17">\n <expr ...
#> [2] <expr line1="2" col1="1" line2="2" col2="10" start="23" end="32">\n <exp ...
#>
#> $expressions[[3]]$xml_find_function_calls
#> function (function_names, keep_names = FALSE)
#> {
#> if (is.null(function_names)) {
#> res <- function_call_cache
#> }
#> else {
#> res <- function_call_cache[names(function_call_cache) %in%
#> function_names]
#> }
#> if (keep_names)
#> res
#> else unname(res)
#> }
#> <bytecode: 0x557b3fe26b20>
#> <environment: 0x557b37514ab8>
#>
#> $expressions[[3]]$terminal_newline
#> [1] TRUE
#>
#>
#>
#> $error
#> NULL
#>
#> $lines
#> 1 2
#> "x <- 1" "y <- x + 1"
#> attr(,"terminal_newline")
#> [1] TRUE
#>
unlink(tmp)