Skip to content

add name attribute to Linter class #753

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

Merged
merged 11 commits into from
Feb 16, 2021
Merged
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
* `paren_brace_linter` now marks lints at the opening brace instead of the closing parenthesis, making fixing the lints
by jumping to source markers easier (#583, @AshesITR)
* Lints are now marked with the name of the `linter` that caused them instead of the name of their implementation
function (#664, #673, @AshesITR).
function.
Deprecated the obsolete `linter` argument of `Lint()`. (#664, #673, #746, @AshesITR)
* New syntax to exclude only selected linters from linting lines or passages. Use `# nolint: linter_name, linter2_name.`
or `# nolint start: linter_name, linter2_name.` in source files or named lists of line numbers in `.lintr`.
(#660, @AshesITR)
Expand Down
3 changes: 1 addition & 2 deletions R/T_and_F_symbol_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ T_and_F_symbol_linter <- function() { # nolint: object_name_linter.
type = "style",
message = sprintf("Use %s instead of the symbol %s.", replacement, symbol),
line = source_file[["lines"]][[as.character(line_num)]],
ranges = list(c(start_col_num, end_col_num)),
linter = "T_and_F_symbol_linter"
ranges = list(c(start_col_num, end_col_num))
)
}
}
Expand Down
10 changes: 5 additions & 5 deletions R/assignment_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#' @export
assignment_linter <- function() {
Linter(function(source_file) {
lapply(ids_with_token(source_file, "EQ_ASSIGN"),
lapply(
ids_with_token(source_file, "EQ_ASSIGN"),
function(id) {
parsed <- with_id(source_file, id)
Lint(
Expand All @@ -11,9 +12,8 @@ assignment_linter <- function() {
column_number = parsed$col1,
type = "style",
message = "Use <-, not =, for assignment.",
line = source_file$lines[as.character(parsed$line1)],
linter = "assignment_linter"
)
line = source_file$lines[as.character(parsed$line1)]
)
})
})
})
}
3 changes: 1 addition & 2 deletions R/assignment_spaces_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ assignment_spaces <- function() {
column_number = parsed$col1,
type = "style",
message = "Assignments should only have one space before and after the operator.",
line = source_file$lines[parsed$line1],
linter = "assignment_spaces"
line = source_file$lines[parsed$line1]
)
}
}
Expand Down
3 changes: 1 addition & 2 deletions R/backport_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ backport_linter <- function(r_version = getRversion()) {
all_names[ii], names(needs_backport_names)[which(needs_backport[ii, ])], r_version
),
line = source_file$lines[[line1]],
ranges = list(c(col1, col2)),
linter = "backport_linter"
ranges = list(c(col1, col2))
)
})
})
Expand Down
3 changes: 1 addition & 2 deletions R/closed_curly_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ closed_curly_linter <- function(allow_single_line = FALSE) {
"Closing curly-braces should always be on their own line,",
"unless they are followed by an else."
),
line = source_file$lines[as.character(parsed$line1)],
linter = "closed_curly_linter"
line = source_file$lines[as.character(parsed$line1)]
)}
}
)
Expand Down
39 changes: 17 additions & 22 deletions R/commas_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,15 @@ commas_linter <- function() {
!empty_comma &&
!is_blank_switch) {

lints[[length(lints) + 1L]] <-
Lint(
filename = source_file$filename,
line_number = line_number,
column_number = comma_loc,
type = "style",
message = "Commas should never have a space before.",
line = line,
ranges = list(c(start, end)),
"commas_linter"
)
lints[[length(lints) + 1L]] <- Lint(
filename = source_file$filename,
line_number = line_number,
column_number = comma_loc,
type = "style",
message = "Commas should never have a space before.",
line = line,
ranges = list(c(start, end))
)
}
}

Expand All @@ -83,17 +81,14 @@ commas_linter <- function() {
source_file$parsed_content$token == "','")

