Skip to content

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

Closed

Description

@AshesITR

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")
    )
  )

Activity

  1. AshesITR commented on Nov 28, 2020

    @AshesITR
    CollaboratorAuthor

    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
  2. MichaelChirico commented on Nov 28, 2020

    @MichaelChirico
    Collaborator
  3. AshesITR commented on Nov 28, 2020

    @AshesITR
    CollaboratorAuthor

    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)
  4. MichaelChirico commented on Nov 28, 2020

    @MichaelChirico
    Collaborator

    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.

  5. AshesITR commented on Nov 28, 2020

    @AshesITR
    CollaboratorAuthor

    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.

  6. added
    feature
    on Dec 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions