-
Notifications
You must be signed in to change notification settings - Fork 195
Update sprintf_linter() to lint constant strings #2894
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
Changes from 12 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
af73210
Update sprintf_linter() tests to lint constant strings
Bisaloo 4efbd5a
Factor out fmt_by_name_xpath
Bisaloo e0c4903
Do not test for string fmt in in_pipe_xpath
Bisaloo 14236f0
Add lint to detect constant strings in sprintf
Bisaloo ea4aded
Fix constant sprintf call() in lintr codebase
Bisaloo cc37ebc
Simplify call_xpath()
Bisaloo 8be9818
Use fmt_by_name_xpath directly without assigning
Bisaloo 83081e9
Remove nested ifelse()
Bisaloo c81fae3
Add example for new lint
Bisaloo ffc47fd
Remove unnecessary glue() call
Bisaloo 85ce180
Use expect_no_lint() where appropriate
Bisaloo 8632a40
Add test for constant '%'
Bisaloo 906eb15
Test and handle %% case better
Bisaloo 5b15285
Use actual function name in lint message
Bisaloo 39240de
Convert one more expect_lint(, NULL, )
Bisaloo ee22ed7
Support comments at various positions
Bisaloo 7210624
Use action/reason format for message
Bisaloo d400ad5
Merge branch 'main' into constant-strings
Bisaloo 6be92d3
Use dedicated xp_call_name() function to get fct_name
Bisaloo 15526db
Document new sprintf_linter() lints in NEWS
Bisaloo 1447a29
one line in NEWS
MichaelChirico File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,11 @@ | |
| #' linters = sprintf_linter() | ||
| #' ) | ||
| #' | ||
| #' lint( | ||
| #' text = 'sprintf("hello")', | ||
| #' linters = sprintf_linter() | ||
| #' ) | ||
| #' | ||
| #' # okay | ||
| #' lint( | ||
| #' text = 'sprintf("hello %s %s %d", x, y, z)', | ||
|
|
@@ -29,20 +34,12 @@ | |
| sprintf_linter <- function() { | ||
| call_xpath <- " | ||
| parent::expr[ | ||
| ( | ||
| OP-LEFT-PAREN/following-sibling::expr[1]/STR_CONST or | ||
| SYMBOL_SUB[text() = 'fmt']/following-sibling::expr[1]/STR_CONST | ||
| ) and | ||
| not(expr/SYMBOL[text() = '...']) | ||
| ]" | ||
|
|
||
| pipes <- setdiff(magrittr_pipes, "%$%") | ||
| in_pipe_xpath <- glue("self::expr[ | ||
| preceding-sibling::*[1][self::PIPE or self::SPECIAL[{ xp_text_in_table(pipes) }]] | ||
| and ( | ||
| preceding-sibling::*[2]/STR_CONST | ||
| or SYMBOL_SUB[text() = 'fmt']/following-sibling::expr[1]/STR_CONST | ||
| ) | ||
| ]") | ||
|
|
||
| is_missing <- function(x) is.symbol(x) && !nzchar(x) | ||
|
|
@@ -104,15 +101,39 @@ sprintf_linter <- function() { | |
| Linter(linter_level = "file", function(source_expression) { | ||
| xml_calls <- source_expression$xml_find_function_calls(c("sprintf", "gettextf")) | ||
| sprintf_calls <- xml_find_all(xml_calls, call_xpath) | ||
| in_pipeline <- !is.na(xml_find_first(sprintf_calls, in_pipe_xpath)) | ||
|
|
||
| fmt_by_name <- get_r_string( | ||
| sprintf_calls, | ||
| "SYMBOL_SUB[text() = 'fmt']/following-sibling::expr[1]/STR_CONST" | ||
| ) | ||
| fmt_by_pos <- ifelse( | ||
| in_pipeline, | ||
| get_r_string(sprintf_calls, "preceding-sibling::*[2]/STR_CONST"), | ||
| get_r_string(sprintf_calls, "OP-LEFT-PAREN/following-sibling::expr[1]/STR_CONST") | ||
| ) | ||
|
|
||
| sprintf_warning <- vapply(sprintf_calls, capture_sprintf_warning, character(1L)) | ||
| fmt <- ifelse(!is.na(fmt_by_name), fmt_by_name, fmt_by_pos) | ||
| constant_fmt <- !is.na(fmt) & !grepl("%[^%]", fmt) | ||
|
|
||
| constant_fmt_lint <- xml_nodes_to_lints( | ||
| sprintf_calls[constant_fmt], | ||
| source_expression = source_expression, | ||
| lint_message = "A constant string is used and the sprintf call can be removed", | ||
|
||
| type = "warning" | ||
| ) | ||
|
|
||
| templated_sprintf_calls <- sprintf_calls[!constant_fmt & !is.na(fmt)] | ||
| sprintf_warning <- vapply(templated_sprintf_calls, capture_sprintf_warning, character(1L)) | ||
|
|
||
| has_warning <- !is.na(sprintf_warning) | ||
| xml_nodes_to_lints( | ||
| sprintf_calls[has_warning], | ||
| invalid_sprintf_lint <- xml_nodes_to_lints( | ||
| templated_sprintf_calls[has_warning], | ||
| source_expression = source_expression, | ||
| lint_message = sprintf_warning[has_warning], | ||
| type = "warning" | ||
| ) | ||
|
|
||
| c(constant_fmt_lint, invalid_sprintf_lint) | ||
| }) | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.