Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
[kapitoshka438](https://github.com/kapitoshka438)
[#3723](https://github.com/realm/SwiftLint/issues/3723)

* Add new `non_final_class` opt-in rule that triggers when a class isn't `final`.
[JaviSoto](https://github.com/JaviSoto)

### Bug Fixes

* Fix issue referencing the Tests package from another Bazel workspace.
Expand Down
1 change: 1 addition & 0 deletions Source/SwiftLintBuiltInRules/Models/BuiltInRules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public let builtInRules: [any Rule.Type] = [
NoGroupingExtensionRule.self,
NoMagicNumbersRule.self,
NoSpaceInMethodCallRule.self,
NonFinalClassRule.self,
NonOptionalStringDataConversionRule.self,
NonOverridableClassDeclarationRule.self,
NotificationCenterDetachmentRule.self,
Expand Down
51 changes: 51 additions & 0 deletions Source/SwiftLintBuiltInRules/Rules/Style/NonFinalClassRule.swift
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(
Copy link
Collaborator

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 implies public which is often not what libraries want instead.

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 {}"),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add violation markers to indicate the exact positions where the violations are expected to appear.

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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer visitPost instead.

let modifiers = node.modifiers
if !modifiers.contains(keyword: .final),
!modifiers.contains(keyword: .open) {
let classToken = node.classKeyword
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let classToken = node.classKeyword
let classTokenPosition = node.classKeyword.positionAfterSkippingLeadingTrivia

violations.append(.init(
position: classToken.positionAfterSkippingLeadingTrivia,
reason: "Classes should be marked as `final` unless they are explicitly `open`",
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
reason: "Classes should be marked as `final` unless they are explicitly `open`",
reason: "Class should be marked as `final` or explicitly `open`",

correction: .init(
start: classToken.positionAfterSkippingLeadingTrivia,
end: classToken.positionAfterSkippingLeadingTrivia,
replacement: "final "
)
))
}
return .visitChildren
}
}
}
6 changes: 6 additions & 0 deletions Tests/GeneratedTests/GeneratedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,12 @@ final class NoSpaceInMethodCallRuleGeneratedTests: SwiftLintTestCase {
}
}

final class NonFinalClassRuleGeneratedTests: SwiftLintTestCase {
func testWithDefaultConfiguration() {
verifyRule(NonFinalClassRule.description)
}
}

final class NonOptionalStringDataConversionRuleGeneratedTests: SwiftLintTestCase {
func testWithDefaultConfiguration() {
verifyRule(NonOptionalStringDataConversionRule.description)
Expand Down
4 changes: 4 additions & 0 deletions Tests/IntegrationTests/default_rule_configurations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,10 @@ no_space_in_method_call:
severity: warning
meta:
opt-in: false
non_final_class:
severity: warning
meta:
opt-in: true
non_optional_string_data_conversion:
severity: warning
meta:
Expand Down
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 {}")
}
}
Loading