Skip to content

Commit

Permalink
- rule added for avoiding redundant extensions resolve realm#5359
Browse files Browse the repository at this point in the history
  • Loading branch information
mzeeshanid committed Dec 11, 2023
1 parent fefc4ce commit 49456c2
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ disabled_rules:
- prefer_nimble
- prefer_self_in_static_references
- prefixed_toplevel_constant
- redundant_extension
- redundant_self_in_closure
- required_deinit
- self_binding
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@

#### Enhancements

* Add new `redundant_extension` rule that detect redundant extensions.
[Muhammad Zeeshan](https://github.com/mzeeshanid)
[#5359](https://github.com/realm/SwiftLint/issues/5359)

* Add new `one_declaration_per_file` rule that allows only a
single class/struct/enum/protocol declaration per file.
Extensions are an exception; more than one is allowed.
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 @@ -166,6 +166,7 @@ public let builtInRules: [any Rule.Type] = [
ReduceBooleanRule.self,
ReduceIntoRule.self,
RedundantDiscardableLetRule.self,
RedundantExtensionRule.self,
RedundantNilCoalescingRule.self,
RedundantObjcAttributeRule.self,
RedundantOptionalInitializationRule.self,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import SwiftSyntax

@SwiftSyntaxRule
struct RedundantExtensionRule: OptInRule {
var configuration = SeverityConfiguration<Self>(.warning)

static let description = RuleDescription(
identifier: "redundant_extension",
name: "Redundant Extension",
description: "Avoid redundant extensions",
kind: .idiomatic,
nonTriggeringExamples: [
Example("""
extension Foo {
func something() {}
}
"""),
Example("""
extension Foo {
var a: Int { 1 }
}
""")
],
triggeringExamples: [
Example("""
↓extension Bar {}
""")
]
)
}

private extension RedundantExtensionRule {
final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> {
private var isRedundantExtension = false
override var skippableDeclarations: [any DeclSyntaxProtocol.Type] {
return .allExcept(VariableDeclSyntax.self, FunctionDeclSyntax.self)
}

override func visitPost(_ node: VariableDeclSyntax) {
isRedundantExtension = false
}

override func visitPost(_ node: FunctionDeclSyntax) {
isRedundantExtension = false
}

override func visit(_ node: ExtensionDeclSyntax) -> SyntaxVisitorContinueKind {
isRedundantExtension = true
return .visitChildren
}

override func visitPost(_ node: ExtensionDeclSyntax) {
appendViolationIfNeeded(node: node.extensionKeyword)
}

func appendViolationIfNeeded(node: TokenSyntax) {
if isRedundantExtension {
violations.append(node.positionAfterSkippingLeadingTrivia)
}
}
}
}
6 changes: 6 additions & 0 deletions Tests/GeneratedTests/GeneratedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,12 @@ class RedundantDiscardableLetRuleGeneratedTests: SwiftLintTestCase {
}
}

class RedundantExtensionRuleGeneratedTests: SwiftLintTestCase {
func testWithDefaultConfiguration() {
verifyRule(RedundantExtensionRule.description)
}
}

class RedundantNilCoalescingRuleGeneratedTests: SwiftLintTestCase {
func testWithDefaultConfiguration() {
verifyRule(RedundantNilCoalescingRule.description)
Expand Down

0 comments on commit 49456c2

Please sign in to comment.