Skip to content
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

safer Lint(), xml_nodes_to_lints() and lint() #1788

Merged
merged 19 commits into from
Dec 8, 2022
Merged

Conversation

AshesITR
Copy link
Collaborator

@AshesITR AshesITR commented Dec 3, 2022

fixes #1427, and ensures an easier debugging experience because Lint() errors immediately on NA in ranges.
xml_nodes_to_lints() uses default values if the location XPaths fail to return a number.
This could also be made an error. WDYT?

Note that the current fallback solution doesn't guarantee col1 <= column_number <= col2.
Do we want to enforce this as well?

@codecov-commenter
Copy link

codecov-commenter commented Dec 3, 2022

Codecov Report

Merging #1788 (371239d) into main (354d4ee) will decrease coverage by 0.05%.
The diff coverage is 83.05%.

@@            Coverage Diff             @@
##             main    #1788      +/-   ##
==========================================
- Coverage   98.89%   98.84%   -0.06%     
==========================================
  Files         111      111              
  Lines        4713     4756      +43     
==========================================
+ Hits         4661     4701      +40     
- Misses         52       55       +3     
Impacted Files Coverage Δ
R/get_source_expressions.R 93.93% <50.00%> (-0.46%) ⬇️
R/lint.R 96.72% <95.83%> (-0.07%) ⬇️
R/indentation_linter.R 100.00% <100.00%> (ø)
R/xml_nodes_to_lints.R 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@AshesITR
Copy link
Collaborator Author

AshesITR commented Dec 3, 2022

Related to #763 as well.
Might do this together.

@AshesITR AshesITR linked an issue Dec 3, 2022 that may be closed by this pull request
@AshesITR AshesITR linked an issue Dec 3, 2022 that may be closed by this pull request
@AshesITR
Copy link
Collaborator Author

AshesITR commented Dec 3, 2022

Funny how many bugs this change uncovered in our own codebase 😅

move column_number to nchar(line) + 1L if it's larger
anchor end-of-line parse error lints to nchar(line) + 1 as well instead of nchar(line)
@AshesITR
Copy link
Collaborator Author

AshesITR commented Dec 3, 2022

@IndrajeetPatil @MichaelChirico this is ready for review now, I think.

@IndrajeetPatil IndrajeetPatil added this to the 3.1.0 milestone Dec 5, 2022
@AshesITR AshesITR removed this from the 3.1.0 milestone Dec 7, 2022
@AshesITR AshesITR added this to the 3.0.3 milestone Dec 7, 2022
@AshesITR
Copy link
Collaborator Author

AshesITR commented Dec 7, 2022

The linked issues are milestone 3.0.3, so moving this PR to 3.0.3 as well.

@MichaelChirico
Copy link
Collaborator

Thanks for this! Mostly nit-picky comments, sorry about that. 95% of the way there 🤞

@IndrajeetPatil
Copy link
Collaborator

The linked issues are milestone 3.0.3, so moving this PR to 3.0.3 as well.

I'm confused now. From #1476, I understood that there isn't going to be a 3.0.3 release; the next release will be 3.1.0.

@AshesITR
Copy link
Collaborator Author

AshesITR commented Dec 7, 2022

Yes, but the 3.1.0 milestone contains a lot of issues tabled for later at the moment.
We could rename the milestones to help with the confusion?

#'
#' @noRd
fixup_line <- function(line) {
nchars <- tryCatch(nchar(line, type = "chars"), error = function(e) NA_integer_)
Copy link
Collaborator

Choose a reason for hiding this comment

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

tempting to use NA here, since we're only checking is.na(), since technically logical type could be smaller than integer... OTOH maybe NULL would allocate even less space. OTOOH this doesn't really matter. Moving on.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

pryr::object_size(NA)
#> 56 B
pryr::object_size(NA_integer_)
#> 56 B

Created on 2022-12-08 with reprex v2.0.2

I'd prefer type safety (nchars is always an integer) instead of code brevity here.

R/lint.R Outdated
@@ -382,7 +382,7 @@ reorder_lints <- function(lints) {
#' Create a `lint` object
#' @param filename path to the source file that was linted.
#' @param line_number line number where the lint occurred.
#' @param column_number column number where the lint occurred.
#' @param number column number where the lint occurred.
Copy link
Collaborator

Choose a reason for hiding this comment

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

is that intentional? this would be a user-facing API change? I guess this is by mistake...

@@ -418,6 +433,35 @@ Lint <- function(filename, line_number = 1L, column_number = 1L, # nolint: objec
obj
}

is_number <- function(number, n = 1L) {
length(number) == n && is.numeric(number) && !anyNA(number)
Copy link
Collaborator

Choose a reason for hiding this comment

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

TIL length() is ever-so-slightly faster & so is better to test first. nice!

@AshesITR AshesITR merged commit a936cf6 into main Dec 8, 2022
@AshesITR AshesITR deleted the fix/1427-errors branch December 8, 2022 06:01
@MichaelChirico MichaelChirico modified the milestones: 3.0.3, 3.1.0 Mar 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invalid parentheses can cause lintr to error or hang Bad error handling for invalid Lint()s
4 participants