|
1 | 1 | import Foundation |
2 | | -import SourceKittenFramework |
3 | 2 | import SwiftSyntax |
4 | 3 |
|
5 | | -public struct ExplicitReturnRule: ConfigurationProviderRule, SubstitutionCorrectableRule, OptInRule { |
6 | | - public var configuration = ExplicitReturnConfiguration() |
| 4 | +@SwiftSyntaxRule(correctable: true, optIn: true) |
| 5 | +struct ExplicitReturnRule: Rule { |
| 6 | + var configuration = ExplicitReturnConfiguration() |
7 | 7 |
|
8 | | - public init() {} |
9 | | - |
10 | | - public static let description = RuleDescription( |
| 8 | + static let description = RuleDescription( |
11 | 9 | identifier: "explicit_return", |
12 | 10 | name: "Explicit Return", |
13 | | - description: "Prefer explicit returns in closures, functions and getters.", |
| 11 | + description: "Prefer explicit returns in closures, functions and getters", |
14 | 12 | kind: .style, |
15 | 13 | nonTriggeringExamples: ExplicitReturnRuleExamples.nonTriggeringExamples, |
16 | 14 | triggeringExamples: ExplicitReturnRuleExamples.triggeringExamples, |
17 | 15 | corrections: ExplicitReturnRuleExamples.corrections |
18 | 16 | ) |
19 | | - |
20 | | - public func validate(file: SwiftLintFile) -> [StyleViolation] { |
21 | | - return violations(file: file).map { |
22 | | - StyleViolation( |
23 | | - ruleDescription: Self.description, |
24 | | - severity: configuration.severityConfiguration.severity, |
25 | | - location: Location(file: file, byteOffset: $0) |
26 | | - ) |
27 | | - } |
28 | | - } |
29 | | - |
30 | | - public func violationRanges(in file: SwiftLintFile) -> [NSRange] { |
31 | | - return violations(file: file).compactMap { |
32 | | - file.stringView.byteRangeToNSRange(ByteRange(location: $0, length: 0)) |
33 | | - } |
34 | | - } |
35 | | - |
36 | | - public func substitution(for violationRange: NSRange, in file: SwiftLintFile) -> (NSRange, String)? { |
37 | | - return (violationRange, "return ") |
38 | | - } |
39 | | - |
40 | | - private func violations(file: SwiftLintFile) -> [ByteCount] { |
41 | | - guard let tree = file.syntaxTree else { return [] } |
42 | | - |
43 | | - let visitor = ExplicitReturnVisitor(includedKinds: configuration.includedKinds) |
44 | | - visitor.walk(tree) |
45 | | - |
46 | | - return visitor.positions.map { ByteCount($0.utf8Offset) } |
47 | | - } |
48 | 17 | } |
49 | 18 |
|
50 | | -private final class ExplicitReturnVisitor: SyntaxVisitor { |
51 | | - private let includedKinds: Set<ExplicitReturnConfiguration.ReturnKind> |
52 | | - |
53 | | - private(set) var positions: [AbsolutePosition] = [] |
54 | | - |
55 | | - init(includedKinds: Set<ExplicitReturnConfiguration.ReturnKind>) { |
56 | | - self.includedKinds = includedKinds |
57 | | - } |
58 | | - |
59 | | - override func visitPost(_ node: ClosureExprSyntax) { |
60 | | - guard includedKinds.contains(.closure), |
61 | | - let firstItem = node.statements.first?.item, |
62 | | - node.statements.count == 1 else { return } |
| 19 | +private extension ExplicitReturnRule { |
| 20 | + final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> { |
| 21 | + override var skippableDeclarations: [any DeclSyntaxProtocol.Type] { [ProtocolDeclSyntax.self] } |
63 | 22 |
|
64 | | - if firstItem.isImplicitlyReturnable { |
65 | | - positions.append(firstItem.positionAfterSkippingLeadingTrivia) |
| 23 | + override func visitPost(_ node: AccessorDeclSyntax) { |
| 24 | + if configuration.isKindIncluded(.getter), |
| 25 | + node.accessorSpecifier.tokenKind == .keyword(.get), |
| 26 | + let body = node.body { |
| 27 | + collectViolation(in: body.statements) |
| 28 | + } |
66 | 29 | } |
67 | | - } |
68 | 30 |
|
69 | | - override func visitPost(_ node: FunctionDeclSyntax) { |
70 | | - guard includedKinds.contains(.function), |
71 | | - node.signature.allowsImplicitReturns, |
72 | | - let firstItem = node.body?.statements.first?.item, |
73 | | - node.body?.statements.count == 1 else { return } |
| 31 | + override func visitPost(_ node: ClosureExprSyntax) { |
| 32 | + if configuration.isKindIncluded(.closure) { |
| 33 | + collectViolation(in: node.statements, isInsideClosure: true) |
| 34 | + } |
| 35 | + } |
74 | 36 |
|
75 | | - if firstItem.isImplicitlyReturnable { |
76 | | - positions.append(firstItem.positionAfterSkippingLeadingTrivia) |
| 37 | + override func visitPost(_ node: FunctionDeclSyntax) { |
| 38 | + if configuration.isKindIncluded(.function), |
| 39 | + node.signature.allowsImplicitReturns, |
| 40 | + let body = node.body { |
| 41 | + collectViolation(in: body.statements) |
| 42 | + } |
77 | 43 | } |
78 | | - } |
79 | 44 |
|
80 | | - override func visitPost(_ node: VariableDeclSyntax) { |
81 | | - guard includedKinds.contains(.getter) else { return } |
| 45 | + override func visitPost(_ node: InitializerDeclSyntax) { |
| 46 | + if configuration.isKindIncluded(.initializer), |
| 47 | + node.optionalMark != nil, |
| 48 | + let body = node.body { |
| 49 | + collectViolation(in: body.statements) |
| 50 | + } |
| 51 | + } |
82 | 52 |
|
83 | | - for binding in node.bindings { |
84 | | - if let accessor = binding.accessor?.as(CodeBlockSyntax.self) { |
85 | | - // Shorthand syntax for getters: `var foo: Int { 0 }` |
86 | | - guard let firstItem = accessor.statements.first?.item, |
87 | | - accessor.statements.count == 1 else { continue } |
| 53 | + override func visitPost(_ node: PatternBindingSyntax) { |
| 54 | + if configuration.isKindIncluded(.getter), |
| 55 | + case let .getter(itemList) = node.accessorBlock?.accessors { |
| 56 | + collectViolation(in: itemList) |
| 57 | + } |
| 58 | + } |
88 | 59 |
|
89 | | - if firstItem.isImplicitlyReturnable { |
90 | | - positions.append(firstItem.positionAfterSkippingLeadingTrivia) |
91 | | - } |
92 | | - } else if let accessorBlock = binding.accessor?.as(AccessorBlockSyntax.self) { |
93 | | - // Full syntax for getters: `var foo: Int { get { 0 } }` |
94 | | - guard let accessor = accessorBlock.accessors.first(where: { $0.accessorKind.text == "get" }), |
95 | | - let firstItem = accessor.body?.statements.first?.item, |
96 | | - accessor.body?.statements.count == 1 else { continue } |
| 60 | + override func visitPost(_ node: SubscriptDeclSyntax) { |
| 61 | + if configuration.isKindIncluded(.subscript), |
| 62 | + case let .getter(itemList) = node.accessorBlock?.accessors { |
| 63 | + collectViolation(in: itemList) |
| 64 | + } |
| 65 | + } |
97 | 66 |
|
98 | | - if firstItem.isImplicitlyReturnable { |
99 | | - positions.append(firstItem.positionAfterSkippingLeadingTrivia) |
100 | | - } |
| 67 | + private func collectViolation(in itemList: CodeBlockItemListSyntax, isInsideClosure: Bool = false) { |
| 68 | + guard let onlyItem = itemList.onlyElement?.item, |
| 69 | + !onlyItem.is(ReturnStmtSyntax.self), |
| 70 | + Syntax(onlyItem).isProtocol((any ExprSyntaxProtocol).self) else { |
| 71 | + return |
101 | 72 | } |
| 73 | + if isInsideClosure, Syntax(onlyItem).isFunctionCallExpr { |
| 74 | + return |
| 75 | + } |
| 76 | + let position = onlyItem.positionAfterSkippingLeadingTrivia |
| 77 | + violations.append( |
| 78 | + at: position, |
| 79 | + correction: .init( |
| 80 | + start: position, |
| 81 | + end: position, |
| 82 | + replacement: "return " |
| 83 | + ) |
| 84 | + ) |
102 | 85 | } |
103 | 86 | } |
104 | 87 | } |
105 | 88 |
|
106 | 89 | private extension Syntax { |
107 | | - var isImplicitlyReturnable: Bool { |
108 | | - isProtocol(ExprSyntaxProtocol.self) |
| 90 | + var isFunctionCallExpr: Bool { |
| 91 | + if `is`(FunctionCallExprSyntax.self) { |
| 92 | + return true |
| 93 | + } |
| 94 | + if let tryExpr = `as`(TryExprSyntax.self) { |
| 95 | + return Syntax(tryExpr.expression).isFunctionCallExpr |
| 96 | + } |
| 97 | + if let awaitExpr = `as`(AwaitExprSyntax.self) { |
| 98 | + return Syntax(awaitExpr.expression).isFunctionCallExpr |
| 99 | + } |
| 100 | + return false |
109 | 101 | } |
110 | 102 | } |
111 | 103 |
|
112 | 104 | private extension FunctionSignatureSyntax { |
113 | 105 | var allowsImplicitReturns: Bool { |
114 | | - if let simpleType = output?.returnType.as(SimpleTypeIdentifierSyntax.self) { |
115 | | - return simpleType.name.text != "Void" && simpleType.name.text != "Never" |
116 | | - } else if let tupleType = output?.returnType.as(TupleTypeSyntax.self) { |
| 106 | + guard let returnClause else { return false } |
| 107 | + if let identifierType = returnClause.type.as(IdentifierTypeSyntax.self) { |
| 108 | + return identifierType.name.text != "Void" && identifierType.name.text != "Never" |
| 109 | + } |
| 110 | + if let tupleType = returnClause.type.as(TupleTypeSyntax.self) { |
117 | 111 | return !tupleType.elements.isEmpty |
118 | | - } else { |
119 | | - return output != nil |
120 | 112 | } |
| 113 | + return true |
121 | 114 | } |
122 | 115 | } |
0 commit comments