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

Feature: allow turning off specific linters per-line #605

Closed
AshesITR opened this issue Nov 28, 2020 · 5 comments · Fixed by #660
Closed

Feature: allow turning off specific linters per-line #605

AshesITR opened this issue Nov 28, 2020 · 5 comments · Fixed by #660
Labels
feature a feature request or enhancement

Comments

@AshesITR
Copy link
Collaborator

It might be useful to disable only a specific linter on a per-line basis, e.g.

`%||%` = function(x, y) {
  if (is.null(x)) y else x
}

produces two default lints: object_name_linter and assignment_linter.
One may actively decide that %||% is an acceptable name here and not want to generally disable object_name_linter.

Currently, this can only be achieved by # nolint'ing the entire line

`%||%` = function(x, y) { # nolint
  if (is.null(x)) y else x
}

but unfortunately this also kills the assignment_linter

It would be very useful to have a way to only exclude some linters on a line (or in a block).
A syntax might be a space-separated list of linters to be disabled, e.g.

`%||%` = function(x, y) { # nolint object_name_linter
  if (is.null(x)) y else x
}

I realize this would require work in the way exclusions are specified. Especially the line-based specification in .lintr files or the exclusions argument requires thought.

One solution would be one more level of (optional) nesting

exclude: list(
    "my_file.R" = list(
      1,
      "2" = "object_name_linter",
      "4" = c("line_length_linter", "commented_code_linter")
    )
  )
@AshesITR
Copy link
Collaborator Author

Another usecase would be to enforce the location of library() calls in some centralized location for larger analysis projects.
You could define library_linter = undesirable_functions_linter(c("library" = "place all library() calls at the top of main.R"))
and combine that with a section like

# nolint start library_linter
# Place all necessary library() calls here
library(tidyverse)
# nolint end

@MichaelChirico
Copy link
Collaborator

MichaelChirico commented Nov 28, 2020 via email

@AshesITR
Copy link
Collaborator Author

I think whatever the API becomes, it should be specified as some well-defined suffix of the (user-configured) # nolint and # nolint start tags.

Tending against parenthesis because that looks akward for block exclusions:

# nolint start(library_linter, object_name_linter)

@MichaelChirico
Copy link
Collaborator

MichaelChirico commented Nov 28, 2020

Also note that a blanket inclusion of things after the # nolint tag is likely not to work (or at least to be awkward), as it's not uncommon to write some notes there about why that line's nolinted (I know I do this for covr especially), e.g.

someReallyLongLineActuallyLongerThan120Chars # nolint this object is defined elsewhere, no choice but to use here

which is why I think we'll need some sort of glyph to signal "this is naming linters, not free text"

I think it should "work" to consider all the stuff after nolint as linter names (maybe comma-separated) as it's unlikely to turn up matches in named lilnters, but still feels awkward.

@AshesITR
Copy link
Collaborator Author

AshesITR commented Nov 28, 2020

What about a colon and a dot?

# nolint: library_linter, object_name_linter. This is free text and will not be interpreted

# nolint start: library_linter. This ...

On the other end it's a lot easier to remember "just stick the names at the end".
We'd need to be smart about making it a general # nolint if none of the tokens following match any of the named linters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants