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

Commit8f032ea

Browse files
committed
Address review comments
1 parent4aa6ada commit8f032ea

File tree

6 files changed

+145
-73
lines changed

6 files changed

+145
-73
lines changed

‎.swiftlint.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ disabled_rules:
2121
-explicit_top_level_acl
2222
-explicit_type_interface
2323
-file_types_order
24-
-final_class
2524
-final_test_case
2625
-force_unwrapping
2726
-function_default_parameter_at_end
@@ -47,6 +46,7 @@ disabled_rules:
4746
-redundant_self_in_closure
4847
-required_deinit
4948
-self_binding
49+
-static_over_final_class
5050
-shorthand_argument
5151
-sorted_enum_cases
5252
-strict_fileprivate

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@
125125
* Make`trailing_closure` correctable.
126126
[KS1019](https://github.com/KS1019/)
127127

128-
* Add new`final_class` rule to prefer`static` over`final class`methods.
128+
* Add new`static_over_final_class` rule to prefer`static` over`final class`declerations.
129129
[phlippieb](https://github.com/phlippieb)
130130
[#5471](https://github.com/realm/SwiftLint/issues/5471)
131131

‎Source/SwiftLintBuiltInRules/Models/BuiltInRules.swift‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ public let builtInRules: [any Rule.Type] = [
7171
FileNameNoSpaceRule.self,
7272
FileNameRule.self,
7373
FileTypesOrderRule.self,
74-
FinalClassRule.self,
7574
FinalTestCaseRule.self,
7675
FirstWhereRule.self,
7776
FlatMapOverMapReduceRule.self,
@@ -191,6 +190,7 @@ public let builtInRules: [any Rule.Type] = [
191190
SortedImportsRule.self,
192191
StatementPositionRule.self,
193192
StaticOperatorRule.self,
193+
StaticOverFinalClassRule.self,
194194
StrictFilePrivateRule.self,
195195
StrongIBOutletRule.self,
196196
SuperfluousElseRule.self,

‎Source/SwiftLintBuiltInRules/Rules/Idiomatic/FinalClassRule.swift‎

Lines changed: 0 additions & 64 deletions
This file was deleted.
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import SwiftSyntax
2+
3+
@SwiftSyntaxRule
4+
structStaticOverFinalClassRule:Rule{
5+
varconfiguration=SeverityConfiguration<Self>(.warning)
6+
7+
staticletdescription=RuleDescription(
8+
identifier:"static_over_final_class",
9+
name:"Static Over Final Class",
10+
description:"Prefer `static` over `final class` for non-overridable declarations",
11+
kind:.idiomatic,
12+
nonTriggeringExamples:[
13+
Example("""
14+
class C {
15+
static func f() {}
16+
}
17+
"""),
18+
Example("""
19+
class C {
20+
static var i: Int { 0 }
21+
}
22+
"""),
23+
Example("""
24+
class C {
25+
static subscript(_: Int) -> Int { 0 }
26+
}
27+
"""),
28+
Example("""
29+
class C {
30+
class func f() {}
31+
}
32+
"""),
33+
Example("""
34+
final class C {}
35+
"""),
36+
Example("""
37+
final class C {
38+
class D {
39+
class func f() {}
40+
}
41+
}
42+
""")
43+
],
44+
triggeringExamples:[
45+
Example("""
46+
class C {
47+
↓final class func f() {}
48+
}
49+
"""),
50+
Example("""
51+
class C {
52+
↓final class var i: Int { 0 }
53+
}
54+
"""),
55+
Example("""
56+
class C {
57+
↓final class subscript(_: Int) -> Int { 0 }
58+
}
59+
"""),
60+
Example("""
61+
final class C {
62+
↓class func f() {}
63+
}
64+
"""),
65+
Example("""
66+
class C {
67+
final class D {
68+
↓class func f() {}
69+
}
70+
}
71+
""")
72+
]
73+
)
74+
}
75+
76+
// Stack of flags indicating whether each level in the tree is a final class
77+
privatetypealiasLevelIsFinalClassStack=Stack<Bool>
78+
79+
privateextensionLevelIsFinalClassStack{
80+
varlastIsFinalClass:Bool{peek()==true}
81+
}
82+
83+
privateextensionStaticOverFinalClassRule{
84+
finalclassVisitor:ViolationsSyntaxVisitor<ConfigurationType>{
85+
privatevarlevels=LevelIsFinalClassStack()
86+
87+
overridefunc visit(_ node:ClassDeclSyntax)->SyntaxVisitorContinueKind{
88+
levels.push(node.modifiers.contains(keyword:.final))
89+
return.visitChildren
90+
}
91+
92+
overridefunc visitPost(_ node:ClassDeclSyntax){
93+
levels.pop()
94+
}
95+
96+
overridefunc visit(_ node:FunctionDeclSyntax)->SyntaxVisitorContinueKind{
97+
validateNode(at: node.positionAfterSkippingLeadingTrivia, with: node.modifiers)
98+
return.visitChildren
99+
}
100+
101+
overridefunc visitPost(_ node:FunctionDeclSyntax){
102+
levels.pop()
103+
}
104+
105+
overridefunc visit(_ node:VariableDeclSyntax)->SyntaxVisitorContinueKind{
106+
validateNode(at: node.positionAfterSkippingLeadingTrivia, with: node.modifiers)
107+
return.visitChildren
108+
}
109+
110+
overridefunc visitPost(_ node:VariableDeclSyntax){
111+
levels.pop()
112+
}
113+
114+
overridefunc visit(_ node:SubscriptDeclSyntax)->SyntaxVisitorContinueKind{
115+
validateNode(at: node.positionAfterSkippingLeadingTrivia, with: node.modifiers)
116+
return.visitChildren
117+
}
118+
119+
overridefunc visitPost(_ node:SubscriptDeclSyntax){
120+
levels.pop()
121+
}
122+
123+
// MARK: -
124+
privatefunc validateNode(at position:AbsolutePosition, with modifiers:DeclModifierListSyntax){
125+
if modifiers.contains(keyword:.final),
126+
modifiers.contains(keyword:.class){
127+
violations.append(position)
128+
}elseif modifiers.contains(keyword:.class),
129+
levels.lastIsFinalClass{
130+
violations.append(position)
131+
}
132+
133+
levels.push(false)
134+
}
135+
}
136+
}

‎Tests/GeneratedTests/GeneratedTests.swift‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -415,12 +415,6 @@ class FileTypesOrderRuleGeneratedTests: SwiftLintTestCase {
415415
}
416416
}
417417

418-
classFinalClassRuleGeneratedTests:SwiftLintTestCase{
419-
func testWithDefaultConfiguration(){
420-
verifyRule(FinalClassRule.description)
421-
}
422-
}
423-
424418
classFinalTestCaseRuleGeneratedTests:SwiftLintTestCase{
425419
func testWithDefaultConfiguration(){
426420
verifyRule(FinalTestCaseRule.description)
@@ -1135,6 +1129,12 @@ class StaticOperatorRuleGeneratedTests: SwiftLintTestCase {
11351129
}
11361130
}
11371131

1132+
classStaticOverFinalClassRuleGeneratedTests:SwiftLintTestCase{
1133+
func testWithDefaultConfiguration(){
1134+
verifyRule(StaticOverFinalClassRule.description)
1135+
}
1136+
}
1137+
11381138
classStrictFilePrivateRuleGeneratedTests:SwiftLintTestCase{
11391139
func testWithDefaultConfiguration(){
11401140
verifyRule(StrictFilePrivateRule.description)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp