Skip to content

Commit

Permalink
Find the correct case colon.
Browse files Browse the repository at this point in the history
  • Loading branch information
dabelknap committed Jun 11, 2018
1 parent d2981d7 commit 723a1f9
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 1 deletion.
88 changes: 88 additions & 0 deletions Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -9995,6 +9995,51 @@ UIView.animate(withDuration: 1.0) {
}
```
```swift
switch myvar {
case MyFunc(x: [1, 2, YourFunc(a: 23)], y: 2):
var three = 3
fallthrough
default:
var three = 4
}
```
```swift
switch myvar {
case .alpha:
var one = 1
case .beta:
var three = 3
fallthrough
default:
var four = 4
}
```
```swift
let aPoint = (1, -1)
switch aPoint {
case let (x, y) where x == y:
let A = "A"
case let (x, y) where x == -y:
let B = "B"
fallthrough
default:
let C = "C"
}
```
```swift
switch myvar {
case MyFun(with: { $1 }):
let one = 1
fallthrough
case "abc":
let two = 2
}
```
</details>
<details>
<summary>Triggering Examples</summary>
Expand All @@ -10011,6 +10056,49 @@ UIView.animate(withDuration: 1.0, animations: {
}
```
```swift
switch myvar {
case MyFunc(x: [1, 2, YourFunc(a: 23)], y: 2):
↓fallthrough
default:
var three = 4
}
```
```swift
switch myvar {
case .alpha:
var one = 1
case .beta:
↓fallthrough
case .gamma:
var three = 3
default:
var four = 4
}
```
```swift
let aPoint = (1, -1)
switch aPoint {
case let (x, y) where x == y:
let A = "A"
case let (x, y) where x == -y:
↓fallthrough
default:
let B = "B"
}
```
```swift
switch myvar {
case MyFun(with: { $1 }):
↓fallthrough
case "abc":
let two = 2
}
```
</details>
Expand Down
115 changes: 114 additions & 1 deletion Source/SwiftLintFramework/Rules/NoFallthroughOnlyRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,47 @@ public struct NoFallthroughOnlyRule: ASTRule, ConfigurationProviderRule {
// comment
var two = 2
}
""",
"""
switch myvar {
case MyFunc(x: [1, 2, YourFunc(a: 23)], y: 2):
var three = 3
fallthrough
default:
var three = 4
}
""",
"""
switch myvar {
case .alpha:
var one = 1
case .beta:
var three = 3
fallthrough
default:
var four = 4
}
""",
"""
let aPoint = (1, -1)
switch aPoint {
case let (x, y) where x == y:
let A = "A"
case let (x, y) where x == -y:
let B = "B"
fallthrough
default:
let C = "C"
}
""",
"""
switch myvar {
case MyFun(with: { $1 }):
let one = 1
fallthrough
case "abc":
let two = 2
}
"""
],
triggeringExamples: [
Expand Down Expand Up @@ -75,6 +116,45 @@ public struct NoFallthroughOnlyRule: ASTRule, ConfigurationProviderRule {
case 2:
var a = 2
}
""",
"""
switch myvar {
case MyFunc(x: [1, 2, YourFunc(a: 23)], y: 2):
↓fallthrough
default:
var three = 4
}
""",
"""
switch myvar {
case .alpha:
var one = 1
case .beta:
↓fallthrough
case .gamma:
var three = 3
default:
var four = 4
}
""",
"""
let aPoint = (1, -1)
switch aPoint {
case let (x, y) where x == y:
let A = "A"
case let (x, y) where x == -y:
↓fallthrough
default:
let B = "B"
}
""",
"""
switch myvar {
case MyFun(with: { $1 }):
↓fallthrough
case "abc":
let two = 2
}
"""
]
)
Expand All @@ -88,7 +168,7 @@ public struct NoFallthroughOnlyRule: ASTRule, ConfigurationProviderRule {
let offset = dictionary.offset,
case let nsstring = file.contents.bridge(),
let range = nsstring.byteRangeToNSRange(start: offset, length: length),
let colonLocation = file.match(pattern: ":", range: range).first?.0.location
let colonLocation = findCaseColon(text: nsstring, range: range)
else {
return []
}
Expand All @@ -112,4 +192,37 @@ public struct NoFallthroughOnlyRule: ASTRule, ConfigurationProviderRule {

return []
}

// Find the first colon that exists outside of all enclosing delimiters
private func findCaseColon(text: NSString, range: NSRange) -> Int? {
var nParen = 0
var nBrace = 0
var nBrack = 0
for index in range.location..<(range.location + range.length) {
let char = text.substring(with: NSRange(location: index, length: 1))
if char == "(" {
nParen += 1
}
if char == ")" {
nParen -= 1
}
if char == "[" {
nBrack += 1
}
if char == "]" {
nBrack -= 1
}
if char == "{" {
nBrace += 1
}
if char == "}" {
nBrace -= 1
}

if nParen == 0 && nBrack == 0 && nBrace == 0 && char == ":" {
return index
}
}
return nil
}
}

0 comments on commit 723a1f9

Please sign in to comment.