|
| 1 | +import SwiftSyntax |
| 2 | + |
| 3 | +@SwiftSyntaxRule(correctable: true, optIn: true) |
| 4 | +struct OpaqueOverExistentialParameterRule: Rule { |
| 5 | + var configuration = SeverityConfiguration<Self>(.warning) |
| 6 | + |
| 7 | + static let description = RuleDescription( |
| 8 | + identifier: "opaque_over_existential", |
| 9 | + name: "Opaque Over Existential Parameter", |
| 10 | + description: "Prefer opaque type over existential type in function parameter", |
| 11 | + kind: .lint, |
| 12 | + nonTriggeringExamples: [ |
| 13 | + Example("func f(_: some P) {}"), |
| 14 | + Example("func f(_: (some P)?) {}"), |
| 15 | + Example("func f(_: some P & Q) {}"), |
| 16 | + Example("func f(_: any P.Type) {}"), |
| 17 | + Example("func f(_: (any P.Type)?) {}"), |
| 18 | + Example("func f(_: borrowing any ~Copyable.Type) {}"), |
| 19 | + Example("func f(_: () -> Int = { let i: any P = p(); return i.get() }) {}"), |
| 20 | + Example("func f(_: [any P]) {}"), |
| 21 | + Example("func f(_: [any P: any Q]) {}"), |
| 22 | + Example("func f(_: () -> any P) {}"), |
| 23 | + ], |
| 24 | + triggeringExamples: [ |
| 25 | + Example("func f(_: ↓any P) {}"), |
| 26 | + Example("func f(_: (↓any P)?) {}"), |
| 27 | + Example("func f(_: ↓any P & Q) {}"), |
| 28 | + Example("func f(_: borrowing ↓any ~Copyable) {}"), |
| 29 | + Example("func f(_: borrowing (↓any ~Copyable)?) {}"), |
| 30 | + Example("func f(_: (↓any P, ↓any Q)) {}"), |
| 31 | + ], |
| 32 | + corrections: [ |
| 33 | + Example("func f(_: any P) {}"): |
| 34 | + Example("func f(_: some P) {}"), |
| 35 | + Example("func f(_: (any P)?) {}"): |
| 36 | + Example("func f(_: (some P)?) {}"), |
| 37 | + Example("func f(_: any P & Q) {}"): |
| 38 | + Example("func f(_: some P & Q) {}"), |
| 39 | + Example("func f(_: /* comment */ any P/* comment*/) {}"): |
| 40 | + Example("func f(_: /* comment */ some P/* comment*/) {}"), |
| 41 | + Example("func f(_: borrowing (any ~Copyable)?) {}"): |
| 42 | + Example("func f(_: borrowing (some ~Copyable)?) {}"), |
| 43 | + ] |
| 44 | + ) |
| 45 | +} |
| 46 | + |
| 47 | +private extension OpaqueOverExistentialParameterRule { |
| 48 | + final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> { |
| 49 | + override func visitPost(_ node: FunctionParameterSyntax) { |
| 50 | + AnyTypeVisitor(viewMode: .sourceAccurate).walk(tree: node.type, handler: \.anyRanges).forEach { range in |
| 51 | + violations.append(.init( |
| 52 | + position: range.start, |
| 53 | + correction: .init( |
| 54 | + start: range.start, |
| 55 | + end: range.end, |
| 56 | + replacement: "some" |
| 57 | + ) |
| 58 | + )) |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +private class AnyTypeVisitor: SyntaxVisitor { |
| 65 | + var anyRanges = [(start: AbsolutePosition, end: AbsolutePosition)]() |
| 66 | + |
| 67 | + override func visitPost(_ node: SomeOrAnyTypeSyntax) { |
| 68 | + let specifier = node.someOrAnySpecifier |
| 69 | + if specifier.tokenKind == .keyword(.any), !node.constraint.isMetaType { |
| 70 | + anyRanges.append((specifier.positionAfterSkippingLeadingTrivia, specifier.endPositionBeforeTrailingTrivia)) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + override func visit(_: ArrayTypeSyntax) -> SyntaxVisitorContinueKind { |
| 75 | + .skipChildren |
| 76 | + } |
| 77 | + |
| 78 | + override func visit(_: DictionaryTypeSyntax) -> SyntaxVisitorContinueKind { |
| 79 | + .skipChildren |
| 80 | + } |
| 81 | + |
| 82 | + override func visit(_: FunctionTypeSyntax) -> SyntaxVisitorContinueKind { |
| 83 | + .skipChildren |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +private extension TypeSyntax { |
| 88 | + var isMetaType: Bool { |
| 89 | + if `is`(MetatypeTypeSyntax.self) { |
| 90 | + return true |
| 91 | + } |
| 92 | + if let suppressedType = `as`(SuppressedTypeSyntax.self) { |
| 93 | + return suppressedType.type.isMetaType |
| 94 | + } |
| 95 | + return false |
| 96 | + } |
| 97 | +} |
0 commit comments