Skip to content

Speed up object_overwrite_linter #2344

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions R/object_overwrite_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,9 @@ object_overwrite_linter <- function(

# test that the symbol doesn't match an argument name in the function
# NB: data.table := has parse token LEFT_ASSIGN as well
xpath <- glue("
xpath_assignments <- glue("
//SYMBOL[
not(text() = ancestor::expr/preceding-sibling::SYMBOL_FORMALS/text())
and ({ xp_text_in_table(pkg_exports$name) })
]/
parent::expr[
count(*) = 1
Expand All @@ -101,14 +100,17 @@ object_overwrite_linter <- function(

xml <- source_expression$xml_parsed_content

bad_expr <- xml_find_all(xml, xpath)
bad_symbol <- xml_text(xml_find_first(bad_expr, "SYMBOL"))
source_pkg <- pkg_exports$package[match(bad_symbol, pkg_exports$name)]
lint_message <-
sprintf("'%s' is an exported object from package '%s'. Avoid re-using such symbols.", bad_symbol, source_pkg)
assigned_exprs <- xml_find_all(xml, xpath_assignments)
assigned_symbols <- get_r_string(assigned_exprs, "SYMBOL")
is_bad <- assigned_symbols %in% pkg_exports$name
source_pkg <- pkg_exports$package[match(assigned_symbols[is_bad], pkg_exports$name)]
lint_message <- sprintf(
"'%s' is an exported object from package '%s'. Avoid re-using such symbols.",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copying here to put it all side-by-side:

'fun' overwrites 'pkg::fun'. Avoid overwriting symbols from other packages.

I don't like 'overwrites' which is misleading. 'masks' might be better. OTOH I don't see all that much wrong with the current message. Alternative:

'%s' is exported by package '%s'. Avoid re-using function names for local symbols.

assigned_symbols[is_bad], source_pkg
)

xml_nodes_to_lints(
bad_expr,
assigned_exprs[is_bad],
source_expression = source_expression,
lint_message = lint_message,
type = "warning"
Expand Down