Skip to content

Commit db83181

Browse files
add name attribute to Linter class (#753)
* add name attribute to Linter class fixes #746 * fix test failures * document() * restore 100% coverage for utils.R * deprecate Lint(linter = ...) and remove all calling instances make expect_lint() resilient to complete removal of the argument * add NEWS bullet * document() * fix lint, collapse with space fix test expectation Co-authored-by: Michael Chirico <michaelchirico4@gmail.com>
1 parent 3f6ccc6 commit db83181

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+293
-293
lines changed

NEWS.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
* `paren_brace_linter` now marks lints at the opening brace instead of the closing parenthesis, making fixing the lints
3838
by jumping to source markers easier (#583, @AshesITR)
3939
* Lints are now marked with the name of the `linter` that caused them instead of the name of their implementation
40-
function (#664, #673, @AshesITR).
40+
function.
41+
Deprecated the obsolete `linter` argument of `Lint()`. (#664, #673, #746, @AshesITR)
4142
* New syntax to exclude only selected linters from linting lines or passages. Use `# nolint: linter_name, linter2_name.`
4243
or `# nolint start: linter_name, linter2_name.` in source files or named lists of line numbers in `.lintr`.
4344
(#660, @AshesITR)

R/T_and_F_symbol_linter.R

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ T_and_F_symbol_linter <- function() { # nolint: object_name_linter.
2121
type = "style",
2222
message = sprintf("Use %s instead of the symbol %s.", replacement, symbol),
2323
line = source_file[["lines"]][[as.character(line_num)]],
24-
ranges = list(c(start_col_num, end_col_num)),
25-
linter = "T_and_F_symbol_linter"
24+
ranges = list(c(start_col_num, end_col_num))
2625
)
2726
}
2827
}

R/assignment_linter.R

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
#' @export
33
assignment_linter <- function() {
44
Linter(function(source_file) {
5-
lapply(ids_with_token(source_file, "EQ_ASSIGN"),
5+
lapply(
6+
ids_with_token(source_file, "EQ_ASSIGN"),
67
function(id) {
78
parsed <- with_id(source_file, id)
89
Lint(
@@ -11,9 +12,8 @@ assignment_linter <- function() {
1112
column_number = parsed$col1,
1213
type = "style",
1314
message = "Use <-, not =, for assignment.",
14-
line = source_file$lines[as.character(parsed$line1)],
15-
linter = "assignment_linter"
16-
)
15+
line = source_file$lines[as.character(parsed$line1)]
16+
)
1717
})
18-
})
18+
})
1919
}

R/assignment_spaces_linter.R

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ assignment_spaces <- function() {
2424
column_number = parsed$col1,
2525
type = "style",
2626
message = "Assignments should only have one space before and after the operator.",
27-
line = source_file$lines[parsed$line1],
28-
linter = "assignment_spaces"
27+
line = source_file$lines[parsed$line1]
2928
)
3029
}
3130
}

R/backport_linter.R

+1-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ backport_linter <- function(r_version = getRversion()) {
4040
all_names[ii], names(needs_backport_names)[which(needs_backport[ii, ])], r_version
4141
),
4242
line = source_file$lines[[line1]],
43-
ranges = list(c(col1, col2)),
44-
linter = "backport_linter"
43+
ranges = list(c(col1, col2))
4544
)
4645
})
4746
})

R/closed_curly_linter.R

+1-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ closed_curly_linter <- function(allow_single_line = FALSE) {
5858
"Closing curly-braces should always be on their own line,",
5959
"unless they are followed by an else."
6060
),
61-
line = source_file$lines[as.character(parsed$line1)],
62-
linter = "closed_curly_linter"
61+
line = source_file$lines[as.character(parsed$line1)]
6362
)}
6463
}
6564
)

R/commas_linter.R

+17-22
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,15 @@ commas_linter <- function() {
5959
!empty_comma &&
6060
!is_blank_switch) {
6161

62-
lints[[length(lints) + 1L]] <-
63-
Lint(
64-
filename = source_file$filename,
65-
line_number = line_number,
66-
column_number = comma_loc,
67-
type = "style",
68-
message = "Commas should never have a space before.",
69-
line = line,
70-
ranges = list(c(start, end)),
71-
"commas_linter"
72-
)
62+
lints[[length(lints) + 1L]] <- Lint(
63+
filename = source_file$filename,
64+
line_number = line_number,
65+
column_number = comma_loc,
66+
type = "style",
67+
message = "Commas should never have a space before.",
68+
line = line,
69+
ranges = list(c(start, end))
70+
)
7371
}
7472
}
7573

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

