Skip to content

spaces_left_parentheses_linter uses XML tree - #724

Merged
AshesITR merged 21 commits into
masterfrom
spaces-left-paren-xml
Mar 13, 2021
Merged

AshesITR merged 21 commits into
masterfrom
spaces-left-paren-xml

Conversation

@MichaelChirico

@MichaelChirico MichaelChirico commented Jan 31, 2021 •

Copy link
Copy Markdown
Collaborator

Closes #628 , Closes #780

@russHyde

Copy link
Copy Markdown
Collaborator

Yes I can do that.

@russHyde

russHyde commented Feb 1, 2021

Copy link
Copy Markdown
Collaborator

Hi.
Running #724 found lints in these packages, that weren't found in main:

with(lints, pr %>% filter(!line %in% main$line) %>% count(package) %>% arrange(desc(n)))
# A tibble: 39 x 2
   package       n
   <chr>     <int>
 1 assertr      85
 2 GRANBase     71
 3 purrr        68
 4 shiny        47
 5 rlang        46
 6 knitr        23
 7 functools    20
 8 remotes      18
 9 withr        17
10 aoos         10
# … with 29 more rows

Conversely, the lints found in main but not #724:

with(lints, main %>% filter(!line %in% pr$line) %>% count(package) %>% arrange(desc(n)) %>% as.data.frame() %>% head(10))
      package  n
1    unitizer 15
2        Rcpp 11
3       aprof  5
4     assertr  5
5       lintr  4
6      import  3
7  Rd2roxygen  3
8   gWidgets2  2
9       knitr  2
10      rlang  2

assertr has unique lints in each branch.

Here's some examples

# called by the PR but not in main
with(lints, pr %>% filter(!line %in% main$line & package == "assertr") %>% sample_n(6)) %>% as.data.frame()
  package                         filename line_number column_number  type
1 assertr tests/testthat/test-predicates.R          94            57 style
2 assertr tests/testthat/test-predicates.R         179            32 style
3 assertr tests/testthat/test-predicates.R         158            31 style
4 assertr tests/testthat/test-predicates.R         133            31 style
5 assertr tests/testthat/test-predicates.R          77            35 style
6 assertr            vignettes/assertr.Rmd         313            33 style
                                                            message
1 Place a space before left parenthesis, except in a function call.
2 Place a space before left parenthesis, except in a function call.
3 Place a space before left parenthesis, except in a function call.
4 Place a space before left parenthesis, except in a function call.
5 Place a space before left parenthesis, except in a function call.
6 Place a space before left parenthesis, except in a function call.
                                                                     line