if (has_token) {

lints[[length(lints) + 1L]] <-
Lint(
filename = source_file$filename,
line_number = line_number,
column_number = comma_loc + 1,
type = "style",
message = "Commas should always have a space after.",
line = line,
linter = "commas_linter"
)
lints[[length(lints) + 1L]] <- Lint(
filename = source_file$filename,
line_number = line_number,
column_number = comma_loc + 1,
type = "style",
message = "Commas should always have a space after.",
line = line
)
}

}
Expand Down
4 changes: 1 addition & 3 deletions R/comment_linters.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ commented_code_linter <- function() {
type = "style",
message = "Commented code should be removed.",
line = source_file$file_lines[line_number],
linter = "commented_code_linter",
ranges = list(column_offset + c(code_candidates[code_candidate, "code.start"],
code_candidates[code_candidate, "code.end"]))
)
Expand Down Expand Up @@ -95,8 +94,7 @@ todo_comment_linter <- function(todo = c("todo", "fixme")) {
type = "style",
message = "TODO comments should be removed.",
line = source_file[["lines"]][[as.character(token[["line1"]])]],
ranges = list(c(token[["col1"]], token[["col2"]])),
linter = "todo_comment_linter"
ranges = list(c(token[["col1"]], token[["col2"]]))
)
}
)
Expand Down
3 changes: 1 addition & 2 deletions R/cyclocomp_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ cyclocomp_linter <- function(complexity_limit = 15L) {
complexity_limit, ", this has ", complexity, "."
),
ranges = list(c(source_file[["column"]][1], source_file[["column"]][1])),
line = source_file$lines[1],
linter = "cyclocomp_linter"
line = source_file$lines[1]
)
})
}
17 changes: 8 additions & 9 deletions R/deprecated.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ absolute_paths_linter <- function(source_file) {
absolute_path_linter(lax = TRUE)(source_file)
}
class(absolute_paths_linter) <- "linter"
attr(absolute_paths_linter, "name") <- "absolute_paths_linter"


#' @describeIn lintr-deprecated Check there are no trailing semicolons.
Expand All @@ -44,6 +45,7 @@ trailing_semicolons_linter <- function(source_file) {
semicolon_terminator_linter(semicolon = "trailing")(source_file)
}
class(trailing_semicolons_linter) <- "linter"
attr(trailing_semicolons_linter, "name") <- "trailing_semicolons_linter"


#' @describeIn lintr-deprecated Check that objects are not in camelCase.
Expand All @@ -59,10 +61,9 @@ camel_case_linter <- make_object_linter(function(source_file, parsed) {
!is_base_function(parsed$text)) {
object_lint(source_file,
parsed,
"Variable and function names should be all lowercase.",
"camel_case_linter")
"Variable and function names should be all lowercase.")
}
})
}, name = "camel_case_linter")


#' @describeIn lintr-deprecated Check that objects are not in snake_case.
Expand All @@ -78,10 +79,9 @@ snake_case_linter <- make_object_linter(function(source_file, parsed) {
!is_base_function(parsed$text)) {
object_lint(source_file,
parsed,
"Variable and function names should not use underscores.",
"snake_case_linter")
"Variable and function names should not use underscores.")
}
})
}, name = "snake_case_linter")


#' @describeIn lintr-deprecated check that objects do not have.multiple.dots.
Expand All @@ -96,7 +96,6 @@ multiple_dots_linter <- make_object_linter(function(source_file, parsed) {
!is_base_function(parsed$text)) {
object_lint(source_file,
parsed,
"Words within variable and function names should be separated by '_' rather than '.'.",
"multiple_dots_linter")
"Words within variable and function names should be separated by '_' rather than '.'.")
}
})
}, name = "multiple_dots_linter")
2 changes: 1 addition & 1 deletion R/equals_na_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ equals_na_linter <- function() {

lapply(bad_expr, xml_nodes_to_lint, source_file,
message = "Use is.na for comparisons to NA (not == or !=)",
linter = "equals_na_linter", type = "warning")
type = "warning")
})
}
5 changes: 3 additions & 2 deletions R/expect_lint.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ expect_lint <- function(content, checks, ..., file = NULL, language = "en") {
}

