-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Add new opt-in non_final_class
rule
#6025
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,51 @@ | ||||||
import SwiftSyntax | ||||||
import SwiftSyntaxBuilder | ||||||
|
||||||
@SwiftSyntaxRule(correctable: true, optIn: true) | ||||||
struct NonFinalClassRule: Rule { | ||||||
var configuration = SeverityConfiguration<Self>(.warning) | ||||||
|
||||||
static let description = RuleDescription( | ||||||
identifier: "non_final_class", | ||||||
name: "Non-Final Class", | ||||||
description: "Classes should be marked as `final` unless they are explicitly `open`.", | ||||||
kind: .idiomatic, | ||||||
nonTriggeringExamples: [ | ||||||
Example("final class MyClass {}"), | ||||||
Example("open class MyClass {}"), | ||||||
Example("public final class MyClass {}"), | ||||||
], | ||||||
triggeringExamples: [ | ||||||
Example("class MyClass {}"), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add violation markers |
||||||
Example("public class MyClass {}"), | ||||||
], | ||||||
corrections: [ | ||||||
Example("class MyClass {}"): Example("final class MyClass {}"), | ||||||
Example("public class MyClass {}"): Example("public final class MyClass {}"), | ||||||
] | ||||||
) | ||||||
|
||||||
func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor<ConfigurationType>? { | ||||||
Visitor(configuration: configuration, file: file) | ||||||
} | ||||||
|
||||||
private final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> { | ||||||
override func visit(_ node: ClassDeclSyntax) -> SyntaxVisitorContinueKind { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer |
||||||
let modifiers = node.modifiers | ||||||
if !modifiers.contains(keyword: .final), | ||||||
!modifiers.contains(keyword: .open) { | ||||||
let classToken = node.classKeyword | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
violations.append(.init( | ||||||
position: classToken.positionAfterSkippingLeadingTrivia, | ||||||
reason: "Classes should be marked as `final` unless they are explicitly `open`", | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could be more succinct and targeted to the single class being checked:
Suggested change
|
||||||
correction: .init( | ||||||
start: classToken.positionAfterSkippingLeadingTrivia, | ||||||
end: classToken.positionAfterSkippingLeadingTrivia, | ||||||
replacement: "final " | ||||||
) | ||||||
)) | ||||||
} | ||||||
return .visitChildren | ||||||
} | ||||||
} | ||||||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test isn't needed. All examples in the description are test cases by design. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
@testable import SwiftLintFramework | ||
import XCTest | ||
|
||
final class NonFinalClassRuleTests: XCTestCase { | ||
func testNonTriggeringExamples() { | ||
let examples = [ | ||
"final class MyClass {}", | ||
"open class MyClass {}", | ||
"public final class MyClass {}", | ||
] | ||
examples.forEach { example in | ||
verifyRule(NonFinalClassRule.description, string: example, expected: []) | ||
} | ||
} | ||
|
||
func testTriggeringExamples() { | ||
let examples = [ | ||
"class MyClass {}", | ||
"public class MyClass {}", | ||
] | ||
examples.forEach { example in | ||
let violations = violationsForRule(NonFinalClassRule.description, string: example) | ||
XCTAssertEqual(violations.count, 1, "Expected one violation in: \(example)") | ||
XCTAssertEqual( | ||
violations.first?.reason, | ||
"Classes should be marked as `final` unless they are explicitly `open`") | ||
} | ||
} | ||
|
||
func testCorrections() { | ||
assertCorrection(NonFinalClassRule.description, | ||
input: "class MyClass {}", | ||
expected: "final class MyClass {}") | ||
assertCorrection(NonFinalClassRule.description, | ||
input: "public class MyClass {}", | ||
expected: "public final class MyClass {}") | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We now support adding rationals to rule descriptions. This feels like a very good candidate for some more explanation, as it seems highly opinionated and project-specific considering that
open
impliespublic
which is often not what libraries want instead.