-
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 new
opaque_over_existential
rule
- Loading branch information
1 parent
ff21ff7
commit 834705b
Showing
8 changed files
with
82 additions
and
16 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
51 changes: 51 additions & 0 deletions
51
Source/SwiftLintBuiltInRules/Rules/Lint/OpaqueOverExistentialParameterRule.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,51 @@ | ||
import SwiftSyntax | ||
|
||
@SwiftSyntaxRule(correctable: true, optIn: true) | ||
struct OpaqueOverExistentialParameterRule: Rule { | ||
var configuration = SeverityConfiguration<Self>(.warning) | ||
|
||
static let description = RuleDescription( | ||
identifier: "opaque_over_existential", | ||
name: "Opaque Over Existential Parameter", | ||
description: "Prefer opaque type over existential type in function parameter", | ||
kind: .lint, | ||
nonTriggeringExamples: [ | ||
Example("func f(_: some P) {}"), | ||
Example("func f(_: some P & Q) {}"), | ||
Example("func f(_: any P.Type) {}"), | ||
], | ||
triggeringExamples: [ | ||
Example("func f(_: ↓any P) {}"), | ||
Example("func f(_: ↓any P & Q) {}"), | ||
], | ||
corrections: [ | ||
Example("func f(_: any P) {}"): | ||
Example("func f(_: some P) {}"), | ||
Example("func f(_: any P & Q) {}"): | ||
Example("func f(_: some P & Q) {}"), | ||
Example("func f(_: /* comment */ any P/* comment*/) {}"): | ||
Example("func f(_: /* comment */ some P/* comment*/) {}"), | ||
] | ||
) | ||
} | ||
|
||
private extension OpaqueOverExistentialParameterRule { | ||
final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> { | ||
override func visitPost(_ node: FunctionParameterSyntax) { | ||
guard let type = node.type.as(SomeOrAnyTypeSyntax.self) else { | ||
return | ||
} | ||
let specifier = type.someOrAnySpecifier | ||
if specifier.tokenKind == .keyword(.any), !type.constraint.is(MetatypeTypeSyntax.self) { | ||
violations.append(.init( | ||
position: specifier.positionAfterSkippingLeadingTrivia, | ||
correction: .init( | ||
start: specifier.positionAfterSkippingLeadingTrivia, | ||
end: specifier.endPositionBeforeTrailingTrivia, | ||
replacement: "some" | ||
) | ||
)) | ||
} | ||
} | ||
} | ||
} |
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
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