Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf0c1780

Browse files
authored
Makeempty_count rule correctable (realm#5409)
1 parent62a3dda commitf0c1780

File tree

3 files changed

+151
-12
lines changed

3 files changed

+151
-12
lines changed

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@
107107
that contain one of the patterns.
108108
[kasrababaei](https://github.com/kasrababaei)
109109

110+
* Make`empty_count` auto-correctable.
111+
[KS1019](https://github.com/KS1019/)
112+
110113
####Bug Fixes
111114

112115
* Silence`discarded_notification_center_observer` rule in closures. Furthermore,

‎Source/SwiftLintBuiltInRules/Rules/Performance/EmptyCountRule.swift‎

Lines changed: 116 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import SwiftLintCore
12
import SwiftSyntax
23

34
@SwiftSyntaxRule(foldExpressions:true)
4-
structEmptyCountRule:OptInRule{
5+
structEmptyCountRule:SwiftSyntaxCorrectableRule,OptInRule{
56
varconfiguration=EmptyCountConfiguration()
67

78
staticletdescription=RuleDescription(
@@ -31,31 +32,95 @@ struct EmptyCountRule: OptInRule {
3132
Example("[Int]().↓count == 0b00"),
3233
Example("[Int]().↓count == 0o00"),
3334
Example("↓count == 0")
35+
],
36+
corrections:[
37+
Example("[].↓count == 0"):
38+
Example("[].isEmpty"),
39+
Example("0 == [].↓count"):
40+
Example("[].isEmpty"),
41+
Example("[Int]().↓count == 0"):
42+
Example("[Int]().isEmpty"),
43+
Example("0 == [Int]().↓count"):
44+
Example("[Int]().isEmpty"),
45+
Example("[Int]().↓count==0"):
46+
Example("[Int]().isEmpty"),
47+
Example("[Int]().↓count > 0"):
48+
Example("![Int]().isEmpty"),
49+
Example("[Int]().↓count != 0"):
50+
Example("![Int]().isEmpty"),
51+
Example("[Int]().↓count == 0x0"):
52+
Example("[Int]().isEmpty"),
53+
Example("[Int]().↓count == 0x00_00"):
54+
Example("[Int]().isEmpty"),
55+
Example("[Int]().↓count == 0b00"):
56+
Example("[Int]().isEmpty"),
57+
Example("[Int]().↓count == 0o00"):
58+
Example("[Int]().isEmpty"),
59+
Example("↓count == 0"):
60+
Example("isEmpty"),
61+
Example("↓count == 0 && [Int]().↓count == 0o00"):
62+
Example("isEmpty && [Int]().isEmpty"),
63+
Example("[Int]().count != 3 && [Int]().↓count != 0 || ↓count == 0 && [Int]().count > 2"):
64+
Example("[Int]().count != 3 && ![Int]().isEmpty || isEmpty && [Int]().count > 2")
3465
]
3566
)
67+
68+
func makeRewriter(file:SwiftLintFile)->(someViolationsSyntaxRewriter)?{
69+
Rewriter(
70+
configuration: configuration,
71+
locationConverter: file.locationConverter,
72+
disabledRegions:disabledRegions(file: file)
73+
)
74+
}
3675
}
3776

3877
privateextensionEmptyCountRule{
3978
finalclassVisitor:ViolationsSyntaxVisitor<ConfigurationType>{
40-
privateletoperators:Set=["==","!=",">",">=","<","<="]
41-
4279
overridefunc visitPost(_ node:InfixOperatorExprSyntax){
43-
guardlet operatorNode= node.operator.as(BinaryOperatorExprSyntax.self),
44-
let binaryOperator= operatorNode.operator.binaryOperator,
45-
operators.contains(binaryOperator)else{
80+
guardlet binaryOperator= node.binaryOperator, binaryOperator.isComparisonelse{
4681
return
4782
}
4883

49-
iflet intExpr= node.rightOperand.as(IntegerLiteralExprSyntax.self), intExpr.isZero,
50-
let position= node.leftOperand.countCallPosition(onlyAfterDot: configuration.onlyAfterDot){
84+
iflet(_, position)= node.countNodeAndPosition(onlyAfterDot: configuration.onlyAfterDot){
5185
violations.append(position)
52-
return
5386
}
87+
}
88+
}
5489

55-
iflet intExpr= node.leftOperand.as(IntegerLiteralExprSyntax.self), intExpr.isZero,
56-
let position= node.rightOperand.countCallPosition(onlyAfterDot: configuration.onlyAfterDot){
57-
violations.append(position)
90+
finalclassRewriter:ViolationsSyntaxRewriter{
91+
privateletconfiguration:EmptyCountConfiguration
92+
93+
init(configuration:EmptyCountConfiguration,
94+
locationConverter:SourceLocationConverter,
95+
disabledRegions:[SourceRange]){
96+
self.configuration= configuration
97+
super.init(locationConverter: locationConverter, disabledRegions: disabledRegions)
98+
}
99+
100+
overridefunc visit(_ node:InfixOperatorExprSyntax)->ExprSyntax{
101+
guardlet binaryOperator= node.binaryOperator, binaryOperator.isComparisonelse{
102+
return super.visit(node)
103+
}
104+
105+
iflet(count, position)= node.countNodeAndPosition(onlyAfterDot: configuration.onlyAfterDot){
106+
letnewNode=
107+
iflet count= count.as(MemberAccessExprSyntax.self){
108+
count.with(\.declName.baseName,"isEmpty").trimmed.as(ExprSyntax.self)
109+
}else{
110+
count.as(DeclReferenceExprSyntax.self)?.with(\.baseName,"isEmpty").trimmed.as(ExprSyntax.self)
111+
}
112+
guardlet newNodeelse{return super.visit(node)}
113+
correctionPositions.append(position)
58114
return
115+
if["!=","<",">"].contains(binaryOperator){
116+
newNode.negated
117+
.withTrivia(from: node)
118+
}else{
119+
newNode
120+
.withTrivia(from: node)
121+
}
122+
}else{
123+
return super.visit(node)
59124
}
60125
}
61126
}
@@ -89,3 +154,42 @@ private extension TokenSyntax {
89154
}
90155
}
91156
}
157+
158+
privateextensionExprSyntaxProtocol{
159+
varnegated:ExprSyntax{
160+
ExprSyntax(PrefixOperatorExprSyntax(operator:.prefixOperator("!"), expression:self))
161+
}
162+
}
163+
164+
privateextensionSyntaxProtocol{
165+
func withTrivia(from node:someSyntaxProtocol)->Self{
166+
self
167+
.with(\.leadingTrivia, node.leadingTrivia)
168+
.with(\.trailingTrivia, node.trailingTrivia)
169+
}
170+
}
171+
172+
privateextensionInfixOperatorExprSyntax{
173+
func countNodeAndPosition(onlyAfterDot:Bool)->(ExprSyntax,AbsolutePosition)?{
174+
iflet intExpr= rightOperand.as(IntegerLiteralExprSyntax.self), intExpr.isZero,
175+
let position= leftOperand.countCallPosition(onlyAfterDot: onlyAfterDot){
176+
return(leftOperand, position)
177+
}elseiflet intExpr= leftOperand.as(IntegerLiteralExprSyntax.self), intExpr.isZero,
178+
let position= rightOperand.countCallPosition(onlyAfterDot: onlyAfterDot){
179+
return(rightOperand, position)
180+
}else{
181+
returnnil
182+
}
183+
}
184+
185+
varbinaryOperator:String?{
186+
self.operator.as(BinaryOperatorExprSyntax.self)?.operator.binaryOperator
187+
}
188+
}
189+
190+
privateextensionString{
191+
privatestaticletoperators:Set=["==","!=",">",">=","<","<="]
192+
varisComparison:Bool{
193+
String.operators.contains(self)
194+
}
195+
}

‎Tests/SwiftLintFrameworkTests/EmptyCountRuleTests.swift‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,41 @@ class EmptyCountRuleTests: SwiftLintTestCase {
2525
Example("[Int]().↓count == 0o00\n")
2626
]
2727

28+
letcorrections=[
29+
Example("[].↓count == 0"):
30+
Example("[].isEmpty"),
31+
Example("0 == [].↓count"):
32+
Example("[].isEmpty"),
33+
Example("[Int]().↓count == 0"):
34+
Example("[Int]().isEmpty"),
35+
Example("0 == [Int]().↓count"):
36+
Example("[Int]().isEmpty"),
37+
Example("[Int]().↓count==0"):
38+
Example("[Int]().isEmpty"),
39+
Example("[Int]().↓count > 0"):
40+
Example("![Int]().isEmpty"),
41+
Example("[Int]().↓count != 0"):
42+
Example("![Int]().isEmpty"),
43+
Example("[Int]().↓count == 0x0"):
44+
Example("[Int]().isEmpty"),
45+
Example("[Int]().↓count == 0x00_00"):
46+
Example("[Int]().isEmpty"),
47+
Example("[Int]().↓count == 0b00"):
48+
Example("[Int]().isEmpty"),
49+
Example("[Int]().↓count == 0o00"):
50+
Example("[Int]().isEmpty"),
51+
Example("count == 0"):
52+
Example("count == 0"),
53+
Example("count == 0 && [Int]().↓count == 0o00"):
54+
Example("count == 0 && [Int]().isEmpty"),
55+
Example("[Int]().count != 3 && [Int]().↓count != 0 || count == 0 && [Int]().count > 2"):
56+
Example("[Int]().count != 3 && ![Int]().isEmpty || count == 0 && [Int]().count > 2")
57+
]
58+
2859
letdescription=EmptyCountRule.description
2960
.with(triggeringExamples: triggeringExamples)
3061
.with(nonTriggeringExamples: nonTriggeringExamples)
62+
.with(corrections: corrections)
3163

3264
verifyRule(description, ruleConfiguration:["only_after_dot":true])
3365
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp