|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import Foundation |
| 14 | +import SwiftSyntax |
| 15 | +import SwiftParser |
| 16 | + |
| 17 | +/// Never use `[<Type>]()` syntax. In call sites that should be replaced with `[]`, |
| 18 | +/// for initializations use explicit type combined with empty array literal `let _: [<Type>] = []` |
| 19 | +/// Static properties of a type that return that type should not include a reference to their type. |
| 20 | +/// |
| 21 | +/// Lint: Non-literal empty array initialization will yield a lint error. |
| 22 | +/// Format: All invalid use sites would be related with empty literal (with or without explicit type annotation). |
| 23 | +@_spi(Rules) |
| 24 | +public final class AlwaysUseLiteralForEmptyArrayInit : SyntaxFormatRule { |
| 25 | + public override func visit(_ node: PatternBindingSyntax) -> PatternBindingSyntax { |
| 26 | + guard let initializer = node.initializer else { |
| 27 | + return node |
| 28 | + } |
| 29 | + |
| 30 | + // Check whether the initializer is `[<Type>]()` |
| 31 | + guard let initCall = initializer.value.as(FunctionCallExprSyntax.self), |
| 32 | + var arrayLiteral = initCall.calledExpression.as(ArrayExprSyntax.self), |
| 33 | + initCall.arguments.isEmpty else { |
| 34 | + return node |
| 35 | + } |
| 36 | + |
| 37 | + guard let elementType = getElementType(arrayLiteral) else { |
| 38 | + return node |
| 39 | + } |
| 40 | + |
| 41 | + var replacement = node |
| 42 | + |
| 43 | + var withFixIt = "[]" |
| 44 | + if replacement.typeAnnotation == nil { |
| 45 | + withFixIt = ": [\(elementType)] = []" |
| 46 | + } |
| 47 | + |
| 48 | + diagnose(.refactorEmptyArrayInit(replace: "\(initCall)", with: withFixIt), on: initCall) |
| 49 | + |
| 50 | + if replacement.typeAnnotation == nil { |
| 51 | + // Drop trailing trivia after pattern because ':' has to appear connected to it. |
| 52 | + replacement.pattern = node.pattern.with(\.trailingTrivia, []) |
| 53 | + // Add explicit type annotiation: ': [<Type>]` |
| 54 | + replacement.typeAnnotation = .init(type: ArrayTypeSyntax(leadingTrivia: .space, |
| 55 | + element: elementType, |
| 56 | + trailingTrivia: .space)) |
| 57 | + } |
| 58 | + |
| 59 | + // Replace initializer call with empty array literal: `[<Type>]()` -> `[]` |
| 60 | + arrayLiteral.elements = ArrayElementListSyntax.init([]) |
| 61 | + replacement.initializer = initializer.with(\.value, ExprSyntax(arrayLiteral)) |
| 62 | + |
| 63 | + return replacement |
| 64 | + } |
| 65 | + |
| 66 | + private func getElementType(_ arrayLiteral: ArrayExprSyntax) -> TypeSyntax? { |
| 67 | + guard let elementExpr = arrayLiteral.elements.firstAndOnly?.as(ArrayElementSyntax.self) else { |
| 68 | + return nil |
| 69 | + } |
| 70 | + |
| 71 | + var parser = Parser(elementExpr.description) |
| 72 | + let elementType = TypeSyntax.parse(from: &parser) |
| 73 | + return elementType.hasError ? nil : elementType |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +extension Finding.Message { |
| 78 | + public static func refactorEmptyArrayInit(replace: String, with: String) -> Finding.Message { |
| 79 | + "replace '\(replace)' with '\(with)'" |
| 80 | + } |
| 81 | +} |
0 commit comments