|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2020 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 SwiftFormatCore |
| 14 | +import SwiftSyntax |
| 15 | + |
| 16 | +/// Declarations at file scope should be declared `fileprivate`, not `private`. |
| 17 | +/// |
| 18 | +/// Using `private` at file scope actually gives the declaration `fileprivate` visibility, so using |
| 19 | +/// `fileprivate` explicitly is a better indicator of intent. |
| 20 | +/// |
| 21 | +/// Lint: If a file-scoped declaration has `private` visibility, a lint error is raised. |
| 22 | +/// |
| 23 | +/// Format: File-scoped declarations that have `private` visibility will have their visibility |
| 24 | +/// changed to `fileprivate`. |
| 25 | +public final class FileprivateAtFileScope: SyntaxFormatRule { |
| 26 | + public override func visit(_ node: SourceFileSyntax) -> Syntax { |
| 27 | + let newStatements = rewrittenCodeBlockItems(node.statements) |
| 28 | + return Syntax(node.withStatements(newStatements)) |
| 29 | + } |
| 30 | + |
| 31 | + /// Returns a list of code block items equivalent to the given list, but where any file-scoped |
| 32 | + /// declarations have had their `private` modifier replaced by `fileprivate` if present. |
| 33 | + /// |
| 34 | + /// - Parameter codeBlockItems: The list of code block items to rewrite. |
| 35 | + /// - Returns: A new `CodeBlockItemListSyntax` that has possibly been rewritten. |
| 36 | + private func rewrittenCodeBlockItems(_ codeBlockItems: CodeBlockItemListSyntax) |
| 37 | + -> CodeBlockItemListSyntax |
| 38 | + { |
| 39 | + let newCodeBlockItems = codeBlockItems.map { codeBlockItem -> CodeBlockItemSyntax in |
| 40 | + switch codeBlockItem.item.as(SyntaxEnum.self) { |
| 41 | + case .ifConfigDecl(let ifConfigDecl): |
| 42 | + // We need to look through `#if/#elseif/#else` blocks because the decls directly inside |
| 43 | + // them are still considered file-scope for our purposes. |
| 44 | + return codeBlockItem.withItem(Syntax(rewrittenIfConfigDecl(ifConfigDecl))) |
| 45 | + |
| 46 | + case .functionDecl(let functionDecl): |
| 47 | + return codeBlockItem.withItem( |
| 48 | + Syntax(rewrittenDecl( |
| 49 | + functionDecl, |
| 50 | + modifiers: functionDecl.modifiers, |
| 51 | + factory: functionDecl.withModifiers))) |
| 52 | + |
| 53 | + case .variableDecl(let variableDecl): |
| 54 | + return codeBlockItem.withItem( |
| 55 | + Syntax(rewrittenDecl( |
| 56 | + variableDecl, |
| 57 | + modifiers: variableDecl.modifiers, |
| 58 | + factory: variableDecl.withModifiers))) |
| 59 | + |
| 60 | + case .classDecl(let classDecl): |
| 61 | + return codeBlockItem.withItem( |
| 62 | + Syntax(rewrittenDecl( |
| 63 | + classDecl, |
| 64 | + modifiers: classDecl.modifiers, |
| 65 | + factory: classDecl.withModifiers))) |
| 66 | + |
| 67 | + case .structDecl(let structDecl): |
| 68 | + return codeBlockItem.withItem( |
| 69 | + Syntax(rewrittenDecl( |
| 70 | + structDecl, |
| 71 | + modifiers: structDecl.modifiers, |
| 72 | + factory: structDecl.withModifiers))) |
| 73 | + |
| 74 | + case .enumDecl(let enumDecl): |
| 75 | + return codeBlockItem.withItem( |
| 76 | + Syntax(rewrittenDecl( |
| 77 | + enumDecl, |
| 78 | + modifiers: enumDecl.modifiers, |
| 79 | + factory: enumDecl.withModifiers))) |
| 80 | + |
| 81 | + case .protocolDecl(let protocolDecl): |
| 82 | + return codeBlockItem.withItem( |
| 83 | + Syntax(rewrittenDecl( |
| 84 | + protocolDecl, |
| 85 | + modifiers: protocolDecl.modifiers, |
| 86 | + factory: protocolDecl.withModifiers))) |
| 87 | + |
| 88 | + case .typealiasDecl(let typealiasDecl): |
| 89 | + return codeBlockItem.withItem( |
| 90 | + Syntax(rewrittenDecl( |
| 91 | + typealiasDecl, |
| 92 | + modifiers: typealiasDecl.modifiers, |
| 93 | + factory: typealiasDecl.withModifiers))) |
| 94 | + |
| 95 | + case .extensionDecl(let extensionDecl): |
| 96 | + return codeBlockItem.withItem( |
| 97 | + Syntax(rewrittenDecl( |
| 98 | + extensionDecl, |
| 99 | + modifiers: extensionDecl.modifiers, |
| 100 | + factory: extensionDecl.withModifiers))) |
| 101 | + |
| 102 | + default: |
| 103 | + return codeBlockItem |
| 104 | + } |
| 105 | + } |
| 106 | + return SyntaxFactory.makeCodeBlockItemList(newCodeBlockItems) |
| 107 | + } |
| 108 | + |
| 109 | + /// Returns a new `IfConfigDeclSyntax` equivalent to the given node, but where any file-scoped |
| 110 | + /// declarations have had their `private` modifier replaced by `fileprivate` if present. |
| 111 | + /// |
| 112 | + /// - Parameter ifConfigDecl: The `IfConfigDeclSyntax` to rewrite. |
| 113 | + /// - Returns: A new `IfConfigDeclSyntax` that has possibly been rewritten. |
| 114 | + private func rewrittenIfConfigDecl(_ ifConfigDecl: IfConfigDeclSyntax) -> IfConfigDeclSyntax { |
| 115 | + let newClauses = ifConfigDecl.clauses.map { clause -> IfConfigClauseSyntax in |
| 116 | + switch clause.elements.as(SyntaxEnum.self) { |
| 117 | + case .codeBlockItemList(let codeBlockItemList): |
| 118 | + return clause.withElements(Syntax(rewrittenCodeBlockItems(codeBlockItemList))) |
| 119 | + default: |
| 120 | + return clause |
| 121 | + } |
| 122 | + } |
| 123 | + return ifConfigDecl.withClauses(SyntaxFactory.makeIfConfigClauseList(newClauses)) |
| 124 | + } |
| 125 | + |
| 126 | + /// Returns a rewritten version of the given declaration if its modifier list contains `private` |
| 127 | + /// that contains `fileprivate` instead. |
| 128 | + /// |
| 129 | + /// If the modifier list does not contain `private`, the original declaration is returned |
| 130 | + /// unchanged. |
| 131 | + /// |
| 132 | + /// - Parameters: |
| 133 | + /// - decl: The declaration to possibly rewrite. |
| 134 | + /// - modifiers: The modifier list of the declaration (i.e., `decl.modifiers`). |
| 135 | + /// - factory: A reference to the `decl`'s `withModifiers` instance method that is called to |
| 136 | + /// rewrite the node if needed. |
| 137 | + /// - Returns: A new node if the modifiers were rewritten, or the original node if not. |
| 138 | + private func rewrittenDecl<DeclType: DeclSyntaxProtocol>( |
| 139 | + _ decl: DeclType, |
| 140 | + modifiers: ModifierListSyntax?, |
| 141 | + factory: (ModifierListSyntax?) -> DeclType |
| 142 | + ) -> DeclType { |
| 143 | + guard let modifiers = modifiers, modifiers.has(modifier: "private") else { |
| 144 | + return decl |
| 145 | + } |
| 146 | + |
| 147 | + let newModifiers = modifiers.map { modifier -> DeclModifierSyntax in |
| 148 | + let name = modifier.name |
| 149 | + if name.tokenKind == .privateKeyword { |
| 150 | + diagnose(.replacePrivateWithFileprivate, on: name) |
| 151 | + return modifier.withName(name.withKind(.fileprivateKeyword)) |
| 152 | + } |
| 153 | + return modifier |
| 154 | + } |
| 155 | + return factory(SyntaxFactory.makeModifierList(newModifiers)) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +extension Diagnostic.Message { |
| 160 | + public static let replacePrivateWithFileprivate = |
| 161 | + Diagnostic.Message(.warning, "replace 'private' with 'fileprivate' on file-scoped declarations") |
| 162 | +} |
0 commit comments