Skip to content

Commit d8fe7ef

Browse files
committed
Add NoAccessLevelOnExtensionDeclaration rule to swift-format
1 parent 031b2e3 commit d8fe7ef

File tree

58 files changed

+398
-391
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+398
-391
lines changed

.swift-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"rules": {
1010
"AlwaysUseLowerCamelCase": false,
1111
"AmbiguousTrailingClosureOverload": false,
12+
"NoAccessLevelOnExtensionDeclaration": true,
1213
"NoBlockComments": false,
1314
"OrderedImports": true,
1415
"UseLetInEveryBoundCaseVariable": false,

CodeGeneration/Sources/SyntaxSupport/Node.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,8 @@ public struct CollectionNode {
420420
}
421421
}
422422

423-
fileprivate extension Child {
424-
var kinds: [SyntaxNodeKind] {
423+
extension Child {
424+
fileprivate var kinds: [SyntaxNodeKind] {
425425
switch kind {
426426
case .node(let kind):
427427
return [kind]

CodeGeneration/Sources/SyntaxSupport/String+Extensions.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,20 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
public extension StringProtocol {
14-
var withFirstCharacterLowercased: String {
13+
extension StringProtocol {
14+
public var withFirstCharacterLowercased: String {
1515
guard first?.isLetter ?? false else {
1616
return String(first!) + dropFirst().withFirstCharacterLowercased
1717
}
1818
return prefix(1).lowercased() + dropFirst()
1919
}
20-
var withFirstCharacterUppercased: String {
20+
public var withFirstCharacterUppercased: String {
2121
guard first?.isLetter ?? false else {
2222
return String(first!) + dropFirst().withFirstCharacterUppercased
2323
}
2424
return prefix(1).uppercased() + dropFirst()
2525
}
26-
var backtickedIfNeeded: String {
26+
public var backtickedIfNeeded: String {
2727
if Keyword.allCases.map(\.spec).contains(where: {
2828
$0.name == self && ($0.isLexerClassified || $0.name == "Type" || $0.name == "Protocol")
2929
}) {

CodeGeneration/Sources/SyntaxSupport/Utils.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ extension SwiftSyntax.Trivia {
5858
}
5959
}
6060

61-
public extension Collection {
61+
extension Collection {
6262
/// If the collection contains a single element, return it, otherwise `nil`.
63-
var only: Element? {
63+
public var only: Element? {
6464
if !isEmpty && index(after: startIndex) == endIndex {
6565
return self.first!
6666
} else {
@@ -69,8 +69,8 @@ public extension Collection {
6969
}
7070
}
7171

72-
public extension TokenSyntax {
73-
var backtickedIfNeeded: TokenSyntax {
72+
extension TokenSyntax {
73+
public var backtickedIfNeeded: TokenSyntax {
7474
if Keyword.allCases.map(\.spec).contains(where: {
7575
$0.name == self.description && ($0.isLexerClassified || $0.name == "Type" || $0.name == "Protocol")
7676
}) {

CodeGeneration/Sources/Utils/SyntaxBuildableChild.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ public enum SyntaxOrTokenNodeKind: Hashable {
2323

2424
/// Extension to the ``Child`` type to provide functionality specific to
2525
/// SwiftSyntaxBuilder.
26-
public extension Child {
26+
extension Child {
2727
/// The type of this child, represented by a ``SyntaxBuildableType``, which can
2828
/// be used to create the corresponding `Buildable` and `ExpressibleAs` types.
29-
var buildableType: SyntaxBuildableType {
29+
public var buildableType: SyntaxBuildableType {
3030
let buildableKind: SyntaxOrTokenNodeKind
3131
switch kind {
3232
case .node(kind: let kind):
@@ -44,7 +44,7 @@ public extension Child {
4444
)
4545
}
4646

47-
var parameterBaseType: TypeSyntax {
47+
public var parameterBaseType: TypeSyntax {
4848
switch kind {
4949
case .nodeChoices:
5050
return self.syntaxChoicesType
@@ -53,11 +53,11 @@ public extension Child {
5353
}
5454
}
5555

56-
var parameterType: TypeSyntax {
56+
public var parameterType: TypeSyntax {
5757
return self.buildableType.optionalWrapped(type: parameterBaseType)
5858
}
5959

60-
var defaultValue: ExprSyntax? {
60+
public var defaultValue: ExprSyntax? {
6161
if isOptional || isUnexpectedNodes {
6262
if buildableType.isBaseType && kind.isNodeChoicesEmpty {
6363
return ExprSyntax("\(buildableType.buildable).none")
@@ -85,7 +85,7 @@ public extension Child {
8585
/// If the child node has a default value, return an expression of the form
8686
/// ` = default_value` that can be used as the default value to for a
8787
/// function parameter. Otherwise, return `nil`.
88-
var defaultInitialization: InitializerClauseSyntax? {
88+
public var defaultInitialization: InitializerClauseSyntax? {
8989
if let defaultValue {
9090
return InitializerClauseSyntax(
9191
equal: .equalToken(leadingTrivia: .space, trailingTrivia: .space),
@@ -99,7 +99,7 @@ public extension Child {
9999
/// If this node is a token that can't contain arbitrary text, generate a Swift
100100
/// `precondition` statement that verifies the variable with name var_name and of type
101101
/// ``TokenSyntax`` contains one of the supported text options. Otherwise return `nil`.
102-
func generateAssertStmtTextChoices(varName: String) -> FunctionCallExprSyntax? {
102+
public func generateAssertStmtTextChoices(varName: String) -> FunctionCallExprSyntax? {
103103
guard case .token(choices: let choices, requiresLeadingSpace: _, requiresTrailingSpace: _) = kind else {
104104
return nil
105105
}

CodeGeneration/Sources/Utils/SyntaxBuildableNode.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,41 @@ import SyntaxSupport
1414

1515
/// Extension to the ``Node`` type to provide functionality specific to
1616
/// SwiftSyntaxBuilder.
17-
public extension Node {
17+
extension Node {
1818
/// The node's syntax kind as ``SyntaxBuildableType``.
19-
var type: SyntaxBuildableType {
19+
public var type: SyntaxBuildableType {
2020
SyntaxBuildableType(kind: .node(kind: kind))
2121
}
2222

2323
/// The node's syntax kind as ``SyntaxBuildableType``.
24-
var baseType: SyntaxBuildableType {
24+
public var baseType: SyntaxBuildableType {
2525
if base == .syntaxCollection {
2626
return SyntaxBuildableType(kind: .node(kind: .syntax))
2727
} else {
2828
return SyntaxBuildableType(kind: .node(kind: base))
2929
}
3030
}
3131

32-
static func from(type: SyntaxBuildableType) -> Node {
32+
public static func from(type: SyntaxBuildableType) -> Node {
3333
guard case .node(kind: let kind) = type.kind, let node = SYNTAX_NODE_MAP[kind] else {
3434
fatalError("Base name \(type) does not have a syntax node")
3535
}
3636
return node
3737
}
3838
}
3939

40-
public extension LayoutNode {
40+
extension LayoutNode {
4141
/// Assuming this node has a single child without a default value, that child.
42-
var singleNonDefaultedChild: Child {
42+
public var singleNonDefaultedChild: Child {
4343
let nonDefaultedParams = children.filter { $0.defaultInitialization == nil }
4444
precondition(nonDefaultedParams.count == 1)
4545
return nonDefaultedParams[0]
4646
}
4747
}
4848

49-
public extension CollectionNode {
49+
extension CollectionNode {
5050
/// Assuming this node is a syntax collection, the type of its elements.
51-
var collectionElementType: SyntaxBuildableType {
51+
public var collectionElementType: SyntaxBuildableType {
5252
if elementChoices.count > 1 {
5353
return SyntaxBuildableType(kind: .node(kind: .syntax))
5454
} else {

CodeGeneration/Sources/generate-swift-syntax/templates/swiftsyntax/RawSyntaxNodesFile.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import SwiftSyntaxBuilder
1515
import SyntaxSupport
1616
import Utils
1717

18-
fileprivate extension Node {
19-
var childrenChoicesEnums: [(name: TypeSyntax, choices: [(caseName: TokenSyntax, kind: SyntaxNodeKind)])] {
18+
extension Node {
19+
fileprivate var childrenChoicesEnums: [(name: TypeSyntax, choices: [(caseName: TokenSyntax, kind: SyntaxNodeKind)])] {
2020
let node = self
2121
if let node = node.layoutNode {
2222
return node.children.compactMap {
@@ -268,8 +268,8 @@ func rawSyntaxNodesFile(nodesStartingWith: [Character]) -> SourceFileSyntax {
268268
}
269269
}
270270

271-
fileprivate extension Child {
272-
var rawParameterType: TypeSyntax {
271+
extension Child {
272+
fileprivate var rawParameterType: TypeSyntax {
273273
let paramType: TypeSyntax
274274
if case ChildKind.nodeChoices = kind {
275275
paramType = syntaxChoicesType

CodeGeneration/Tests/ValidateSyntaxNodes/ValidateSyntaxNodes.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ fileprivate func assertFailuresMatchXFails(
4848
assertNoFailures(unmatchedXFails, message: "Unmatched expected failures", file: file, line: line)
4949
}
5050

51-
fileprivate extension ChildKind {
52-
func hasSameType(as other: ChildKind) -> Bool {
51+
extension ChildKind {
52+
fileprivate func hasSameType(as other: ChildKind) -> Bool {
5353
switch (self, other) {
5454
case (.node(let kind), .node(kind: let otherKind)):
5555
return kind == otherKind
@@ -68,7 +68,7 @@ fileprivate extension ChildKind {
6868
}
6969
}
7070

71-
var isCollection: Bool {
71+
fileprivate var isCollection: Bool {
7272
switch self {
7373
case .node: return false
7474
case .nodeChoices(let choices): return choices.contains(where: { $0.kind.isCollection })
@@ -78,13 +78,13 @@ fileprivate extension ChildKind {
7878
}
7979
}
8080

81-
fileprivate extension Child {
82-
func hasSameType(as other: Child) -> Bool {
81+
extension Child {
82+
fileprivate func hasSameType(as other: Child) -> Bool {
8383
return varOrCaseName.description == other.varOrCaseName.description && kind.hasSameType(as: other.kind)
8484
&& isOptional == other.isOptional
8585
}
8686

87-
func isFollowedByColonToken(in node: LayoutNode) -> Bool {
87+
fileprivate func isFollowedByColonToken(in node: LayoutNode) -> Bool {
8888
let childIndex = node.children.firstIndex(where: { $0.varOrCaseName.description == self.varOrCaseName.description })
8989
guard let childIndex else {
9090
preconditionFailure("\(self.varOrCaseName) is not a child of \(node.kind.syntaxType)")
@@ -100,13 +100,13 @@ fileprivate extension Child {
100100
}
101101
}
102102

103-
fileprivate extension Array where Element: Hashable, Element: Comparable {
103+
extension Array where Element: Hashable, Element: Comparable {
104104
/// Returns the element that occurs most frequently in the array.
105105
///
106106
/// If there is a tie, returns the lexicographically first element.
107107
///
108108
/// Returns `nil` if the array is empty.
109-
var mostCommon: Element? {
109+
fileprivate var mostCommon: Element? {
110110
var elementCounts: [Element: Int] = [:]
111111
for element in self {
112112
elementCounts[element, default: 0] += 1

Examples/Sources/MacroExamples/Implementation/ComplexMacros/ObservableMacro.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
import SwiftSyntax
1414
import SwiftSyntaxMacros
1515

16-
private extension DeclSyntaxProtocol {
17-
var isObservableStoredProperty: Bool {
16+
extension DeclSyntaxProtocol {
17+
fileprivate var isObservableStoredProperty: Bool {
1818
if let property = self.as(VariableDeclSyntax.self),
1919
let binding = property.bindings.first,
2020
let identifier = binding.pattern.as(IdentifierPatternSyntax.self)?.identifier,

Sources/SwiftBasicFormat/BasicFormat.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -658,16 +658,16 @@ open class BasicFormat: SyntaxRewriter {
658658
}
659659
}
660660

661-
fileprivate extension TokenSyntax {
662-
var isStringSegment: Bool {
661+
extension TokenSyntax {
662+
fileprivate var isStringSegment: Bool {
663663
if case .stringSegment = self.tokenKind {
664664
return true
665665
} else {
666666
return false
667667
}
668668
}
669669

670-
var isStringSegmentWithLastCharacterBeingNewline: Bool {
670+
fileprivate var isStringSegmentWithLastCharacterBeingNewline: Bool {
671671
switch self.tokenKind {
672672
case .stringSegment(let segment):
673673
return segment.last?.isNewline ?? false
@@ -677,9 +677,9 @@ fileprivate extension TokenSyntax {
677677
}
678678
}
679679

680-
fileprivate extension SyntaxProtocol {
680+
extension SyntaxProtocol {
681681
/// Returns this node or the first ancestor that satisfies `condition`.
682-
func ancestorOrSelf<T>(mapping map: (Syntax) -> T?) -> T? {
682+
fileprivate func ancestorOrSelf<T>(mapping map: (Syntax) -> T?) -> T? {
683683
var walk: Syntax? = Syntax(self)
684684
while let unwrappedParent = walk {
685685
if let mapped = map(unwrappedParent) {

Sources/SwiftBasicFormat/InferIndentation.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ private class IndentationInferrer: SyntaxVisitor {
124124
}
125125
}
126126

127-
fileprivate extension Array<Int> {
128-
var sum: Int {
127+
extension Array<Int> {
128+
fileprivate var sum: Int {
129129
return self.reduce(0) { return $0 + $1 }
130130
}
131131
}

Sources/SwiftBasicFormat/SyntaxProtocol+Formatted.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ public import SwiftSyntax
1616
import SwiftSyntax
1717
#endif
1818

19-
public extension SyntaxProtocol {
19+
extension SyntaxProtocol {
2020
/// Build a syntax node from this `Buildable` and format it with the given format.
21-
func formatted(using format: BasicFormat = BasicFormat()) -> Syntax {
21+
public func formatted(using format: BasicFormat = BasicFormat()) -> Syntax {
2222
format.reset()
2323
return format.rewrite(self)
2424
}

Sources/SwiftCompilerPlugin/CompilerPlugin.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,16 +236,16 @@ internal struct PluginHostConnection: MessageConnection {
236236
}
237237
}
238238

239-
private extension FileHandle {
240-
func _write(contentsOf data: Data) throws {
239+
extension FileHandle {
240+
fileprivate func _write(contentsOf data: Data) throws {
241241
if #available(macOS 10.15.4, iOS 13.4, watchOS 6.2, tvOS 13.4, *) {
242242
return try self.write(contentsOf: data)
243243
} else {
244244
return self.write(data)
245245
}
246246
}
247247

248-
func _read(upToCount count: Int) throws -> Data? {
248+
fileprivate func _read(upToCount count: Int) throws -> Data? {
249249
if #available(macOS 10.15.4, iOS 13.4, watchOS 6.2, tvOS 13.4, *) {
250250
return try self.read(upToCount: count)
251251
} else {

Sources/SwiftCompilerPluginMessageHandling/CompilerPluginMessageHandler.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,13 @@ struct UnimplementedError: Error, CustomStringConvertible {
182182
}
183183

184184
/// Default implementation of 'PluginProvider' requirements.
185-
public extension PluginProvider {
186-
var features: [PluginFeature] {
185+
extension PluginProvider {
186+
public var features: [PluginFeature] {
187187
// No optional features by default.
188188
return []
189189
}
190190

191-
func loadPluginLibrary(libraryPath: String, moduleName: String) throws {
191+
public func loadPluginLibrary(libraryPath: String, moduleName: String) throws {
192192
// This should be unreachable. The host should not call 'loadPluginLibrary'
193193
// unless the feature is not declared.
194194
throw UnimplementedError()

Sources/SwiftCompilerPluginMessageHandling/Macros.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ extension CompilerPluginMessageHandler {
184184
}
185185
}
186186

187-
private extension MacroRole {
188-
init(messageMacroRole: PluginMessage.MacroRole) {
187+
extension MacroRole {
188+
fileprivate init(messageMacroRole: PluginMessage.MacroRole) {
189189
switch messageMacroRole {
190190
case .expression: self = .expression
191191
case .declaration: self = .declaration

Sources/SwiftCompilerPluginMessageHandling/PluginMacroExpansionContext.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,9 @@ class SourceManager {
180180
}
181181
}
182182

183-
fileprivate extension Syntax {
183+
extension Syntax {
184184
/// Get a position in the node by ``PositionInSyntaxNode``.
185-
func position(at pos: PositionInSyntaxNode) -> AbsolutePosition {
185+
fileprivate func position(at pos: PositionInSyntaxNode) -> AbsolutePosition {
186186
switch pos {
187187
case .beforeLeadingTrivia:
188188
return self.position

Sources/SwiftCompilerPluginMessageHandling/PluginMessageCompatibility.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
/// Old compiler might send '.declaration' as "freeStandingDeclaration".
14-
@_spi(PluginMessage) public extension PluginMessage.MacroRole {
15-
init(from decoder: Decoder) throws {
14+
@_spi(PluginMessage) extension PluginMessage.MacroRole {
15+
public init(from decoder: Decoder) throws {
1616
let stringValue = try decoder.singleValueContainer().decode(String.self)
1717
if let role = Self(rawValue: stringValue) {
1818
self = role

Sources/SwiftDiagnostics/FixIt.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ extension FixIt {
6969
}
7070
}
7171

72-
private extension FixIt.Change {
73-
var edit: SourceEdit {
72+
extension FixIt.Change {
73+
fileprivate var edit: SourceEdit {
7474
switch self {
7575
case .replace(let oldNode, let newNode):
7676
return SourceEdit(

0 commit comments

Comments
 (0)