1   expect_equal(within_bounds(1,3, include.lower = FALSE)(c(0,1,2,3,4)),
2      expect_equal(within_n_mads(1)(test.vect3)(test.vect3[101]), FALSE)
3                                        expect_error(within_n_sds(1)(1),
4                   expect_equal(within_n_sds(2)(test.vect)(138.1), TRUE)
5                              expect_equal(within_bounds(3, 4)(3), TRUE)
6                        within_n_mads(1)(example.vector)(example.vector)
  linter
1 linter
2 linter
3 linter
4 linter
5 linter
6 linter
# Called in main but not the PR
with(lints, main %>% filter(!line %in% pr$line & package == "assertr")) %>% as.data.frame()
  package       filename line_number column_number  type
1 assertr R/predicates.R         247            23 style
2 assertr R/predicates.R         247            39 style
3 assertr R/predicates.R         321            25 style
4 assertr R/predicates.R         321            42 style
5 assertr  R/row-redux.R         112            40 style
                                                            message
1 Place a space before left parenthesis, except in a function call.
2 Place a space before left parenthesis, except in a function call.
3 Place a space before left parenthesis, except in a function call.
4 Place a space before left parenthesis, except in a function call.
5 Place a space before left parenthesis, except in a function call.
                                                       line linter
1        within_bounds((mu-(n*stdev)), (mu+(n*stdev)), ...) linter
2        within_bounds((mu-(n*stdev)), (mu+(n*stdev)), ...) linter
3      within_bounds((dmed-(n*dmad)), (dmed+(n*dmad)), ...) linter
4      within_bounds((dmed-(n*dmad)), (dmed+(n*dmad)), ...) linter
5             function(x){ sum((is.na(x)&(!(is.nan(x))))) } linter

@russHyde

russHyde commented Feb 1, 2021

Copy link
Copy Markdown
Collaborator

f(x)(y)(z) seems to be a problem, but g((x * y)) seems to be fixed

@MichaelChirico

Copy link
Copy Markdown
Collaborator Author

Thanks! As I suspected *( is not enough. I see -( and +( there too. Also &(...

The repeated calls thing is the main work ahead. Will revisit.

@MichaelChirico

Copy link
Copy Markdown
Collaborator Author

Much more confident in the current approach so removing WIP. Running some comparisons now to see if there are some more edge cases to consider.

@AshesITR

Copy link
Copy Markdown
Collaborator

Did you see the lint annotations in unchanged files?
There seems to be a problem with immediately preceeding linebreaks, e.g. lint("x <- a &&\n (b_in_parentheses)")

@MichaelChirico

Copy link
Copy Markdown
Collaborator Author

Seems we'll need to use the affirmative (@start - 1 = @end) rather than the positive (@start - 2 != @end). Will fix.

@MichaelChirico

MichaelChirico commented Mar 11, 2021 •

Copy link
Copy Markdown
Collaborator Author

Still missing a ton of branches caught on current HEAD. Mostly because of nested arithmetic expressions; fixing that now.

HEAD might generate lints for files that don't parse where this branch doesn't; I think behavior in error cases doesn't need to be consistent.

The number of lints caught here and not on main is very small. All seem to be generated by files in the demo folder of this package:

https://github.com/cran/CVD/blob/master/demo/FarnsworthD15.R

For some reason HEAD is not generating a lint on this line:

https://github.com/cran/CVD/blob/85474eb2f075943a488bb25b19a2b4c0b8ba6812/demo/FarnsworthD15.R#L49

posI<-(as.numeric(strToB))

@MichaelChirico

Copy link
Copy Markdown
Collaborator Author

There's one case that is pretty tough to fix with an XML approach:

x <- 5;(x+2)

That's because those are split into different expressions and we're using the expression-level approach here for cache-friendliness.

It's pretty rare (in 500 tested packages, it happened 3 times) -- WDYT?

@AshesITR

Copy link
Copy Markdown
Collaborator

There's one case that is pretty tough to fix with an XML approach:

x <- 5;(x+2)

That's because those are split into different expressions and we're using the expression-level approach here for cache-friendliness.

It's pretty rare (in 500 tested packages, it happened 3 times) -- WDYT?

I would favour correctness over cache-friendlyness.

@MichaelChirico

MichaelChirico commented Mar 11, 2021 •

Copy link
Copy Markdown
Collaborator Author

I would favour correctness over cache-friendlyness

OK, done; it's actually pretty easy to just write a targeted version of the logic for the global case.

@MichaelChirico

Copy link
Copy Markdown
Collaborator Author

Consolidated all of the apparent false positives caught by this branch in #780. Purging those from the discussion above to provide more focus.

@MichaelChirico

Copy link
Copy Markdown
Collaborator Author

Not sure what's going on with coverage...

I had to add the branch in 80a0139 since tests were failing. Tests are passing now but codecov says that branch is missed? Not sure how that can be.

xml_nodes_to_lint still isn't used in any linters where it's possible for the start & end lines of the matched node to be different, so that's still unreachable (though it should be reachable eventually if we add more linters to use it)

Comment thread R/spaces_left_parentheses_linter.R Outdated
})

if (is.null(source_file$xml_parsed_content)) {
if (is.null(source_file$full_xml_parsed_content)) return(list())

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This if might be unreachable, either xml_parsed_content or full_xml_parsed_content is filled (unless maybe parsing fails, not sure about that).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

in fact it was this test that caused was triggering an error:

https://github.com/jimhester/lintr/blob/1aa166ef7aa3d85a667675847c277b83b4ff4908/tests/testthat/test-error.R#L3

Hence including this in 80a0139 -- maybe that same error doesn't show up on the codecov platform for some reason?

@AshesITR

Copy link
Copy Markdown
Collaborator

Maybe we should unit test xml_nodes_to_lint() to get 100% coverage before merging this?

@MichaelChirico

Copy link
Copy Markdown
Collaborator Author

Maybe we should unit test xml_nodes_to_lint() to get 100% coverage before merging this?

Are you unable to just merge as is? I think it's fine to occasionally make a one-line backslide like this now that we're >99%, given that we look at the issue and decide it's OK.

@MichaelChirico

MichaelChirico commented Mar 12, 2021 •

Copy link
Copy Markdown
Collaborator Author

Ran on 1000 packages at current commit in this branch -- all that are left are differences in lints spotted in the presence of syntax errors, and the false positives mentioned in #780 -- good to merge IMO!

Here is a file of the ~1300 such lints found at HEAD but not in this PR (out of ~125K lints total on HEAD):

main_not_pr.txt

You can apply this filter to skip the false positives IINM:

!grepl("(break|next)\\([a-z]?\\)", line) & !grepl("@", line) & !grepl("[-*'\"~][(]", line)

The rest (23 more) are all syntax errors AFAICT

@AshesITR

Copy link
Copy Markdown
Collaborator

Maybe we should unit test xml_nodes_to_lint() to get 100% coverage before merging this?

Are you unable to just merge as is? I think it's fine to occasionally make a one-line backslide like this now that we're >99%, given that we look at the issue and decide it's OK.

Merging is blocked by codecov.
Getting the coverage back up shouldn't be too much work IINM though.

@MichaelChirico

Copy link
Copy Markdown
Collaborator Author

Just confirmed for sure that branch is being hit locally for me. Just pushed a test commit with tracing, let's see what comes out in the logs.

@MichaelChirico

Copy link
Copy Markdown
Collaborator Author

Didn't really see anything in the trace log 🧐

Anyway, added a ::: test for xml_nodes_to_lint -- glad we did! caught a small bug.

@MichaelChirico

Copy link
Copy Markdown
Collaborator Author

Still couldn't hit the required coverage... #nocov'd that line for now to just move on.

Comment thread R/utils.R Resolved
Comment thread R/spaces_left_parentheses_linter.R Resolved
Comment thread tests/testthat/test-utils.R Outdated
# for coverage
test_that("xml_nodes_to_lint handles >1-line nodes correctly", {
xml <- xml2::read_xml('<x line1="1" col1="1" line2="2" col2="10" start="8" end="25"></x>')
source_file = list(filename = "test", lines = c(`1` = "abcdefg", `2` = "hijklmnopq"))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

here's a lint

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

thanks! is there a place to see these without checking out the Changes tab (which I don't always do)...

maybe I should write a precommit hook for this

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

You can manually look at the action logs, e.g. here
Other than that, locally running devtools::load_all(); lint_package() is probably the most convenient way.

@AshesITR AshesITR left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@AshesITR
AshesITR merged commit 9878ce3 into master Mar 13, 2021
@AshesITR
AshesITR deleted the spaces-left-paren-xml branch March 13, 2021 10:59
@MichaelChirico

Copy link
Copy Markdown
Collaborator Author

Just realized we never included a NEWS item here :\ would have been useful. e.g. this update to the linter fixed some false positives, e.g. something we see a lot in data.table usage:

DT[, ":="(a = 1, b = 2)]

Would get linted because "( was considered a lint even though it's a legit call here.

From the discussion above it seems there were a few such edge cases that are now handled "more correctly" but I'm forgetting the specifics...

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.

False positives in spaces_left_parentheses_linter Refactor spaces_left_parentheses_linter to use xpath

3 participants