-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathtest-unreachable_code_linter.R
184 lines (171 loc) · 4.61 KB
/
test-unreachable_code_linter.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
test_that("unreachable_code_linter works in simple function", {
lines <- trim_some("
foo <- function(bar) {
return(bar)
}
")
expect_lint(lines, NULL, unreachable_code_linter())
})
test_that("unreachable_code_linter ignores expressions that aren't functions", {
expect_lint("x + 1", NULL, unreachable_code_linter())
})
test_that("unreachable_code_linter ignores anonymous/inline functions", {
expect_lint("lapply(rnorm(10), function(x) x + 1)", NULL, unreachable_code_linter())
})
test_that("unreachable_code_linter passes on multi-line functions", {
lines <- trim_some("
oo <- function(x) {
y <- x + 1
return(y)
}
")
expect_lint(lines, NULL, unreachable_code_linter())
})
test_that("unreachable_code_linter ignores comments on the same expression", {
lines <- trim_some("
foo <- function(x) {
return(
y^2
) # y^3
}
")
expect_lint(lines, NULL, unreachable_code_linter())
})
test_that("unreachable_code_linter ignores comments on the same line", {
lines <- trim_some("
foo <- function(x) {
return(y^2) # y^3
}
")
expect_lint(lines, NULL, unreachable_code_linter())
})
test_that("unreachable_code_linter identifies simple unreachable code", {
lines <- trim_some("
foo <- function(bar) {
return(bar)
x + 3
}
")
# testing the correct expression is linted (the first culprit line)
expect_lint(
lines,
list(
line_number = 3L,
message = rex::rex("Code and comments coming after a top-level return() or stop()")
),
unreachable_code_linter()
)
})
test_that("unreachable_code_linter finds unreachable comments", {
lines <- trim_some("
foo <- function(x) {
y <- x + 1
return(y^2)
# y^3
}
")
expect_lint(
lines,
rex::rex("Code and comments coming after a top-level return() or stop()"),
unreachable_code_linter()
)
})
test_that("unreachable_code_linter finds a double return", {
lines <- trim_some("
foo <- function(x) {
return(y^2)
return(y^3)
}
")
expect_lint(
lines,
rex::rex("Code and comments coming after a top-level return() or stop()"),
unreachable_code_linter()
)
})
test_that("unreachable_code_linter finds code after stop()", {
lines <- trim_some("
foo <- function(x) {
y <- x + 1
stop(y^2)
# y^3
}
")
expect_lint(
lines,
rex::rex("Code and comments coming after a top-level return() or stop()"),
unreachable_code_linter()
)
})
test_that("unreachable_code_linter ignores code after foo$stop(), which might be stopping a subprocess, for example", {
expect_lint(
trim_some("
foo <- function(x) {
bar <- get_process()
bar$stop()
TRUE
}
"),
NULL,
unreachable_code_linter()
)
expect_lint(
trim_some("
foo <- function(x) {
bar <- get_process()
bar@stop()
TRUE
}
"),
NULL,
unreachable_code_linter()
)
})
test_that("unreachable_code_linter ignores terminal nolint end comments", {
expect_lint(
trim_some("
foo <- function() {
do_something
# nolint start: one_linter.
a = 42
return(a)
# nolint end
}
"),
NULL,
list(unreachable_code_linter(), one_linter = assignment_linter())
)
})
# nolint start: commented_code_linter.
# TODO(michaelchirico): extend to work on switch() statements
# test_that("unreachable_code_linter interacts with switch() as expected", {
# unreachable_inside_switch_lines <- trim_some("
# foo <- function(x) {
# switch(x,
# a = {
# return(x)
# x + 1
# },
# b = {
# return(x + 1)
# }
# )
# }
# ")
# expect_lint(
# unreachable_inside_switch_lines,
# rex::rex("Code and comments coming after a top-level return() or stop()"),
# unreachable_code_linter()
# )
# })
# nolint end: commented_code_linter.
# TODO(michaelchirico): the logic could be extended to terminal if statements
# or control flows (for/while). There shouldn't really be such a thing as
# a terminal for/while (owing to ExplicitReturnLinter forcing these to
# be followed by return(invisible()) or similar), but could be included to
# catch comments for completeness / robustness as a standalone function.
# Terminal if statements are a bit messy, but would have some payoff.
# TODO(michaelchirico): similarly, return(x); x+1 should also lint, even though
# the styler won't allow this in our current setup.
# TODO(michaelchirico): again similarly, this could also apply to cases without
# explicit returns (where it can only apply to comments)