Skip to content

Commit

Permalink
Add ignore_multiline_condition_statements option for opening_brace rule
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardosrodrigues0 committed Apr 8, 2024
1 parent 8d055ad commit c970554
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ struct OpeningBraceConfiguration: SeverityBasedRuleConfiguration {
private(set) var severityConfiguration = SeverityConfiguration<Parent>(.warning)
@ConfigurationElement(key: "allow_multiline_func")
private(set) var allowMultilineFunc = false
@ConfigurationElement(key: "ignore_multiline_condition_statements")
private(set) var ignoreMultilineConditionStatement = true
}
53 changes: 45 additions & 8 deletions Source/SwiftLintBuiltInRules/Rules/Style/OpeningBraceRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ private extension OpeningBraceRule {

override func visitPost(_ node: CatchClauseSyntax) {
let body = node.body
if isIgnoredMultilineConditionStatement(body, keywordToken: node.catchKeyword) {
return
}
if let correction = body.violationCorrection(locationConverter) {
violations.append(body.openingPosition)
violationCorrections.append(correction)
Expand All @@ -77,6 +80,9 @@ private extension OpeningBraceRule {

override func visitPost(_ node: DeferStmtSyntax) {
let body = node.body
if isIgnoredMultilineConditionStatement(body, keywordToken: node.deferKeyword) {
return
}
if let correction = body.violationCorrection(locationConverter) {
violations.append(body.openingPosition)
violationCorrections.append(correction)
Expand All @@ -85,6 +91,9 @@ private extension OpeningBraceRule {

override func visitPost(_ node: DoStmtSyntax) {
let body = node.body
if isIgnoredMultilineConditionStatement(body, keywordToken: node.doKeyword) {
return
}
if let correction = body.violationCorrection(locationConverter) {
violations.append(body.openingPosition)
violationCorrections.append(correction)
Expand All @@ -93,6 +102,9 @@ private extension OpeningBraceRule {

override func visitPost(_ node: ForStmtSyntax) {
let body = node.body
if isIgnoredMultilineConditionStatement(body, keywordToken: node.forKeyword) {
return
}
if let correction = body.violationCorrection(locationConverter) {
violations.append(body.openingPosition)
violationCorrections.append(correction)
Expand All @@ -101,6 +113,9 @@ private extension OpeningBraceRule {

override func visitPost(_ node: GuardStmtSyntax) {
let body = node.body
if isIgnoredMultilineConditionStatement(body, keywordToken: node.guardKeyword) {
return
}
if let correction = body.violationCorrection(locationConverter) {
violations.append(body.openingPosition)
violationCorrections.append(correction)
Expand All @@ -109,7 +124,8 @@ private extension OpeningBraceRule {

override func visitPost(_ node: IfExprSyntax) {
let body = node.body
if let correction = body.violationCorrection(locationConverter) {
if !isIgnoredMultilineConditionStatement(body, keywordToken: node.ifKeyword),
let correction = body.violationCorrection(locationConverter) {
violations.append(body.openingPosition)
violationCorrections.append(correction)
}
Expand All @@ -121,6 +137,9 @@ private extension OpeningBraceRule {

override func visitPost(_ node: RepeatStmtSyntax) {
let body = node.body
if isIgnoredMultilineConditionStatement(body, keywordToken: node.repeatKeyword) {
return
}
if let correction = body.violationCorrection(locationConverter) {
violations.append(body.openingPosition)
violationCorrections.append(correction)
Expand All @@ -129,6 +148,9 @@ private extension OpeningBraceRule {

override func visitPost(_ node: WhileStmtSyntax) {
let body = node.body
if isIgnoredMultilineConditionStatement(body, keywordToken: node.whileKeyword) {
return
}
if let correction = body.violationCorrection(locationConverter) {
violations.append(body.openingPosition)
violationCorrections.append(correction)
Expand Down Expand Up @@ -184,7 +206,7 @@ private extension OpeningBraceRule {
guard let body = node.body else {
return
}
if configuration.allowMultilineFunc, refersToMultilineFunction(body, functionIndicator: node.funcKeyword) {
if isIgnoredMultilineFunc(body, keywordToken: node.funcKeyword) {
return
}
if let correction = body.violationCorrection(locationConverter) {
Expand All @@ -197,7 +219,7 @@ private extension OpeningBraceRule {
guard let body = node.body else {
return
}
if configuration.allowMultilineFunc, refersToMultilineFunction(body, functionIndicator: node.initKeyword) {
if isIgnoredMultilineFunc(body, keywordToken: node.initKeyword) {
return
}
if let correction = body.violationCorrection(locationConverter) {
Expand All @@ -206,14 +228,29 @@ private extension OpeningBraceRule {
}
}

private func refersToMultilineFunction(_ body: CodeBlockSyntax, functionIndicator: TokenSyntax) -> Bool {
private func isIgnoredMultilineFunc(_ body: CodeBlockSyntax, keywordToken: TokenSyntax) -> Bool {
guard configuration.allowMultilineFunc else {
return false
}

return isBodyPredecessorMultiline(body, keywordToken: keywordToken)
}

private func isIgnoredMultilineConditionStatement(_ body: CodeBlockSyntax, keywordToken: TokenSyntax) -> Bool {
guard configuration.ignoreMultilineConditionStatement else {
return false
}

return isBodyPredecessorMultiline(body, keywordToken: keywordToken)
}

private func isBodyPredecessorMultiline(_ body: CodeBlockSyntax, keywordToken: TokenSyntax) -> Bool {
guard let endToken = body.previousToken(viewMode: .sourceAccurate) else {
return false
}
let startLocation = functionIndicator.endLocation(converter: locationConverter)
let endLocation = endToken.endLocation(converter: locationConverter)
let braceLocation = body.leftBrace.endLocation(converter: locationConverter)
return startLocation.line != endLocation.line && endLocation.line != braceLocation.line
let keywordEndLocation = keywordToken.endLocation(converter: locationConverter)
let bodyPredecessorEndLocation = endToken.endLocation(converter: locationConverter)
return keywordEndLocation.line != bodyPredecessorEndLocation.line
}
}
}
Expand Down
57 changes: 55 additions & 2 deletions Tests/SwiftLintFrameworkTests/OpeningBraceRuleTests.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@testable import SwiftLintBuiltInRules

class OpeningBraceRuleTests: SwiftLintTestCase {
func testDefaultExamplesRunInMultilineMode() {
func testDefaultExamplesRunInMultilineFuncMode() {
let description = OpeningBraceRule.description
.with(triggeringExamples: OpeningBraceRule.description.triggeringExamples.removing([
Example("func abc(a: A,\n\tb: B)\n↓{"),
Expand All @@ -19,7 +19,7 @@ class OpeningBraceRuleTests: SwiftLintTestCase {
}

// swiftlint:disable:next function_body_length
func testWithAllowMultilineTrue() {
func testWithAllowMultilineFuncTrue() {
let nonTriggeringExamples = [
Example("""
func abc(
Expand Down Expand Up @@ -89,6 +89,59 @@ class OpeningBraceRuleTests: SwiftLintTestCase {

verifyRule(description, ruleConfiguration: ["allow_multiline_func": true])
}

func testWithIgnoreMultilineConditionStatementsTrue() {
let nonTriggeringExamples = OpeningBraceRule.description.nonTriggeringExamples + [
Example("""
if x,
y
{
} else if z,
w
{
}
"""),
Example("""
while
x,
y
{}
""")
]

let triggeringExamples = [
Example("""
if x, y
{
}
"""),
Example("""
if
x,
y
{
} else if z
{
}
"""),
Example("""
while x, y
{}
""")
]

let description = OpeningBraceRule.description
.with(nonTriggeringExamples: nonTriggeringExamples)
.with(triggeringExamples: triggeringExamples)
.with(corrections: [:])

verifyRule(description, ruleConfiguration: ["ignore_multiline_condition_statements": true])
}
}

private extension Array where Element == Example {
Expand Down

0 comments on commit c970554

Please sign in to comment.