Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IfSwitchExpressionRule for enforcing if and switch statements. In… #5595

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Foundation
import SwiftSyntax
import SwiftLintCore


@SwiftSyntaxRule(explicitRewriter: true)
struct IfSwitchExpressionRule: Rule {
var configuration = SeverityConfiguration<Self>(.warning)

static let description = RuleDescription(
identifier: "if_switch_expression",
name: "If and Switch Expression",
description: "Enforce the of if and switch statements when they are allowed ",
kind: .style,
nonTriggeringExamples: IfSwitchExpressionRuleExamples.nonTriggeringExamples,
triggeringExamples: IfSwitchExpressionRuleExamples.triggeringExamples
)
}
private extension IfSwitchExpressionRule {
final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> {
override func visit(_ node: IfExprSyntax) -> SyntaxVisitorContinueKind {
violations.append(node.positionAfterSkippingLeadingTrivia)
return .skipChildren
}
override func visit(_ node: SwitchCaseListSyntax) -> SyntaxVisitorContinueKind {
violations.append(node.positionAfterSkippingLeadingTrivia)
return .skipChildren
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
struct IfSwitchExpressionRuleExamples {
static let nonTriggeringExamples: [Example] = [
Example("let result = if condition { true } else { false }"),
Example("""
func f(cond: Bool) -> Int {
if cond {
// Nothing
} else {
return 2
}
return 1
}
"""),
Example("""
func f(cond: Bool) -> Int {
let result = if cond { 1 } else { 2 }
return result
}
"""),
Example("""
let value = 3
let description = switch value {
case 1 -> "one"
case 2 -> "two"
default -> "other"
}
"""),
Example("""
func g(value: Int) -> String {
return switch value {
case 1 -> "one"
case 2 -> "two"
default -> "other"
}
}
"""),
Example("""
let result = switch number {
case 1 -> "one"
case 2 -> "two"
case 3 -> "three"
default -> "unknown"
}
"""),
Example("""
func calculate(value: Int) -> Int {
return switch value {
case 1 -> 10
case 2 -> 20
case 3 -> 30
default -> 0
}
}
""")
]

static let triggeringExamples: [Example] = [
Example("""
func f(cond: Bool) {
let r: Int
if cond {
r = 1
} else {
r = 2
}
}
"""),
Example("""
func f(cond: Bool) -> Int {
if cond {
return 1
} else {
return 2
}
}
"""),
Example("""
func f(cond: Bool) -> Int {
if cond {
// Nothing
} else {
return 2
}
return 1
}
"""),
Example("""
let result: String
if condition {
result = "yes"
} else {
result = "no"
}
"""),
Example("""
func g(value: Int) -> String {
let result: String
switch value {
case 1:
result = "one"
case 2:
result = "two"
default:
result = "other"
}
return result
}
"""),
Example("""
func calculate(value: Int) -> Int {
if value == 1 {
return 10
} else if value == 2 {
return 20
} else {
return 0
}
}
"""),
Example("""
let result: Int
if value == 1 {
result = 10
} else if value == 2 {
result = 20
} else {
result = 0
}
"""),
Example("""
let description: String
switch number {
case 1:
description = "one"
case 2:
description = "two"
case 3:
description = "three"
default:
description = "unknown"
}
""")
]
}
Loading