# will produce lints
lint(
text = "if (x == 'a') 1 else if (x == 'b') 2 else 3",
linters = if_switch_linter()
)
code <- paste(
"if (x == 'a') {",
" 1",
"} else if (x == 'b') {",
" 2",
"} else if (x == 'c') {",
" y <- x",
" z <- sqrt(match(y, letters))",
" z",
"}",
sep = "\n"
)
writeLines(code)
lint(
text = code,
linters = if_switch_linter()
)
code <- paste(
"if (x == 'a') {",
" 1",
"} else if (x == 'b') {",
" 2",
"} else if (x == 'c') {",
" y <- x",
" z <- sqrt(",
" match(y, letters)",
" )",
" z",
"}",
sep = "\n"
)
writeLines(code)
lint(
text = code,
linters = if_switch_linter()
)
code <- paste(
"switch(x,",
" a = {",
" 1",
" 2",
" 3",
" },",
" b = {",
" 1",
" 2",
" }",
")",
sep = "\n"
)
writeLines(code)
lint(
text = code,
linters = if_switch_linter(max_branch_lines = 2L)
)
# okay
lint(
text = "switch(x, a = 1, b = 2, 3)",
linters = if_switch_linter()
)
# switch() version not as clear
lint(
text = "if (x == 'a') 1 else if (x == 'b' & y == 2) 2 else 3",
linters = if_switch_linter()
)
code <- paste(
"if (x == 'a') {",
" 1",
"} else if (x == 'b') {",
" 2",
"} else if (x == 'c') {",
" y <- x",
" z <- sqrt(match(y, letters))",
" z",
"}",
sep = "\n"
)
writeLines(code)
lint(
text = code,
linters = if_switch_linter(max_branch_lines = 2L)
)
code <- paste(
"if (x == 'a') {",
" 1",
"} else if (x == 'b') {",
" 2",
"} else if (x == 'c') {",
" y <- x",
" z <- sqrt(",
" match(y, letters)",
" )",
" z",
"}",
sep = "\n"
)
writeLines(code)
lint(
text = code,
linters = if_switch_linter(max_branch_expressions = 2L)
)
code <- paste(
"switch(x,",
" a = {",
" 1",
" 2",
" 3",
" },",
" b = {",
" 1",
" 2",
" }",
")",
sep = "\n"
)
writeLines(code)
lint(
text = code,
linters = if_switch_linter(max_branch_lines = 3L)
)
Run the code above in your browser using DataLab