8583
if (has_token) {
86-
87-
lints[[length(lints) + 1L]] <-
88-
Lint(
89-
filename = source_file$filename,
90-
line_number = line_number,
91-
column_number = comma_loc + 1,
92-
type = "style",
93-
message = "Commas should always have a space after.",
94-
line = line,
95-
linter = "commas_linter"
96-
)
84+
lints[[length(lints) + 1L]] <- Lint(
85+
filename = source_file$filename,
86+
line_number = line_number,
87+
column_number = comma_loc + 1,
88+
type = "style",
89+
message = "Commas should always have a space after.",
90+
line = line
91+
)
9792
}
9893

9994
}

R/comment_linters.R

+1-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ commented_code_linter <- function() {
6161
type = "style",
6262
message = "Commented code should be removed.",
6363
line = source_file$file_lines[line_number],
64-
linter = "commented_code_linter",
6564
ranges = list(column_offset + c(code_candidates[code_candidate, "code.start"],
6665
code_candidates[code_candidate, "code.end"]))
6766
)
@@ -95,8 +94,7 @@ todo_comment_linter <- function(todo = c("todo", "fixme")) {
9594
type = "style",
9695
message = "TODO comments should be removed.",
9796
line = source_file[["lines"]][[as.character(token[["line1"]])]],
98-
ranges = list(c(token[["col1"]], token[["col2"]])),
99-
linter = "todo_comment_linter"
97+
ranges = list(c(token[["col1"]], token[["col2"]]))
10098
)
10199
}
102100
)

R/cyclocomp_linter.R

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ cyclocomp_linter <- function(complexity_limit = 15L) {
2525
complexity_limit, ", this has ", complexity, "."
2626
),
2727
ranges = list(c(source_file[["column"]][1], source_file[["column"]][1])),
28-
line = source_file$lines[1],
29-
linter = "cyclocomp_linter"
28+
line = source_file$lines[1]
3029
)
3130
})
3231
}

R/deprecated.R

+8-9
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ absolute_paths_linter <- function(source_file) {
3535
absolute_path_linter(lax = TRUE)(source_file)
3636
}
3737
class(absolute_paths_linter) <- "linter"
38+
attr(absolute_paths_linter, "name") <- "absolute_paths_linter"
3839

3940

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

4850

4951
#' @describeIn lintr-deprecated Check that objects are not in camelCase.
@@ -59,10 +61,9 @@ camel_case_linter <- make_object_linter(function(source_file, parsed) {
5961
!is_base_function(parsed$text)) {
6062
object_lint(source_file,
6163
parsed,
62-
"Variable and function names should be all lowercase.",
63-
"camel_case_linter")
64+
"Variable and function names should be all lowercase.")
6465
}
65-
})
66+
}, name = "camel_case_linter")
6667

6768

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

8686

8787
#' @describeIn lintr-deprecated check that objects do not have.multiple.dots.
@@ -96,7 +96,6 @@ multiple_dots_linter <- make_object_linter(function(source_file, parsed) {
9696
!is_base_function(parsed$text)) {
9797
object_lint(source_file,
9898
parsed,
99-
"Words within variable and function names should be separated by '_' rather than '.'.",
100-
"multiple_dots_linter")
99+
"Words within variable and function names should be separated by '_' rather than '.'.")
101100
}
102-
})
101+
}, name = "multiple_dots_linter")

R/equals_na_linter.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ equals_na_linter <- function() {
1919

2020
lapply(bad_expr, xml_nodes_to_lint, source_file,
2121
message = "Use is.na for comparisons to NA (not == or !=)",
22-
linter = "equals_na_linter", type = "warning")
22+
type = "warning")
2323
})
2424
}

R/expect_lint.R

+3-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ expect_lint <- function(content, checks, ..., file = NULL, language = "en") {
7070
}
7171

