lintr icon indicating copy to clipboard operation
lintr copied to clipboard

Linter for package code that is not within a function

Open gaborcsardi opened this issue 7 years ago • 5 comments

This is usually bad practice.

gaborcsardi avatar May 17 '17 12:05 gaborcsardi

Could you be more specific about what should be linted exactly?

MichaelChirico avatar Apr 05 '22 04:04 MichaelChirico

Well, I am not entirely sure, it depends on how strict you want to be. If very strict, then all package code that is not a function definition. But of course this would also catch (S4, R6, etc.) class definitions, which is not good. So maybe it is not realistic to implement this.

gaborcsardi avatar Apr 05 '22 08:04 gaborcsardi

Maybe lint all top level non-assignment expressions except for some whitelisted function calls, string literals (used for roxygen on data) and NULL?

Otherwise, factory patterns (e.g. R6) would be false positives.

Alternatively, another whitelist of factory functions would be necessary.

AshesITR avatar Apr 05 '22 19:04 AshesITR

Top level assignments can be bad as well, actually, e.g.

foo <- anotherpackage::fun()

is not good practice. (Of course sometimes you do need it.)

gaborcsardi avatar Apr 05 '22 19:04 gaborcsardi

Yeah, that's exactly the factory pattern I meant. MyClass <- R6::R6Class(...)

I have yet to see packages with problematic use of that pattern, though.

AshesITR avatar Apr 05 '22 20:04 AshesITR

Happy to support anyone trying to implement this, but it seems there's not much interest.

MichaelChirico avatar Nov 14 '23 23:11 MichaelChirico