Skip to content

Commit

Permalink
Ignore MARK in multiline comment
Browse files Browse the repository at this point in the history
  • Loading branch information
goranche authored and jpsim committed Mar 4, 2022
1 parent 7f9f41a commit c65b3b3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
[SimplyDanny](https://github.com/SimplyDanny)
[#3804](https://github.com/realm/SwiftLint/issues/3804)

* Ignore MARK in multiline comment, fixing cases that would previous crash or
produce invalid results when correcting.
[goranche](https://github.com/goranche)
[#1749](https://github.com/realm/SwiftLint/issues/1749)
[#3841](https://github.com/realm/SwiftLint/issues/3841)

## 0.46.3: Detergent Spill

#### Breaking
Expand Down
36 changes: 31 additions & 5 deletions Source/SwiftLintFramework/Rules/Lint/MarkRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public struct MarkRule: CorrectableRule, ConfigurationProviderRule {
Example("// MARK: -\n"),
Example("// BOOKMARK"),
Example("//BOOKMARK"),
Example("// BOOKMARKS")
Example("// BOOKMARKS"),
issue1749Example
],
triggeringExamples: [
Example("↓//MARK: bad"),
Expand Down Expand Up @@ -64,7 +65,8 @@ public struct MarkRule: CorrectableRule, ConfigurationProviderRule {
Example("↓// MARKK -"): Example("// MARK: -"),
Example("↓/// MARK:"): Example("// MARK:"),
Example("↓/// MARK comment"): Example("// MARK: comment"),
issue1029Example: issue1029Correction
issue1029Example: issue1029Correction,
issue1749Example: issue1749Correction
]
)

Expand Down Expand Up @@ -183,11 +185,20 @@ public struct MarkRule: CorrectableRule, ConfigurationProviderRule {
}

private func violationRanges(in file: SwiftLintFile, matching pattern: String) -> [NSRange] {
return file.rangesAndTokens(matching: pattern).filter { _, syntaxTokens in
guard let syntaxKind = syntaxTokens.first?.kind else {
return file.rangesAndTokens(matching: pattern).filter { matchRange, syntaxTokens in
guard let syntaxToken = syntaxTokens.first, let syntaxKind = syntaxToken.kind else {
return false
}
return syntaxTokens.isNotEmpty && SyntaxKind.commentKinds.contains(syntaxKind)
guard SyntaxKind.commentKinds.contains(syntaxKind) else {
return false
}
let tokenLocation = Location(file: file, byteOffset: ByteCount(syntaxToken.offset.value))
let matchLocation = Location(file: file, characterOffset: matchRange.location)
// Skip those MARKs that are part of a multiline comment
guard let tokenLine = tokenLocation.line, let matchLine = matchLocation.line, matchLine == tokenLine else {
return false
}
return true
}.compactMap { range, syntaxTokens in
let byteRange = ByteRange(location: syntaxTokens[0].offset, length: 0)
let identifierRange = file.stringView.byteRangeToNSRange(byteRange)
Expand All @@ -212,6 +223,21 @@ private let issue1029Correction = Example("""
extension MarkTest {}
""")

// https://github.com/realm/SwiftLint/issues/1749
// https://github.com/realm/SwiftLint/issues/3841
private let issue1749Example = Example("""
/*
func test1() {
}
//MARK: mark
func test2() {
}
*/
""")

// This example should not trigger changes
private let issue1749Correction = issue1749Example

// These need to be at the bottom of the file to work around https://bugs.swift.org/browse/SR-10486

private let nonSpace = "[^ ]"
Expand Down

0 comments on commit c65b3b3

Please sign in to comment.