7272
local({
73-
itr <- 0L #nolint
74-
lint_fields <- names(formals(Lint))
73+
itr <- 0L
74+
# keep 'linter' as a field even if we remove the deprecated argument from Lint() in the future
75+
lint_fields <- unique(c(names(formals(Lint)), "linter"))
7576
Map(function(lint, check) {
7677
itr <<- itr + 1L
7778
lapply(names(check), function(field) {

R/extraction_operator_linter.R

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
#' @export
44
extraction_operator_linter <- function() {
55
Linter(function(source_file) {
6-
tokens <- source_file[["parsed_content"]] <-
7-
filter_out_token_type(source_file[["parsed_content"]], "expr")
6+
tokens <- source_file[["parsed_content"]] <- filter_out_token_type(source_file[["parsed_content"]], "expr")
7+
88
lapply(
99
ids_with_token(source_file, c("'$'", "'['"), fun = `%in%`),
1010
function(token_num) {
@@ -14,14 +14,14 @@ extraction_operator_linter <- function() {
1414
end_col_num <- token[["col2"]]
1515
line_num <- token[["line1"]]
1616
line <- source_file[["lines"]][[as.character(line_num)]]
17+
1718
Lint(
1819
filename = source_file[["filename"]],
1920
line_number = line_num,
2021
column_number = start_col_num,
2122
type = "warning",
2223
message = sprintf("Use `[[` instead of `%s` to extract an element.", token[["text"]]),
2324
line = line,
24-
linter = "extraction_operator_linter",
2525
ranges = list(c(start_col_num, end_col_num))
2626
)
2727
}

R/function_left_parentheses.R

+3-4
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,11 @@ function_left_parentheses_linter <- function() { # nolint: object_length_linter.
3434
column_number = parsed$col1,
3535
type = "style",
3636
message = "Remove spaces before the left parenthesis in a function call.",
37-
line = line,
38-
linter = "function_left_parentheses_linter"
37+
line = line
3938
)
4039
}
4140
}
42-
43-
})
41+
}
42+
)
4443
})
4544
}

R/get_source_expressions.R

+4-8
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ get_source_expressions <- function(filename) {
106106
column_number = 1,
107107
type = "error",
108108
message = e$message,
109-
line = "",
110-
linter = "error"
109+
line = ""
111110
)
112111
)
113112
# nocov end
@@ -123,8 +122,7 @@ get_source_expressions <- function(filename) {
123122
column_number = column_number,
124123
type = "error",
125124
message = e$message,
126-
line = source_file$lines[[line_number]],
127-
linter = "error"
125+
line = source_file$lines[[line_number]]
128126
)
129127
)
130128
}
@@ -148,8 +146,7 @@ get_source_expressions <- function(filename) {
148146
column_number = column_number,
149147
type = "error",
150148
message = message_info$message,
151-
line = line,
152-
linter = "error"
149+
line = line
153150
)
154151
}
155152

@@ -178,8 +175,7 @@ get_source_expressions <- function(filename) {
178175
column_number = column_number,
179176
type = "error",
180177
message = message_info$message,
181-
line = source_file$lines[line_number],
182-
linter = "error"
178+
line = source_file$lines[line_number]
183179
)
184180
}
185181

R/implicit_integer_linter.R

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ implicit_integer_linter <- function() {
1919
message =
2020
"Integers should not be implicit. Use the form 1L for integers or 1.0 for doubles.",
2121
line = source_file[["lines"]][[as.character(line_num)]],
22-
ranges = list(c(start_col_num, end_col_num)),
23-
linter = "implicit_integer_linter"
22+
ranges = list(c(start_col_num, end_col_num))
2423
)
2524
}
2625
}

R/infix_spaces_linter.R

+6-12
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,29 @@ infix_tokens <- c(
2121
"'*'", # * : unary multiplication
2222

2323
NULL
24-
)
24+
)
2525

2626
#' @describeIn linters Check that infix operators are surrounded by spaces.
2727
#' @export
2828
infix_spaces_linter <- function() {
2929
Linter(function(source_file) {
30-
lapply(ids_with_token(source_file, infix_tokens, fun = `%in%`),
30+
lapply(
31+
ids_with_token(source_file, infix_tokens, fun = `%in%`),
3132
function(id) {
3233
parsed <- with_id(source_file, id)
3334

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

3637
around_operator <- substr(line, parsed$col1 - 1L, parsed$col2 + 1L)
37-
3838
non_space_before <- re_matches(around_operator, rex(start, non_space))
39-
4039
newline_after <- unname(nchar(line)) %==% parsed$col2
41-
4240
non_space_after <- re_matches(around_operator, rex(non_space, end))
4341

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

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

5148
start <- end <- parsed$col1
5249

@@ -65,13 +62,10 @@ infix_spaces_linter <- function() {
6562
type = "style",
6663
message = "Put spaces around all infix operators.",
6764
line = line,
68-
ranges = list(c(start, end)),
69-
linter = "infix_spaces_linter"
70-
)
71-
65+
ranges = list(c(start, end))
66+
)
7267
}
7368
}
74-
7569
})
7670
})
7771
}

R/line_length_linter.R

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ line_length_linter <- function(length = 80L) {
2020
type = "style",
2121
message = lint_message,
2222
line = source_file$file_lines[long_line],
23-
ranges = list(c(1L, line_lengths[long_line])),
24-
linter = "line_length_linter"
23+
ranges = list(c(1L, line_lengths[long_line]))
2524
)
2625
})
2726
})

0 commit comments

Comments
 (0)