-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add comma_inheritance_rule rule (#3954)
Fixes #3950
- Loading branch information
1 parent
1752587
commit 5f44d3f
Showing
5 changed files
with
130 additions
and
3 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
115 changes: 115 additions & 0 deletions
115
Source/SwiftLintFramework/Rules/Style/CommaInheritanceRule.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,115 @@ | ||
import Foundation | ||
import SourceKittenFramework | ||
import SwiftSyntax | ||
|
||
public struct CommaInheritanceRule: SubstitutionCorrectableRule, ConfigurationProviderRule, AutomaticTestableRule { | ||
public var configuration = SeverityConfiguration(.warning) | ||
|
||
public init() {} | ||
|
||
public static let description = RuleDescription( | ||
identifier: "comma_inheritance", | ||
name: "Comma Inheritance Rule", | ||
description: "Use commas to separate types in inheritance lists", | ||
kind: .style, | ||
nonTriggeringExamples: [ | ||
Example("struct A: Codable, Equatable {}"), | ||
Example("enum B: Codable, Equatable {}"), | ||
Example("class C: Codable, Equatable {}"), | ||
Example("protocol D: Codable, Equatable {}"), | ||
Example("typealias E = Equatable & Codable"), | ||
Example("func foo<T: Equatable & Codable>(_ param: T) {}"), | ||
Example(""" | ||
protocol G { | ||
associatedtype Model: Codable, Equatable | ||
} | ||
""") | ||
], | ||
triggeringExamples: [ | ||
Example("struct A: Codable↓ & Equatable {}"), | ||
Example("struct A: Codable↓ & Equatable {}"), | ||
Example("struct A: Codable↓&Equatable {}"), | ||
Example("struct A: Codable↓& Equatable {}"), | ||
Example("enum B: Codable↓ & Equatable {}"), | ||
Example("class C: Codable↓ & Equatable {}"), | ||
Example("protocol D: Codable↓ & Equatable {}"), | ||
Example(""" | ||
protocol G { | ||
associatedtype Model: Codable↓ & Equatable | ||
} | ||
""") | ||
], | ||
corrections: [ | ||
Example("struct A: Codable↓ & Equatable {}"): Example("struct A: Codable, Equatable {}"), | ||
Example("struct A: Codable↓ & Equatable {}"): Example("struct A: Codable, Equatable {}"), | ||
Example("struct A: Codable↓&Equatable {}"): Example("struct A: Codable, Equatable {}"), | ||
Example("struct A: Codable↓& Equatable {}"): Example("struct A: Codable, Equatable {}"), | ||
Example("enum B: Codable↓ & Equatable {}"): Example("enum B: Codable, Equatable {}"), | ||
Example("class C: Codable↓ & Equatable {}"): Example("class C: Codable, Equatable {}"), | ||
Example("protocol D: Codable↓ & Equatable {}"): Example("protocol D: Codable, Equatable {}"), | ||
Example(""" | ||
protocol G { | ||
associatedtype Model: Codable↓ & Equatable | ||
} | ||
"""): Example(""" | ||
protocol G { | ||
associatedtype Model: Codable, Equatable | ||
} | ||
""") | ||
] | ||
) | ||
|
||
// MARK: - Rule | ||
|
||
public func validate(file: SwiftLintFile) -> [StyleViolation] { | ||
return violationRanges(in: file).map { | ||
StyleViolation(ruleDescription: Self.description, | ||
severity: configuration.severity, | ||
location: Location(file: file, characterOffset: $0.location)) | ||
} | ||
} | ||
|
||
// MARK: - SubstitutionCorrectableRule | ||
|
||
public func substitution(for violationRange: NSRange, in file: SwiftLintFile) -> (NSRange, String)? { | ||
return (violationRange, ", ") | ||
} | ||
|
||
public func violationRanges(in file: SwiftLintFile) -> [NSRange] { | ||
guard let tree = file.syntaxTree else { | ||
return [] | ||
} | ||
|
||
let visitor = CommaInheritanceRuleVisitor() | ||
visitor.walk(tree) | ||
return visitor.violationRanges.compactMap { | ||
file.stringView.byteRangeToNSRange($0) | ||
} | ||
} | ||
} | ||
|
||
private final class CommaInheritanceRuleVisitor: SyntaxVisitor { | ||
private(set) var violationRanges: [ByteRange] = [] | ||
|
||
override func visitPost(_ node: InheritedTypeSyntax) { | ||
for type in node.children { | ||
guard let composition = type.as(CompositionTypeSyntax.self) else { | ||
continue | ||
} | ||
|
||
for ampersand in composition.elements.compactMap(\.ampersand) { | ||
let position: AbsolutePosition | ||
if let previousToken = ampersand.previousToken { | ||
position = previousToken.endPositionBeforeTrailingTrivia | ||
} else { | ||
position = ampersand.position | ||
} | ||
|
||
violationRanges.append(ByteRange( | ||
location: ByteCount(position), | ||
length: ByteCount(ampersand.endPosition.utf8Offset - position.utf8Offset) | ||
)) | ||
} | ||
} | ||
} | ||
} |
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