@kamaal111/kamaal-quality-config 0.11.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,12 +210,18 @@ 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 five checks are enabled:
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.
217
217
  - `view_body_declarations`: move local `let` and `var` declarations out of SwiftUI
218
218
  `var body: some View`, including nested builder closures. Conditional bindings remain allowed.
219
+ - `private_setter_access`: require `private(set)` wherever a setter has explicit access control.
220
+ For example, `public internal(set) var value` reports `internal`. Both `public var value` and
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.
219
225
  - `no_nested_do_catch`: reject a `do/catch` nested anywhere in another `do/catch`,
220
226
  including its catch clauses. Plain `do` blocks do not count, but do not hide nesting.
221
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.11.0",
3
+ "version": "0.13.0",
4
4
  "description": "Kamaal Farah's shared quality configurations.",
5
5
  "keywords": [
6
6
  "lint",
@@ -9,6 +9,8 @@ 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"
13
+ case privateSetterAccess = "private_setter_access"
12
14
  case singleTryPerDoCatch = "single_try_per_do_catch"
13
15
  case viewBodyDeclarations = "view_body_declarations"
14
16
 
@@ -20,6 +22,10 @@ enum LintRule: String {
20
22
  "Place each chained call on its own line in a multiline expression."
21
23
  case .noNestedDoCatch:
22
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."
27
+ case .privateSetterAccess:
28
+ "Use private(set) for explicit setter access."
23
29
  case .singleTryPerDoCatch:
24
30
  """
25
31
  Keep at most one plain try expression in each do/catch body. \
@@ -0,0 +1,14 @@
1
+ import SwiftSyntax
2
+
3
+ final class PrivateSetterAccessVisitor: SyntaxVisitor {
4
+ private(set) var violations: [LintViolation] = []
5
+
6
+ override func visit(_ node: DeclModifierSyntax) -> SyntaxVisitorContinueKind {
7
+ if node.detail?.detail.text == "set", node.name.tokenKind != .keyword(.private) {
8
+ violations.append(
9
+ LintViolation(position: node.name.positionAfterSkippingLeadingTrivia, rule: .privateSetterAccess)
10
+ )
11
+ }
12
+ return .visitChildren
13
+ }
14
+ }
@@ -18,7 +18,15 @@ enum SwiftLint {
18
18
  tries.walk(tree)
19
19
  let handlers = NoNestedDoCatchVisitor(viewMode: .sourceAccurate)
20
20
  handlers.walk(tree)
21
- return (visitor.violations + chains.violations + bodies.violations + tries.violations + handlers.violations)
21
+ let setters = PrivateSetterAccessVisitor(viewMode: .sourceAccurate)
22
+ setters.walk(tree)
23
+ let switchAssignments = SwitchAssignmentVisitor(viewMode: .sourceAccurate)
24
+ switchAssignments.walk(tree)
25
+ let violations =
26
+ visitor.violations + chains.violations + bodies.violations + tries.violations
27
+ + handlers.violations + setters.violations + switchAssignments.violations
28
+ return
29
+ violations
22
30
  .sorted { $0.position < $1.position }
23
31
  .map { violation in
24
32
  let location = converter.location(for: violation.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,33 @@
1
+ import Testing
2
+
3
+ @testable import SwiftLintChecks
4
+
5
+ struct PrivateSetterAccessTests {
6
+ @Test(arguments: ["fileprivate", "internal", "package"])
7
+ func `Reports the explicit setter modifier`(access: String) {
8
+ let source = """
9
+ public struct Example {
10
+ public \(access)(set) var value = 1
11
+ }
12
+ """
13
+ let diagnostics = SwiftLint.diagnostics(source: source, path: "Example.swift")
14
+ #expect(
15
+ diagnostics == [
16
+ "Example.swift:2:12: error: Use private(set) for explicit setter access. [private_setter_access]"
17
+ ]
18
+ )
19
+ }
20
+
21
+ @Test
22
+ func `Allows private setters and declarations without setter modifiers`() {
23
+ let source = """
24
+ public struct Example {
25
+ public private(set) var restricted = 1
26
+ public var unrestricted = 2
27
+ private var hidden = 3
28
+ }
29
+ """
30
+ let diagnostics = SwiftLint.diagnostics(source: source, path: "Example.swift")
31
+ #expect(diagnostics.isEmpty)
32
+ }
33
+ }
@@ -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
+ }