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 committed Feb 5, 2022
1 parent c09b6fe commit 6326199
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions Source/SwiftLintFramework/Rules/Lint/MarkRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ public struct MarkRule: CorrectableRule, ConfigurationProviderRule {
public static let description = RuleDescription(
identifier: "mark",
name: "Mark",
description: "MARK comment should be in valid format. e.g. '// MARK: ...' or '// MARK: - ...'",
description: "MARK comment should be in valid format. e.g. '// MARK: ...' or '// MARK: - ...'; MARK in multiline comments will be ignored.",
kind: .lint,
nonTriggeringExamples: [
Example("// MARK: good\n"),
Example("// MARK: - good\n"),
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 6326199

Please sign in to comment.