forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- rule added for avoiding redundant extensions resolve realm#5359
- Loading branch information
1 parent
fefc4ce
commit 49456c2
Showing
5 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
Source/SwiftLintBuiltInRules/Rules/Idiomatic/RedundantExtensionRule.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters