From 2eba1941986fe31ebb9a6c106f400217b35cf081 Mon Sep 17 00:00:00 2001 From: Abdallah Date: Sat, 18 May 2024 00:26:03 +0300 Subject: [PATCH] Add IfSwitchExpressionRule for enforcing if and switch statements. Include non-triggering and triggering examples. Add IfSwitchExpressionRule for enforcing if and switch statements. Include non-triggering and triggering examples. --- .../Rules/Style/IfSwitchExpressionRule.swift | 30 ++++ .../IfSwitchExpressionRuleExcamples.swift | 144 ++++++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 Source/SwiftLintBuiltInRules/Rules/Style/IfSwitchExpressionRule.swift create mode 100644 Source/SwiftLintBuiltInRules/Rules/Style/IfSwitchExpressionRuleExcamples.swift diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/IfSwitchExpressionRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/IfSwitchExpressionRule.swift new file mode 100644 index 0000000000..04b9580329 --- /dev/null +++ b/Source/SwiftLintBuiltInRules/Rules/Style/IfSwitchExpressionRule.swift @@ -0,0 +1,30 @@ +import Foundation +import SwiftSyntax +import SwiftLintCore + + +@SwiftSyntaxRule(explicitRewriter: true) +struct IfSwitchExpressionRule: Rule { + var configuration = SeverityConfiguration(.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 { + override func visit(_ node: IfExprSyntax) -> SyntaxVisitorContinueKind { + violations.append(node.positionAfterSkippingLeadingTrivia) + return .skipChildren + } + override func visit(_ node: SwitchCaseListSyntax) -> SyntaxVisitorContinueKind { + violations.append(node.positionAfterSkippingLeadingTrivia) + return .skipChildren + } + } +} diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/IfSwitchExpressionRuleExcamples.swift b/Source/SwiftLintBuiltInRules/Rules/Style/IfSwitchExpressionRuleExcamples.swift new file mode 100644 index 0000000000..720a3aea4c --- /dev/null +++ b/Source/SwiftLintBuiltInRules/Rules/Style/IfSwitchExpressionRuleExcamples.swift @@ -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" + } + """) + ] +}