Skip to content

Commit

Permalink
Rewrite SyntacticSugarRule with SwiftSyntax
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulTaykalo committed Mar 21, 2022
1 parent 702b36a commit ba4e1bf
Show file tree
Hide file tree
Showing 5 changed files with 395 additions and 109 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@
#### Experimental

* None.
* Fix incorrect autocorrection in `syntactic_sugar` rule
[Paul Taykalo](https://github.com/PaulTaykalo)
[#3866](https://github.com/realm/SwiftLint/issues/3866)

#### Enhancements

Expand Down
14 changes: 13 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ $ xcodebuild -scheme swiftlint test
$ swift test
$ make docker_test
```

## Rules

New rules should be added in the `Source/SwiftLintFramework/Rules` directory.
Expand All @@ -72,6 +71,19 @@ over time. This way adding a unit test for your new Rule is just a matter of
adding a test case in `RulesTests.swift` which simply calls
`verifyRule(YourNewRule.description)`.

For debugging purposes examples can be marked as `focused`. If there are any
focused examples found, then only those will be run when running tests.
```
nonTriggeringExamples: [
Example("let x: [Int]"),
Example("let x: [Int: String]").focused() // only this one will be run in tests
],
triggeringExamples: [
Example("let x: ↓Array<String>"),
Example("let x: ↓Dictionary<Int, String>")
]
```

### `ConfigurationProviderRule`

If your rule supports user-configurable options via `.swiftlint.yml`, you can
Expand Down
25 changes: 25 additions & 0 deletions Source/SwiftLintFramework/Extensions/StringView+SwiftSyntax.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Foundation
import SourceKittenFramework
import SwiftSyntax

extension StringView {
/// Converts two absolute positions from SwiftSyntax to valid NSRange if possible
/// - Parameters:
/// - start: starting poisiition
/// - end: end position
/// - Returns: NSRange or nil in case of empty string
func NSRange(start: AbsolutePosition, end: AbsolutePosition) -> NSRange? {
precondition(end.utf8Offset >= start.utf8Offset, "End position should be beigger than start position")
return NSRange(start: start, length: end.utf8Offset - start.utf8Offset)
}

/// Converts absolute position with length from SwiftSyntax to valid NSRange if possible
/// - Parameters:
/// - start: starting position
/// - length: length in bytes
/// - Returns: NSRange or nil in case of empty string
private func NSRange(start: AbsolutePosition, length: Int) -> NSRange? {
let byteRange = ByteRange(location: ByteCount(start.utf8Offset), length: ByteCount(length))
return byteRangeToNSRange(byteRange)
}
}
Loading

0 comments on commit ba4e1bf

Please sign in to comment.