local({
itr <- 0L #nolint
lint_fields <- names(formals(Lint))
itr <- 0L
# keep 'linter' as a field even if we remove the deprecated argument from Lint() in the future
lint_fields <- unique(c(names(formals(Lint)), "linter"))
Map(function(lint, check) {
itr <<- itr + 1L
lapply(names(check), function(field) {
Expand Down
6 changes: 3 additions & 3 deletions R/extraction_operator_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#' @export
extraction_operator_linter <- function() {
Linter(function(source_file) {
tokens <- source_file[["parsed_content"]] <-
filter_out_token_type(source_file[["parsed_content"]], "expr")
tokens <- source_file[["parsed_content"]] <- filter_out_token_type(source_file[["parsed_content"]], "expr")

lapply(
ids_with_token(source_file, c("'$'", "'['"), fun = `%in%`),
function(token_num) {
Expand All @@ -14,14 +14,14 @@ extraction_operator_linter <- function() {
end_col_num <- token[["col2"]]
line_num <- token[["line1"]]
line <- source_file[["lines"]][[as.character(line_num)]]

Lint(
filename = source_file[["filename"]],
line_number = line_num,
column_number = start_col_num,
type = "warning",
message = sprintf("Use `[[` instead of `%s` to extract an element.", token[["text"]]),
line = line,
linter = "extraction_operator_linter",
ranges = list(c(start_col_num, end_col_num))
)
}
Expand Down
7 changes: 3 additions & 4 deletions R/function_left_parentheses.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ function_left_parentheses_linter <- function() { # nolint: object_length_linter.
column_number = parsed$col1,
type = "style",
message = "Remove spaces before the left parenthesis in a function call.",
line = line,
linter = "function_left_parentheses_linter"
line = line
)
}
}

})
}
)
})
}
12 changes: 4 additions & 8 deletions R/get_source_expressions.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ get_source_expressions <- function(filename) {
column_number = 1,
type = "error",
message = e$message,
line = "",
linter = "error"
line = ""
)
)
# nocov end
Expand All @@ -123,8 +122,7 @@ get_source_expressions <- function(filename) {
column_number = column_number,
type = "error",
message = e$message,
line = source_file$lines[[line_number]],
linter = "error"
line = source_file$lines[[line_number]]
)
)
}
Expand All @@ -148,8 +146,7 @@ get_source_expressions <- function(filename) {
column_number = column_number,
type = "error",
message = message_info$message,
line = line,
linter = "error"
line = line
)
}

Expand Down Expand Up @@ -178,8 +175,7 @@ get_source_expressions <- function(filename) {
column_number = column_number,
type = "error",
message = message_info$message,
line = source_file$lines[line_number],
linter = "error"
line = source_file$lines[line_number]
)
}

Expand Down
3 changes: 1 addition & 2 deletions R/implicit_integer_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ implicit_integer_linter <- function() {
message =
"Integers should not be implicit. Use the form 1L for integers or 1.0 for doubles.",
line = source_file[["lines"]][[as.character(line_num)]],
ranges = list(c(start_col_num, end_col_num)),
linter = "implicit_integer_linter"
ranges = list(c(start_col_num, end_col_num))
)
}
}
Expand Down
18 changes: 6 additions & 12 deletions R/infix_spaces_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,29 @@ infix_tokens <- c(
"'*'", # * : unary multiplication

NULL
)
)

#' @describeIn linters Check that infix operators are surrounded by spaces.
#' @export
infix_spaces_linter <- function() {
Linter(function(source_file) {
lapply(ids_with_token(source_file, infix_tokens, fun = `%in%`),
lapply(
ids_with_token(source_file, infix_tokens, fun = `%in%`),
function(id) {
parsed <- with_id(source_file, id)

line <- source_file$lines[as.character(parsed$line1)]

around_operator <- substr(line, parsed$col1 - 1L, parsed$col2 + 1L)

non_space_before <- re_matches(around_operator, rex(start, non_space))

newline_after <- unname(nchar(line)) %==% parsed$col2

non_space_after <- re_matches(around_operator, rex(non_space, end))

if (non_space_before || (!newline_after && non_space_after)) {

# we only should check spacing if the operator is infix,
# which only happens if there is more than one sibling
is_infix <-
length(siblings(source_file$parsed_content, parsed$id, 1)) > 1L
is_infix <- length(siblings(source_file$parsed_content, parsed$id, 1)) > 1L

start <- end <- parsed$col1

Expand All @@ -65,13 +62,10 @@ infix_spaces_linter <- function() {
type = "style",
message = "Put spaces around all infix operators.",
line = line,
ranges = list(c(start, end)),
linter = "infix_spaces_linter"
)

ranges = list(c(start, end))
)
}
}

})
})
}
3 changes: 1 addition & 2 deletions R/line_length_linter.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ line_length_linter <- function(length = 80L) {
type = "style",
message = lint_message,
line = source_file$file_lines[long_line],
ranges = list(c(1L, line_lengths[long_line])),
linter = "line_length_linter"
ranges = list(c(1L, line_lengths[long_line]))
)
})
})
Expand Down
Loading