-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathUnneededBreakInSwitchRule.swift
More file actions
142 lines (135 loc) · 4.73 KB
/
UnneededBreakInSwitchRule.swift
File metadata and controls
142 lines (135 loc) · 4.73 KB
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
import SwiftBasicFormat
import SwiftSyntax
private func embedInSwitch(
_ text: String,
case: String = "case .bar",
file: StaticString = #filePath,
line: UInt = #line) -> Example {
Example("""
switch foo {
\(`case`):
\(text)
}
""", file: file, line: line)
}
@SwiftSyntaxRule(explicitRewriter: true)
struct UnneededBreakInSwitchRule: Rule {
var configuration = SeverityConfiguration<Self>(.warning)
static let description = RuleDescription(
identifier: "unneeded_break_in_switch",
name: "Unneeded Break in Switch",
description: "Avoid using unneeded break statements",
kind: .idiomatic,
nonTriggeringExamples: [
embedInSwitch("break"),
embedInSwitch("break", case: "default"),
embedInSwitch("for i in [0, 1, 2] { break }"),
embedInSwitch("if true { break }"),
embedInSwitch("something()"),
Example("""
let items = [Int]()
for item in items {
if bar() {
do {
try foo()
} catch {
bar()
break
}
}
}
"""),
],
triggeringExamples: [
embedInSwitch("something()\n ↓break"),
embedInSwitch("something()\n ↓break // comment"),
embedInSwitch("something()\n ↓break", case: "default"),
embedInSwitch("something()\n ↓break", case: "case .foo, .foo2 where condition"),
],
corrections: [
embedInSwitch("something()\n ↓break")
: embedInSwitch("something()"),
embedInSwitch("something()\n ↓break // line comment")
: embedInSwitch("something()\n // line comment"),
embedInSwitch("""
something()
↓break
/*
block comment
*/
""")
: embedInSwitch("""
something()
/*
block comment
*/
"""),
embedInSwitch("something()\n ↓break /// doc line comment")
: embedInSwitch("something()\n /// doc line comment"),
embedInSwitch("""
something()
↓break
///
/// doc block comment
///
""")
: embedInSwitch("""
something()
///
/// doc block comment
///
"""),
embedInSwitch("something()\n ↓break", case: "default")
: embedInSwitch("something()", case: "default"),
embedInSwitch("something()\n ↓break", case: "case .foo, .foo2 where condition")
: embedInSwitch("something()", case: "case .foo, .foo2 where condition"),
]
)
}
private extension UnneededBreakInSwitchRule {
final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> {
override func visitPost(_ node: SwitchCaseSyntax) {
guard let statement = node.unneededBreak else {
return
}
violations.append(statement.item.positionAfterSkippingLeadingTrivia)
}
}
final class Rewriter: ViolationsSyntaxRewriter<ConfigurationType> {
override func visit(_ node: SwitchCaseSyntax) -> SwitchCaseSyntax {
let stmts = CodeBlockItemListSyntax(node.statements.dropLast())
guard let breakStatement = node.unneededBreak, let secondLast = stmts.last else {
return super.visit(node)
}
numberOfCorrections += 1
let trivia = breakStatement.item.leadingTrivia + breakStatement.item.trailingTrivia
let newNode = node
.with(\.statements, stmts)
.with(\.statements.trailingTrivia, secondLast.item.trailingTrivia + trivia)
.trimmed { !$0.isComment }
.formatted()
.as(SwitchCaseSyntax.self)!
return super.visit(newNode)
}
}
}
private extension SwitchCaseSyntax {
var unneededBreak: CodeBlockItemSyntax? {
guard statements.count > 1,
let breakStatement = statements.last?.item.as(BreakStmtSyntax.self),
breakStatement.label == nil else {
return nil
}
return statements.last
}
}
private extension TriviaPiece {
var isComment: Bool {
switch self {
case .lineComment, .blockComment, .docLineComment, .docBlockComment:
true
default:
false
}
}
}