Skip to content

Commit

Permalink
Rename allow_multiline_func option to `ignore_multiline_function_si…
Browse files Browse the repository at this point in the history
…gnatures`
  • Loading branch information
leonardosrodrigues0 authored and SimplyDanny committed Aug 24, 2024
1 parent b0800d0 commit 3e549c3
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 134 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@

#### Enhancements

* None.
* Add `ignore_multiline_type_headers` and `ignore_multiline_statement_conditions`
options to `opening_brace` rule to allow opening braces to be on a new line after
multiline type headers or statement conditions. Rename `allow_multiline_func` to
`ignore_multiline_function_signatures`.
[leonardosrodrigues0](https://github.com/leonardosrodrigues0)
[#3720](https://github.com/realm/SwiftLint/issues/3720)

#### Bug Fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ struct OpeningBraceConfiguration: SeverityBasedRuleConfiguration {

@ConfigurationElement(key: "severity")
private(set) var severityConfiguration = SeverityConfiguration<Parent>(.warning)
@ConfigurationElement(key: "allow_multiline_func")
private(set) var allowMultilineFunc = false
@ConfigurationElement(key: "ignore_multiline_type_headers")
private(set) var ignoreMultilineTypeHeaders = false
@ConfigurationElement(key: "ignore_multiline_statement_conditions")
private(set) var ignoreMultilineStatementConditions = false
@ConfigurationElement(key: "ignore_multiline_function_signatures")
private(set) var ignoreMultilineFunctionSignatures = false
// TODO: [08/23/2026] Remove deprecation warning after ~2 years.
@ConfigurationElement(key: "allow_multiline_func", deprecationNotice: .suggestAlternative(
ruleID: Parent.identifier, name: "ignore_multiline_function_signatures"))
private(set) var allowMultilineFunc = false

var shouldIgnoreMultilineFunctionSignatures: Bool {
ignoreMultilineFunctionSignatures || allowMultilineFunc
}
}
76 changes: 30 additions & 46 deletions Source/SwiftLintBuiltInRules/Rules/Style/OpeningBraceRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,65 +28,53 @@ private extension OpeningBraceRule {
// MARK: - Type Declarations

override func visitPost(_ node: ActorDeclSyntax) {
if
configuration.ignoreMultilineTypeHeaders,
hasMultilinePredecessors(node.memberBlock, keyword: node.actorKeyword)
{
if configuration.ignoreMultilineTypeHeaders,
hasMultilinePredecessors(node.memberBlock, keyword: node.actorKeyword) {
return
}

super.visitPost(node)
}

override func visitPost(_ node: ClassDeclSyntax) {
if
configuration.ignoreMultilineTypeHeaders,
hasMultilinePredecessors(node.memberBlock, keyword: node.classKeyword)
{
if configuration.ignoreMultilineTypeHeaders,
hasMultilinePredecessors(node.memberBlock, keyword: node.classKeyword) {
return
}

super.visitPost(node)
}

override func visitPost(_ node: EnumDeclSyntax) {
if
configuration.ignoreMultilineTypeHeaders,
hasMultilinePredecessors(node.memberBlock, keyword: node.enumKeyword)
{
if configuration.ignoreMultilineTypeHeaders,
hasMultilinePredecessors(node.memberBlock, keyword: node.enumKeyword) {
return
}

super.visitPost(node)
}

override func visitPost(_ node: ExtensionDeclSyntax) {
if
configuration.ignoreMultilineTypeHeaders,
hasMultilinePredecessors(node.memberBlock, keyword: node.extensionKeyword)
{
if configuration.ignoreMultilineTypeHeaders,
hasMultilinePredecessors(node.memberBlock, keyword: node.extensionKeyword) {
return
}

super.visitPost(node)
}

override func visitPost(_ node: ProtocolDeclSyntax) {
if
configuration.ignoreMultilineTypeHeaders,
hasMultilinePredecessors(node.memberBlock, keyword: node.protocolKeyword)
{
if configuration.ignoreMultilineTypeHeaders,
hasMultilinePredecessors(node.memberBlock, keyword: node.protocolKeyword) {
return
}

super.visitPost(node)
}

override func visitPost(_ node: StructDeclSyntax) {
if
configuration.ignoreMultilineTypeHeaders,
hasMultilinePredecessors(node.memberBlock, keyword: node.structKeyword)
{
if configuration.ignoreMultilineTypeHeaders,
hasMultilinePredecessors(node.memberBlock, keyword: node.structKeyword) {
return
}

Expand All @@ -96,63 +84,59 @@ private extension OpeningBraceRule {
// MARK: - Conditional Statements

override func visitPost(_ node: ForStmtSyntax) {
if
configuration.ignoreMultilineStatementConditions,
hasMultilinePredecessors(node.body, keyword: node.forKeyword)
{
if configuration.ignoreMultilineStatementConditions,
hasMultilinePredecessors(node.body, keyword: node.forKeyword) {
return
}

super.visitPost(node)
}

override func visitPost(_ node: IfExprSyntax) {
if
configuration.ignoreMultilineStatementConditions,
hasMultilinePredecessors(node.body, keyword: node.ifKeyword)
{
if configuration.ignoreMultilineStatementConditions,
hasMultilinePredecessors(node.body, keyword: node.ifKeyword) {
return
}

super.visitPost(node)
}

override func visitPost(_ node: WhileStmtSyntax) {
if
configuration.ignoreMultilineStatementConditions,
hasMultilinePredecessors(node.body, keyword: node.whileKeyword)
{
if configuration.ignoreMultilineStatementConditions,
hasMultilinePredecessors(node.body, keyword: node.whileKeyword) {
return
}

super.visitPost(node)
}


// MARK: - Functions and Initializers

override func visitPost(_ node: FunctionDeclSyntax) {
guard let body = node.body else {
return
}
if configuration.allowMultilineFunc, hasMultilinePredecessors(body, keyword: node.funcKeyword) {
if let body = node.body,
configuration.shouldIgnoreMultilineFunctionSignatures,
hasMultilinePredecessors(body, keyword: node.funcKeyword) {
return
}

super.visitPost(node)
}

override func visitPost(_ node: InitializerDeclSyntax) {
guard let body = node.body else {
return
}
if configuration.allowMultilineFunc, hasMultilinePredecessors(body, keyword: node.initKeyword) {
if let body = node.body,
configuration.shouldIgnoreMultilineFunctionSignatures,
hasMultilinePredecessors(body, keyword: node.initKeyword) {
return
}

super.visitPost(node)
}

// MARK: - Auxiliar
// MARK: - Other Methods

/// Checks if a `BracedSyntax` has a multiline predecessor.
/// For type declarations, the predecessor is the header. For conditional statements,
/// it is the condition list, and for functions, it is the signature.
private func hasMultilinePredecessors(_ body: some BracedSyntax, keyword: TokenSyntax) -> Bool {
guard let endToken = body.previousToken(viewMode: .sourceAccurate) else {
return false
Expand Down
3 changes: 3 additions & 0 deletions Tests/IntegrationTests/default_rule_configurations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,9 @@ one_declaration_per_file:
severity: warning
opening_brace:
severity: warning
ignore_multiline_type_headers: false
ignore_multiline_statement_conditions: false
ignore_multiline_function_signatures: false
allow_multiline_func: false
operator_usage_whitespace:
severity: warning
Expand Down
Loading

0 comments on commit 3e549c3

Please sign in to comment.