Skip to content

Commit

Permalink
Add Mark rule for triple slash Mark comments (#2868)
Browse files Browse the repository at this point in the history
* Add Mark rule for triple slash Mark comments

* Move changelog entry to appropriate section

* Re-add Package.resolved

* Fix trailing comma

* Rebuild Rules.md using Xcode 10.3

* Combine two regexes into one
  • Loading branch information
Nathan Van Fleet authored and jpsim committed Sep 19, 2019
1 parent d5f37ca commit 406a8f2
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
* Make `contains_over_first_not_nil` rule also match `first(where:) == nil`.
[Colton Schlosser](https://github.com/cltnschlosser)

* Add two new cases to the Mark rule to detect a Mark using three slashes.
[nvanfleet](https://github.com/nvanfleet)
[#2866](https://github.com/realm/SwiftLint/issues/2866)

#### Bug Fixes

* None.
Expand Down
8 changes: 8 additions & 0 deletions Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -12062,6 +12062,14 @@ MARK comment should be in valid format. e.g. '// MARK: ...' or '// MARK: - ...'
↓// MARKK -
```

```swift
↓/// MARK:
```

```swift
↓/// MARK bad
```

```swift
↓//MARK:- Top-Level bad mark
↓//MARK:- Another bad mark
Expand Down
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Models/MasterRuleList.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated using Sourcery 0.16.2 — https://github.com/krzysztofzablocki/Sourcery
// Generated using Sourcery 0.17.0 — https://github.com/krzysztofzablocki/Sourcery
// DO NOT EDIT

public let masterRuleList = RuleList(rules: [
Expand Down
17 changes: 15 additions & 2 deletions Source/SwiftLintFramework/Rules/Lint/MarkRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public struct MarkRule: CorrectableRule, ConfigurationProviderRule {
"↓// MARKL:",
"↓// MARKR ",
"↓// MARKK -",
"↓/// MARK:",
"↓/// MARK bad",
issue1029Example
],
corrections: [
Expand All @@ -60,6 +62,8 @@ public struct MarkRule: CorrectableRule, ConfigurationProviderRule {
"↓// MARKL: -": "// MARK: -",
"↓// MARKK ": "// MARK: ",
"↓// MARKK -": "// MARK: -",
"↓/// MARK:": "// MARK:",
"↓/// MARK comment": "// MARK: comment",
issue1029Example: issue1029Correction
]
)
Expand All @@ -83,14 +87,16 @@ public struct MarkRule: CorrectableRule, ConfigurationProviderRule {
private let oneOrMoreSpacesBeforeColonPattern = "(?:// ?MARK +:)"
private let nonWhitespaceBeforeColonPattern = "(?:// ?MARK\\S+:)"
private let nonWhitespaceNorColonBeforeSpacesPattern = "(?:// ?MARK[^\\s:]* +)"
private let threeSlashesInsteadOfTwo = "/// MARK:?"

private var pattern: String {
return [
spaceStartPattern,
invalidEndSpacesPattern,
invalidSpacesAfterHyphenPattern,
invalidLowercasePattern,
missingColonPattern
missingColonPattern,
threeSlashesInsteadOfTwo
].joined(separator: "|")
}

Expand Down Expand Up @@ -146,6 +152,10 @@ public struct MarkRule: CorrectableRule, ConfigurationProviderRule {
pattern: invalidLowercasePattern,
replaceString: "// MARK:"))

result.append(contentsOf: correct(file: file,
pattern: threeSlashesInsteadOfTwo,
replaceString: "// MARK:"))

return result.unique
}

Expand Down Expand Up @@ -175,7 +185,10 @@ public struct MarkRule: CorrectableRule, ConfigurationProviderRule {
private func violationRanges(in file: File, matching pattern: String) -> [NSRange] {
let nsstring = file.contents.bridge()
return file.rangesAndTokens(matching: pattern).filter { _, syntaxTokens in
return !syntaxTokens.isEmpty && SyntaxKind(rawValue: syntaxTokens[0].type) == .comment
guard let syntaxKind = SyntaxKind(rawValue: syntaxTokens[0].type) else {
return false
}
return !syntaxTokens.isEmpty && SyntaxKind.commentKinds.contains(syntaxKind)
}.compactMap { range, syntaxTokens in
let identifierRange = nsstring
.byteRangeToNSRange(start: syntaxTokens[0].offset, length: 0)
Expand Down
2 changes: 1 addition & 1 deletion Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated using Sourcery 0.16.2 — https://github.com/krzysztofzablocki/Sourcery
// Generated using Sourcery 0.17.0 — https://github.com/krzysztofzablocki/Sourcery
// DO NOT EDIT

@testable import SwiftLintFrameworkTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated using Sourcery 0.16.2 — https://github.com/krzysztofzablocki/Sourcery
// Generated using Sourcery 0.17.0 — https://github.com/krzysztofzablocki/Sourcery
// DO NOT EDIT

import SwiftLintFramework
Expand Down

0 comments on commit 406a8f2

Please sign in to comment.