Skip to content

Commit

Permalink
Fix identifier_name rule when using Swift 4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelofabri authored and sjavora committed Mar 9, 2019
1 parent 8aa4813 commit 870e046
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
* Fix `explicit_enum_raw_value` rule when linting with Swift 4.2.
[Marcelo Fabri](https://github.com/marcelofabri)

* Fix `identifier_name` rule false positives with `enum` when linting
using Swift 4.2.
[Marcelo Fabri](https://github.com/marcelofabri)
[Jacob Greenfield](https://github.com/Coder-256)
[#2231](https://github.com/realm/SwiftLint/issues/2231)

## 0.26.0: Maytagged Pointers

#### Breaking
Expand Down
8 changes: 8 additions & 0 deletions Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -7159,6 +7159,14 @@ func == (lhs: SyntaxToken, rhs: SyntaxToken) -> Bool
override func IsOperator(name: String) -> Bool
```

```swift
enum Foo { case `private` }
```

```swift
enum Foo { case value(String) }
```

</details>
<details>
<summary>Triggering Examples</summary>
Expand Down
10 changes: 9 additions & 1 deletion Source/SwiftLintFramework/Rules/IdentifierNameRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,21 @@ public struct IdentifierNameRule: ASTRule, ConfigurationProviderRule {

private func validateName(dictionary: [String: SourceKitRepresentable],
kind: SwiftDeclarationKind) -> (name: String, offset: Int)? {
guard let name = dictionary.name,
guard var name = dictionary.name,
let offset = dictionary.offset,
kinds.contains(kind),
!name.hasPrefix("$") else {
return nil
}

if kind == .enumelement,
SwiftVersion.current > .fourDotOne,
let parenIndex = name.index(of: "("),
parenIndex > name.startIndex {
let index = name.index(before: parenIndex)
name = String(name[...index])
}

return (name.nameStrippingLeadingUnderscoreIfPrivate(dictionary), offset)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ internal struct IdentifierNameRuleExamples {
"func isOperator(name: String) -> Bool",
"func typeForKind(_ kind: SwiftDeclarationKind) -> String",
"func == (lhs: SyntaxToken, rhs: SyntaxToken) -> Bool",
"override func IsOperator(name: String) -> Bool"
"override func IsOperator(name: String) -> Bool",
"enum Foo { case `private` }",
"enum Foo { case value(String) }"
]

static let triggeringExamples = [
Expand Down

0 comments on commit 870e046

Please sign in to comment.