Skip to content

Commit df8de68

Browse files
.swift-format: Specify an exhaustive configuration (#1057)
* .swift-format: Sort lexicographically * .swift-format: Specify an exhaustive configuration Unexpectedly, an omitted rule tells swift-format to ignore it altogether rather than assume the default setting. This change applies the default configuration, retaining current settings and disabling 3 default-enabled rules: `DoNotUseSemicolons`, `NoAccessLevelOnExtensionDeclaration`, and `DontRepeatTypeInStaticProperties`. The latter is disabled due to undesired effect: ``` warning: [DontRepeatTypeInStaticProperties] remove the suffix 'Paths' from the name of the variable 'envSearchPaths' ```
1 parent 2a1d811 commit df8de68

14 files changed

+93
-33
lines changed

.swift-format

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,76 @@
11
{
2-
"version": 1,
3-
"lineLength": 120,
2+
"fileScopedDeclarationPrivacy": {
3+
"accessLevel": "private"
4+
},
5+
"indentConditionalCompilationBlocks": false,
6+
"indentSwitchCaseLabels": false,
47
"indentation": {
58
"spaces": 2
69
},
10+
"lineBreakAroundMultilineExpressionChainComponents": false,
11+
"lineBreakBeforeControlFlowKeywords": false,
712
"lineBreakBeforeEachArgument": true,
8-
"indentConditionalCompilationBlocks": false,
13+
"lineBreakBeforeEachGenericRequirement": false,
14+
"lineBreakBetweenDeclarationAttributes": false,
15+
"lineLength": 120,
16+
"maximumBlankLines": 1,
17+
"multiElementCollectionTrailingCommas": true,
18+
"noAssignmentInExpressions": {
19+
"allowedFunctions": [
20+
"XCTAssertNoThrow"
21+
]
22+
},
923
"prioritizeKeepingFunctionOutputTogether": true,
24+
"reflowMultilineStringLiterals": {
25+
"never": {
26+
27+
}
28+
},
29+
"respectsExistingLineBreaks": true,
1030
"rules": {
31+
"AllPublicDeclarationsHaveDocumentation": false,
32+
"AlwaysUseLiteralForEmptyCollectionInit": false,
1133
"AlwaysUseLowerCamelCase": false,
1234
"AmbiguousTrailingClosureOverload": false,
35+
"BeginDocumentationCommentWithOneLineSummary": false,
36+
"DoNotUseSemicolons": false,
37+
"DontRepeatTypeInStaticProperties": false,
38+
"FileScopedDeclarationPrivacy": true,
39+
"FullyIndirectEnum": true,
40+
"GroupNumericLiterals": true,
41+
"IdentifiersMustBeASCII": true,
42+
"NeverForceUnwrap": false,
43+
"NeverUseForceTry": false,
44+
"NeverUseImplicitlyUnwrappedOptionals": false,
45+
"NoAccessLevelOnExtensionDeclaration": false,
46+
"NoAssignmentInExpressions": true,
1347
"NoBlockComments": false,
48+
"NoCasesWithOnlyFallthrough": true,
49+
"NoEmptyTrailingClosureParentheses": true,
50+
"NoLabelsInCasePatterns": true,
51+
"NoLeadingUnderscores": false,
52+
"NoParensAroundConditions": true,
53+
"NoPlaygroundLiterals": true,
54+
"NoVoidReturnOnFunctionSignature": true,
55+
"OmitExplicitReturns": false,
56+
"OneCasePerLine": true,
57+
"OneVariableDeclarationPerLine": true,
58+
"OnlyOneTrailingClosureArgument": true,
1459
"OrderedImports": true,
60+
"ReplaceForEachWithForLoop": true,
61+
"ReturnVoidInsteadOfEmptyTuple": true,
62+
"TypeNamesShouldBeCapitalized": true,
63+
"UseEarlyExits": false,
64+
"UseExplicitNilCheckInConditions": true,
1565
"UseLetInEveryBoundCaseVariable": false,
66+
"UseShorthandTypeNames": true,
67+
"UseSingleLinePropertyGetter": true,
1668
"UseSynthesizedInitializer": false,
17-
"ReturnVoidInsteadOfEmptyTuple": true,
18-
"NoVoidReturnOnFunctionSignature": true,
19-
}
69+
"UseTripleSlashForDocumentationComments": true,
70+
"UseWhereClausesInForLoops": false,
71+
"ValidateDocumentationComments": false
72+
},
73+
"spacesAroundRangeFormationOperators": false,
74+
"spacesBeforeEndOfLineComments": 2,
75+
"version": 1,
2076
}

Sources/SwiftFormat/Core/DocumentationCommentText.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ private func asciiArtLength(of string: Substring, leadingSpaces: Int) -> Int {
204204
/// Returns the start index of the earliest comment in the Trivia if we work backwards and
205205
/// skip through comments, newlines, and whitespace. Then we advance a bit forward to be sure
206206
/// the returned index is actually a comment and not whitespace.
207-
private func findCommentStartIndex(_ triviaArray: Array<TriviaPiece>) -> Array<TriviaPiece>.Index {
207+
private func findCommentStartIndex(_ triviaArray: [TriviaPiece]) -> Array<TriviaPiece>.Index {
208208
func firstCommentIndex(_ slice: ArraySlice<TriviaPiece>) -> Array<TriviaPiece>.Index {
209209
return slice.firstIndex(where: {
210210
switch $0 {

Sources/SwiftFormat/Core/RuleMask.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ enum IgnoreDirective: CustomStringConvertible {
131131
/// The rule status comment directives implementation intentionally supports exactly the same nodes
132132
/// as `TokenStreamCreator` to disable pretty printing. This ensures ignore comments for pretty
133133
/// printing and for rules are as consistent as possible.
134-
fileprivate class RuleStatusCollectionVisitor: SyntaxVisitor {
134+
private class RuleStatusCollectionVisitor: SyntaxVisitor {
135135
/// Describes the possible matches for ignore directives, in comments.
136136
enum RuleStatusDirectiveMatch {
137137
/// There is a directive that applies to all rules.
@@ -219,7 +219,9 @@ fileprivate class RuleStatusCollectionVisitor: SyntaxVisitor {
219219
// node's children are irrelevant because all rules are suppressed by this node.
220220
return .skipChildren
221221
case .subset(let ruleNames):
222-
ruleNames.forEach { ruleMap[$0, default: []].append(sourceRange) }
222+
for ruleName in ruleNames {
223+
ruleMap[ruleName, default: []].append(sourceRange)
224+
}
223225
break
224226
}
225227
}

Sources/SwiftFormat/PrettyPrint/PrettyPrint.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public class PrettyPrinter {
242242
// the group.
243243
case .open(let breaktype):
244244
// Determine if the break tokens in this group need to be forced.
245-
if (!canFit(length) || lastBreak), case .consistent = breaktype {
245+
if !canFit(length) || lastBreak, case .consistent = breaktype {
246246
forceBreakStack.append(true)
247247
} else {
248248
forceBreakStack.append(false)

Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fileprivate extension AccessorBlockSyntax {
2727

2828
/// Visits the nodes of a syntax tree and constructs a linear stream of formatting tokens that
2929
/// tell the pretty printer how the source text should be laid out.
30-
fileprivate final class TokenStreamCreator: SyntaxVisitor {
30+
private final class TokenStreamCreator: SyntaxVisitor {
3131
private var tokens = [Token]()
3232
private var beforeMap = [TokenSyntax: [Token]]()
3333
private var afterMap = [TokenSyntax: [[Token]]]()
@@ -3031,7 +3031,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
30313031
}
30323032

30333033
for after in afterGroups.reversed() {
3034-
after.forEach { afterToken in
3034+
for afterToken in after {
30353035
var shouldExtractTrailingComment = false
30363036
if wasLineComment && !hasAppendedTrailingComment {
30373037
switch afterToken {
@@ -4455,7 +4455,7 @@ extension TriviaPiece {
44554455
/// - trivia: Leading trivia for a node that the formatter supports ignoring.
44564456
/// - isWholeFile: Whether to search for a whole-file ignore directive or per node ignore.
44574457
/// - Returns: Whether the trivia contains the specified type of ignore directive.
4458-
fileprivate func isFormatterIgnorePresent(inTrivia trivia: Trivia, isWholeFile: Bool) -> Bool {
4458+
private func isFormatterIgnorePresent(inTrivia trivia: Trivia, isWholeFile: Bool) -> Bool {
44594459
func isFormatterIgnore(in commentText: String, prefix: String, suffix: String) -> Bool {
44604460
let trimmed =
44614461
commentText.dropFirst(prefix.count)
@@ -4488,7 +4488,7 @@ fileprivate func isFormatterIgnorePresent(inTrivia trivia: Trivia, isWholeFile:
44884488
/// be safely ignored.
44894489
///
44904490
/// - Parameter node: A node that can be safely ignored.
4491-
fileprivate func shouldFormatterIgnore(node: Syntax) -> Bool {
4491+
private func shouldFormatterIgnore(node: Syntax) -> Bool {
44924492
// Regardless of the level of nesting, if the ignore directive is present on the first token
44934493
// contained within the node then the entire node is eligible for ignoring.
44944494
return isFormatterIgnorePresent(inTrivia: node.allPrecedingTrivia, isWholeFile: false)
@@ -4499,7 +4499,7 @@ fileprivate func shouldFormatterIgnore(node: Syntax) -> Bool {
44994499
/// in the original source).
45004500
///
45014501
/// - Parameter file: The root syntax node for a source file.
4502-
fileprivate func shouldFormatterIgnore(file: SourceFileSyntax) -> Bool {
4502+
private func shouldFormatterIgnore(file: SourceFileSyntax) -> Bool {
45034503
return isFormatterIgnorePresent(inTrivia: file.allPrecedingTrivia, isWholeFile: true)
45044504
}
45054505

Sources/SwiftFormat/PrettyPrint/Verbatim.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ struct Verbatim {
114114
}
115115

116116
/// Returns the leading number of spaces in the given string.
117-
fileprivate func numberOfLeadingSpaces(in text: Substring) -> Int {
117+
private func numberOfLeadingSpaces(in text: Substring) -> Int {
118118
var count = 0
119119
for char in text {
120120
if char == " " { count += 1 } else { break }

Sources/SwiftFormat/Rules/AlwaysUseLowerCamelCase.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public final class AlwaysUseLowerCamelCase: SyntaxLintRule {
196196
///
197197
/// - Parameter node: A node whose identifier may be used in diagnostics.
198198
/// - Returns: A human readable description of the node and its identifier.
199-
fileprivate func identifierDescription<NodeType: SyntaxProtocol>(for node: NodeType) -> String {
199+
private func identifierDescription<NodeType: SyntaxProtocol>(for node: NodeType) -> String {
200200
switch Syntax(node).as(SyntaxEnum.self) {
201201
case .closureSignature: return "closure parameter"
202202
case .enumCaseElement: return "enum case"

Sources/SwiftFormat/Rules/NoCasesWithOnlyFallthrough.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public final class NoCasesWithOnlyFallthrough: SyntaxFormatRule {
2727

2828
/// Flushes any un-collapsed violations to the new cases list.
2929
func flushViolations() {
30-
fallthroughOnlyCases.forEach {
31-
newChildren.append(.switchCase(visit($0)))
30+
for node in fallthroughOnlyCases {
31+
newChildren.append(.switchCase(visit(node)))
3232
}
3333
fallthroughOnlyCases.removeAll()
3434
}

Sources/SwiftFormat/Rules/OrderedImports.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,14 +296,14 @@ public final class OrderedImports: SyntaxFormatRule {
296296
}
297297

298298
/// Remove any leading blank lines from the main code.
299-
fileprivate func formatCodeblocks(_ codeblocks: inout [Line]) {
299+
private func formatCodeblocks(_ codeblocks: inout [Line]) {
300300
if let contentIndex = codeblocks.firstIndex(where: { !$0.isBlankLine }) {
301301
codeblocks.removeSubrange(0..<contentIndex)
302302
}
303303
}
304304

305305
/// Join the lists of Line objects into a single list of Lines with a blank line separating them.
306-
fileprivate func joinLines(_ inputLineLists: [Line]...) -> [Line] {
306+
private func joinLines(_ inputLineLists: [Line]...) -> [Line] {
307307
var lineLists = inputLineLists
308308
lineLists.removeAll { $0.isEmpty }
309309
guard lineLists.count > 0 else { return [] }
@@ -320,7 +320,7 @@ fileprivate func joinLines(_ inputLineLists: [Line]...) -> [Line] {
320320
/// This function transforms the statements in a CodeBlockItemListSyntax object into a list of Line
321321
/// objects. Blank lines and standalone comments are represented by their own Line object. Code with
322322
/// a trailing comment are represented together in the same Line.
323-
fileprivate func generateLines(
323+
private func generateLines(
324324
codeBlockItemList: CodeBlockItemListSyntax,
325325
context: Context
326326
) -> [Line] {
@@ -380,7 +380,7 @@ fileprivate func generateLines(
380380

381381
/// This function transforms a list of Line objects into a list of CodeBlockItemSyntax objects,
382382
/// replacing the trivia appropriately to ensure comments appear in the right location.
383-
fileprivate func convertToCodeBlockItems(lines: [Line]) -> [CodeBlockItemSyntax] {
383+
private func convertToCodeBlockItems(lines: [Line]) -> [CodeBlockItemSyntax] {
384384
var output: [CodeBlockItemSyntax] = []
385385
var pendingLeadingTrivia: [TriviaPiece] = []
386386

@@ -450,7 +450,7 @@ public enum LineType: CustomStringConvertible {
450450
/// represent a single printed line. Other types of code (e.g. structs and classes) will span
451451
/// multiple literal lines, but can still be represented by a single Line object. This is desireable
452452
/// since we aren't interested in rearranging those types of structures in this rule.
453-
fileprivate class Line {
453+
private class Line {
454454
/// Storage for the different types of AST nodes that can be held by a `Line`.
455455
enum SyntaxNode {
456456
/// A collection of code block items that aren't imports. These types of code blocks aren't

Sources/SwiftFormat/Rules/UseSynthesizedInitializer.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ public final class UseSynthesizedInitializer: SyntaxLintRule {
7272
// initializer, then all of the initializers must remain.
7373
let initializersCount = node.memberBlock.members.filter { $0.decl.is(InitializerDeclSyntax.self) }.count
7474
if extraneousInitializers.count == initializersCount {
75-
extraneousInitializers.forEach { diagnose(.removeRedundantInitializer, on: $0) }
75+
for initializer in extraneousInitializers {
76+
diagnose(.removeRedundantInitializer, on: initializer)
77+
}
7678
}
7779

7880
return .visitChildren
@@ -197,7 +199,7 @@ extension Finding.Message {
197199
}
198200

199201
/// Defines the access levels which may be assigned to a synthesized memberwise initializer.
200-
fileprivate enum AccessLevel {
202+
private enum AccessLevel {
201203
case `internal`
202204
case `fileprivate`
203205
case `private`
@@ -212,7 +214,7 @@ fileprivate enum AccessLevel {
212214
///
213215
/// - Parameter properties: The properties contained within the struct.
214216
/// - Returns: The synthesized memberwise initializer's access level.
215-
fileprivate func synthesizedInitAccessLevel(using properties: [VariableDeclSyntax]) -> AccessLevel {
217+
private func synthesizedInitAccessLevel(using properties: [VariableDeclSyntax]) -> AccessLevel {
216218
var hasFileprivate = false
217219
for property in properties {
218220
// Private takes precedence, so finding 1 private property defines the access level.

0 commit comments

Comments
 (0)