Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Sources/SwiftFormat/Pipelines+Generated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class LintPipeline: SyntaxVisitor {
override func visit(_ node: CodeBlockItemListSyntax) -> SyntaxVisitorContinueKind {
visitIfEnabled(DoNotUseSemicolons.visit, for: node)
visitIfEnabled(OneVariableDeclarationPerLine.visit, for: node)
visitIfEnabled(UseEarlyExits.visit, for: node)
return .visitChildren
}

Expand Down Expand Up @@ -106,6 +107,11 @@ class LintPipeline: SyntaxVisitor {
return .visitChildren
}

override func visit(_ node: ForInStmtSyntax) -> SyntaxVisitorContinueKind {
visitIfEnabled(UseWhereClausesInForLoops.visit, for: node)
return .visitChildren
}

override func visit(_ node: ForcedValueExprSyntax) -> SyntaxVisitorContinueKind {
visitIfEnabled(NeverForceUnwrap.visit, for: node)
return .visitChildren
Expand Down Expand Up @@ -314,9 +320,11 @@ extension FormatPipeline {
node = OneVariableDeclarationPerLine(context: context).visit(node)
node = OrderedImports(context: context).visit(node)
node = ReturnVoidInsteadOfEmptyTuple(context: context).visit(node)
node = UseEarlyExits(context: context).visit(node)
node = UseShorthandTypeNames(context: context).visit(node)
node = UseSingleLinePropertyGetter(context: context).visit(node)
node = UseTripleSlashForDocumentationComments(context: context).visit(node)
node = UseWhereClausesInForLoops(context: context).visit(node)
return node
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ enum RuleRegistry {
"OnlyOneTrailingClosureArgument": true,
"OrderedImports": true,
"ReturnVoidInsteadOfEmptyTuple": true,
"UseEarlyExits": false,
"UseLetInEveryBoundCaseVariable": true,
"UseShorthandTypeNames": true,
"UseSingleLinePropertyGetter": true,
"UseSynthesizedInitializer": true,
"UseTripleSlashForDocumentationComments": true,
"UseWhereClausesInForLoops": false,
"ValidateDocumentationComments": false,
]
}
4 changes: 4 additions & 0 deletions Sources/SwiftFormatRules/UseEarlyExits.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ import SwiftSyntax
/// equivalent `guard ... else { return/throw/break/continue }` constructs.
public final class UseEarlyExits: SyntaxFormatRule {

/// Identifies this rule as being opt-in. This rule is experimental and not yet stable enough to
/// be enabled by default.
public override class var isOptIn: Bool { return true }

public override func visit(_ node: CodeBlockItemListSyntax) -> Syntax {
// Continue recursing down the tree first, so that any nested/child nodes get transformed first.
let nodeAfterTransformingChildren = super.visit(node)
Expand Down
4 changes: 4 additions & 0 deletions Sources/SwiftFormatRules/UseWhereClausesInForLoops.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import SwiftSyntax
/// statement factored out to a `where` clause.
public final class UseWhereClausesInForLoops: SyntaxFormatRule {

/// Identifies this rule as being opt-in. This rule is experimental and not yet stable enough to
/// be enabled by default.
public override class var isOptIn: Bool { return true }

public override func visit(_ node: ForInStmtSyntax) -> StmtSyntax {
// Extract IfStmt node if it's the only node in the function's body.
guard !node.body.statements.isEmpty else { return StmtSyntax(node) }
Expand Down
7 changes: 2 additions & 5 deletions Sources/generate-pipeline/RuleCollector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import Foundation
import SwiftFormatCore
import SwiftSyntax

// These rules will not be added to the pipeline.
let suppressRules = ["UseEarlyExits", "UseWhereClausesInForLoops"]

/// Collects information about rules in the formatter code base.
final class RuleCollector {
/// Information about a detected rule.
Expand Down Expand Up @@ -99,8 +96,8 @@ final class RuleCollector {
return nil
}

// Make sure the rule isn't suppressed, and it must have an inheritance clause.
guard !suppressRules.contains(typeName), let inheritanceClause = maybeInheritanceClause else {
// Make sure it has an inheritance clause.
guard let inheritanceClause = maybeInheritanceClause else {
return nil
}

Expand Down