@kamaal111/kamaal-quality-config 0.12.0 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -210,7 +210,7 @@ Without paths, the command checks the current directory recursively. Explicit fi
210
210
  paths are relative to your working directory. Recursive scanning skips hidden paths (including
211
211
  `.build`), `node_modules`, symlinks, and non-Swift files. Use `--` before paths starting with a dash.
212
212
 
213
- All six checks run whenever you invoke `kqc swift-lint`:
213
+ All seven checks run whenever you invoke `kqc swift-lint`:
214
214
 
215
215
  - `member_spacing`: separate methods and initializers from adjacent members with a blank line.
216
216
  - `multiline_chain_spacing`: put each chained call on its own line in multiline expressions.
@@ -219,6 +219,9 @@ All six checks run whenever you invoke `kqc swift-lint`:
219
219
  - `private_setter_access`: require `private(set)` wherever a setter has explicit access control.
220
220
  For example, `public internal(set) var value` reports `internal`. Both `public var value` and
221
221
  `public private(set) var value` pass the check.
222
+ - `prefer_switch_expression`: assign the result of a `switch` expression once when every case
223
+ contains a single plain assignment to the same name, `self`, or `self` property. The check skips
224
+ conditional case lists and targets shadowed by a case binding.
222
225
  - `no_nested_do_catch`: reject a `do/catch` nested anywhere in another `do/catch`,
223
226
  including its catch clauses. Plain `do` blocks do not count, but do not hide nesting.
224
227
  Functions, closures, initializers, accessors, and nested types start independent scopes;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kamaal111/kamaal-quality-config",
3
- "version": "0.12.0",
3
+ "version": "0.13.0",
4
4
  "description": "Kamaal Farah's shared quality configurations.",
5
5
  "keywords": [
6
6
  "lint",
@@ -9,6 +9,7 @@ enum LintRule: String {
9
9
  case memberSpacing = "member_spacing"
10
10
  case multilineChainSpacing = "multiline_chain_spacing"
11
11
  case noNestedDoCatch = "no_nested_do_catch"
12
+ case preferSwitchExpression = "prefer_switch_expression"
12
13
  case privateSetterAccess = "private_setter_access"
13
14
  case singleTryPerDoCatch = "single_try_per_do_catch"
14
15
  case viewBodyDeclarations = "view_body_declarations"
@@ -21,6 +22,8 @@ enum LintRule: String {
21
22
  "Place each chained call on its own line in a multiline expression."
22
23
  case .noNestedDoCatch:
23
24
  "Avoid nested do/catch statements. Extract error handling into a helper or use sequential do/catch blocks."
25
+ case .preferSwitchExpression:
26
+ "Assign the result of the switch expression once."
24
27
  case .privateSetterAccess:
25
28
  "Use private(set) for explicit setter access."
26
29
  case .singleTryPerDoCatch:
@@ -20,9 +20,11 @@ enum SwiftLint {
20
20
  handlers.walk(tree)
21
21
  let setters = PrivateSetterAccessVisitor(viewMode: .sourceAccurate)
22
22
  setters.walk(tree)
23
+ let switchAssignments = SwitchAssignmentVisitor(viewMode: .sourceAccurate)
24
+ switchAssignments.walk(tree)
23
25
  let violations =
24
26
  visitor.violations + chains.violations + bodies.violations + tries.violations
25
- + handlers.violations + setters.violations
27
+ + handlers.violations + setters.violations + switchAssignments.violations
26
28
  return
27
29
  violations
28
30
  .sorted { $0.position < $1.position }
@@ -0,0 +1,64 @@
1
+ import SwiftSyntax
2
+
3
+ final class SwitchAssignmentVisitor: SyntaxVisitor {
4
+ private(set) var violations: [LintViolation] = []
5
+
6
+ override func visit(_ node: SwitchExprSyntax) -> SyntaxVisitorContinueKind {
7
+ guard node.parent?.is(ExpressionStmtSyntax.self) == true,
8
+ let firstCase = node.cases.first,
9
+ let target = Self.assignmentTarget(in: firstCase),
10
+ node.cases.dropFirst().allSatisfy({ Self.assignmentTarget(in: $0) == target })
11
+ else { return .visitChildren }
12
+
13
+ violations.append(
14
+ LintViolation(
15
+ position: node.switchKeyword.positionAfterSkippingLeadingTrivia,
16
+ rule: .preferSwitchExpression
17
+ )
18
+ )
19
+ return .visitChildren
20
+ }
21
+
22
+ private static func assignmentTarget(in element: SwitchCaseListSyntax.Element) -> String? {
23
+ guard let branch = element.as(SwitchCaseSyntax.self),
24
+ branch.statements.count == 1,
25
+ let expression = branch.statements.first?.item.as(SequenceExprSyntax.self)
26
+ else { return nil }
27
+
28
+ let operands = expression.elements
29
+ guard operands.count >= 3,
30
+ let target = operands.first,
31
+ operands.dropFirst().first?.is(AssignmentExprSyntax.self) == true,
32
+ !operands.dropFirst(2).contains(where: { $0.is(AssignmentExprSyntax.self) })
33
+ else { return nil }
34
+
35
+ if let reference = target.as(DeclReferenceExprSyntax.self) {
36
+ let name = reference.baseName.text
37
+ let bindings = CaseBindingVisitor(targetName: name)
38
+ bindings.walk(branch.label)
39
+ return bindings.shadowsTarget ? nil : name
40
+ }
41
+
42
+ guard let member = target.as(MemberAccessExprSyntax.self),
43
+ member.base?.as(DeclReferenceExprSyntax.self)?.baseName.tokenKind == .keyword(.self)
44
+ else { return nil }
45
+ return "self.\(member.declName.baseName.text)"
46
+ }
47
+ }
48
+
49
+ private final class CaseBindingVisitor: SyntaxVisitor {
50
+ private let targetName: String
51
+ private(set) var shadowsTarget = false
52
+
53
+ init(targetName: String) {
54
+ self.targetName = targetName
55
+ super.init(viewMode: .sourceAccurate)
56
+ }
57
+
58
+ override func visit(_ node: IdentifierPatternSyntax) -> SyntaxVisitorContinueKind {
59
+ if node.identifier.text == targetName {
60
+ shadowsTarget = true
61
+ }
62
+ return .skipChildren
63
+ }
64
+ }
@@ -0,0 +1,37 @@
1
+ import Testing
2
+
3
+ @testable import SwiftLintChecks
4
+
5
+ struct SwitchAssignmentTests {
6
+ @Test(arguments: [
7
+ "switch kind { case .first: result = 1; default: result = 2 }",
8
+ "switch kind { case .first: self.result = 1; default: self.result = 2 }",
9
+ "switch kind { case .first: self = .first; @unknown default: self = .other }",
10
+ "switch number { case .some(let amount): result = amount + 1; case nil: result = 0 }",
11
+ ])
12
+ func `Reports assignments to the same target in every case`(source: String) {
13
+ let diagnostics = SwiftLint.diagnostics(source: "\n \(source)", path: "Example.swift")
14
+ #expect(
15
+ diagnostics == [
16
+ "Example.swift:2:3: error: Assign the result of the switch expression once. [prefer_switch_expression]"
17
+ ]
18
+ )
19
+ }
20
+
21
+ @Test(arguments: [
22
+ "let result = switch kind { case .first: 1; default: 2 }",
23
+ "switch kind { case .first: result = 1; default: other = 2 }",
24
+ "switch kind { case .first: result = 1; default: result = 2; log() }",
25
+ "switch kind { case .first: result = 1; default: return }",
26
+ "switch kind { case .first: result += 1; default: result += 2 }",
27
+ "switch kind { case .first: values[index()] = 1; default: values[index()] = 2 }",
28
+ "switch kind { case .number(var result): result = 1; default: result = 2 }",
29
+ "switch kind { case .first: result = other = 1; default: result = 2 }",
30
+ "switch kind { case .first: use(result); default: use(other) }",
31
+ "switch kind {\n#if os(macOS)\ncase .first: result = 1\n#endif\ndefault: result = 2\n}",
32
+ ])
33
+ func `Allows switches without a common standalone assignment`(source: String) {
34
+ let diagnostics = SwiftLint.diagnostics(source: source, path: "Example.swift")
35
+ #expect(diagnostics.isEmpty)
36
+ }
37
+ }