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

Commit4aa6ada

Browse files
committed
Add newfinal_class rule
1 parentd9c6888 commit4aa6ada

File tree

5 files changed

+76
-0
lines changed

5 files changed

+76
-0
lines changed

‎.swiftlint.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ disabled_rules:
2121
-explicit_top_level_acl
2222
-explicit_type_interface
2323
-file_types_order
24+
-final_class
2425
-final_test_case
2526
-force_unwrapping
2627
-function_default_parameter_at_end

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@
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.
129+
[phlippieb](https://github.com/phlippieb)
130+
[#5471](https://github.com/realm/SwiftLint/issues/5471)
131+
128132
####Bug Fixes
129133

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

‎Source/SwiftLintBuiltInRules/Models/BuiltInRules.swift‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public let builtInRules: [any Rule.Type] = [
7171
FileNameNoSpaceRule.self,
7272
FileNameRule.self,
7373
FileTypesOrderRule.self,
74+
FinalClassRule.self,
7475
FinalTestCaseRule.self,
7576
FirstWhereRule.self,
7677
FlatMapOverMapReduceRule.self,
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import SwiftSyntax
2+
3+
@SwiftSyntaxRule
4+
structFinalClassRule:Rule{
5+
varconfiguration=SeverityConfiguration<Self>(.warning)
6+
7+
staticletdescription=RuleDescription(
8+
identifier:"final_class",
9+
name:"Final Class",
10+
description:"Prefer `static` over `final class`",
11+
kind:.idiomatic,
12+
nonTriggeringExamples:[
13+
Example("""
14+
class C {
15+
static func f() {}
16+
}
17+
"""),
18+
Example("""
19+
class C {
20+
class func f() {}
21+
}
22+
"""),
23+
Example("""
24+
final class C {}
25+
""")
26+
],
27+
triggeringExamples:[
28+
Example("""
29+
class C {
30+
final class func f() {}
31+
}
32+
"""),
33+
Example("""
34+
final class C {
35+
class func f() {}
36+
}
37+
""")
38+
]
39+
)
40+
}
41+
42+
privateextensionFinalClassRule{
43+
finalclassVisitor:ViolationsSyntaxVisitor<ConfigurationType>{
44+
overridefunc visit(_ node:FunctionDeclSyntax)->SyntaxVisitorContinueKind{
45+
if node.modifiers.contains(keyword:.final),
46+
node.modifiers.contains(keyword:.class){
47+
violations.append(node.positionAfterSkippingLeadingTrivia)
48+
}
49+
50+
return.visitChildren
51+
}
52+
53+
overridefunc visit(_ node:ClassDeclSyntax)->SyntaxVisitorContinueKind{
54+
if node.modifiers.contains(keyword:.final){
55+
node.memberBlock.members
56+
.compactMap{ $0.decl.as(FunctionDeclSyntax.self)}
57+
.filter{ $0.modifiers.contains(keyword:.class)}
58+
.forEach{ violations.append($0.positionAfterSkippingLeadingTrivia)}
59+
}
60+
61+
return.visitChildren
62+
}
63+
}
64+
}

‎Tests/GeneratedTests/GeneratedTests.swift‎

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

418+
classFinalClassRuleGeneratedTests:SwiftLintTestCase{
419+
func testWithDefaultConfiguration(){
420+
verifyRule(FinalClassRule.description)
421+
}
422+
}
423+
418424
classFinalTestCaseRuleGeneratedTests:SwiftLintTestCase{
419425
func testWithDefaultConfiguration(){
420426
verifyRule(FinalTestCaseRule.description)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp