This object is given as input to each linter.
Value
A list with three components:
- expressions
a
listofn+1objects. The firstnelements 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 aslinesas a single string (not split across lines).xml_find_function_calls(function_names)(function) a function that returns allSYMBOL_FUNCTION_CALLXML nodes fromxml_parsed_contentwith specified function names.
The final element of
expressionsis 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 whetherfilenamehas a terminal newline (as determined byreadLines()producing a corresponding warning).xml_find_function_calls(function_names)(function) a function that returns allSYMBOL_FUNCTION_CALLXML nodes fromfull_xml_parsed_contentwith specified function names.
- error
A
Lintobject describing any parsing error.- warning
A
lintsobject describing any parsing warning.- 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
encodingkey from the usual lintr configuration settings.The
Encodingfield from a PackageDESCRIPTIONfile in a parent directory.The
Encodingfield from an R Project.Rprojfile 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/RtmpV9C2yT/file1ddc25bfb6d2"
#>
#> $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, include_s4_slots = FALSE)
#> {
#> if (is.null(function_names)) {
#> if (include_s4_slots) {
#> res <- combine_nodesets(function_call_cache, s4_slot_cache)
#> }
#> else {
#> res <- function_call_cache
#> }
#> }
#> else {
#> include_function_idx <- names(function_call_cache) %in%
#> function_names
#> if (include_s4_slots) {
#> res <- combine_nodesets(function_call_cache[include_function_idx],
#> s4_slot_cache[names(s4_slot_cache) %in% function_names])
#> }
#> else {
#> res <- function_call_cache[include_function_idx]
#> }
#> }
#> if (keep_names)
#> res
#> else unname(res)
#> }
#> <bytecode: 0x5583a3fc0070>
#> <environment: 0x5583a433faa0>
#>
#> $expressions[[1]]$content
#> [1] "x <- 1"
#>
#>
#> $expressions[[2]]
#> $expressions[[2]]$filename
#> [1] "/tmp/RtmpV9C2yT/file1ddc25bfb6d2"
#>
#> $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, include_s4_slots = FALSE)
#> {
#> if (is.null(function_names)) {
#> if (include_s4_slots) {
#> res <- combine_nodesets(function_call_cache, s4_slot_cache)
#> }
#> else {
#> res <- function_call_cache
#> }
#> }
#> else {
#> include_function_idx <- names(function_call_cache) %in%
#> function_names
#> if (include_s4_slots) {
#> res <- combine_nodesets(function_call_cache[include_function_idx],
#> s4_slot_cache[names(s4_slot_cache) %in% function_names])
#> }
#> else {
#> res <- function_call_cache[include_function_idx]
#> }
#> }
#> if (keep_names)
#> res
#> else unname(res)
#> }
#> <bytecode: 0x5583a3fc0070>
#> <environment: 0x5583a433b218>
#>
#> $expressions[[2]]$content
#> [1] "y <- x + 1"
#>
#>
#> $expressions[[3]]
#> $expressions[[3]]$filename
#> [1] "/tmp/RtmpV9C2yT/file1ddc25bfb6d2"
#>
#> $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, include_s4_slots = FALSE)
#> {
#> if (is.null(function_names)) {
#> if (include_s4_slots) {
#> res <- combine_nodesets(function_call_cache, s4_slot_cache)
#> }
#> else {
#> res <- function_call_cache
#> }
#> }
#> else {
#> include_function_idx <- names(function_call_cache) %in%
#> function_names
#> if (include_s4_slots) {
#> res <- combine_nodesets(function_call_cache[include_function_idx],
#> s4_slot_cache[names(s4_slot_cache) %in% function_names])
#> }
#> else {
#> res <- function_call_cache[include_function_idx]
#> }
#> }
#> if (keep_names)
#> res
#> else unname(res)
#> }
#> <bytecode: 0x5583a3fc0070>
#> <environment: 0x5583a423f408>
#>
#> $expressions[[3]]$terminal_newline
#> [1] TRUE
#>
#>
#>
#> $error
#> NULL
#>
#> $warning
#> NULL
#>
#> $lines
#> 1 2
#> "x <- 1" "y <- x + 1"
#> attr(,"terminal_newline")
#> [1] TRUE
#>
unlink(tmp)
