Skip to content

Commit 647cd59

Browse files
committed
Switch to a MarkupRewriter instead of using Regex
1 parent 3df47cd commit 647cd59

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

Sources/SwiftFormat/Rules/BeginDocumentationCommentWithOneLineSummary.swift

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import Foundation
1414
import SwiftSyntax
15+
import Markdown
1516

1617
#if os(macOS)
1718
import NaturalLanguage
@@ -91,19 +92,20 @@ public final class BeginDocumentationCommentWithOneLineSummary: SyntaxLintRule {
9192

9293
/// Diagnose documentation comments that don't start with one sentence summary.
9394
private func diagnoseDocComments(in decl: DeclSyntax) {
95+
// Extract the summary from a documentation comment, if it exists, and strip
96+
// out any inline code segments (which shouldn't be considered when looking
97+
// for the end of a sentence).
98+
var inlineCodeRemover = InlineCodeRemover()
9499
guard
95100
let docComment = DocumentationComment(extractedFrom: decl),
96-
let briefSummary = docComment.briefSummary
101+
let briefSummary = docComment.briefSummary,
102+
let noInlineCodeSummary = inlineCodeRemover.visit(briefSummary) as? Paragraph
97103
else { return }
98104

99105
// For the purposes of checking the sentence structure of the comment, we can operate on the
100-
// plain text; we don't need any of the styling. Additionally, the backticks that are
101-
// frequently used to denote symbol names cause issues with quote identification, and
102-
// aren't necessary for this purpose.
103-
let trimmedText = briefSummary.plainText
106+
// plain text; we don't need any of the styling.
107+
let trimmedText = noInlineCodeSummary.plainText
104108
.trimmingCharacters(in: .whitespacesAndNewlines)
105-
.replacingOccurrences(of: "``.+?``", with: "ZZZ", options: .regularExpression)
106-
.replacingOccurrences(of: "`.+?`", with: "zzz", options: .regularExpression)
107109
let (commentSentences, trailingText) = sentences(in: trimmedText)
108110
if commentSentences.count == 0 {
109111
diagnose(.terminateSentenceWithPeriod(trimmedText), on: decl)
@@ -236,3 +238,9 @@ extension Finding.Message {
236238
"add a blank comment line after this sentence: \"\(text)\""
237239
}
238240
}
241+
242+
struct InlineCodeRemover: MarkupRewriter {
243+
mutating func visitInlineCode(_ inlineCode: InlineCode) -> Markup? {
244+
nil
245+
}
246+
}

Tests/SwiftFormatTests/Rules/BeginDocumentationCommentWithOneLineSummaryTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ final class BeginDocumentationCommentWithOneLineSummaryTests: LintOrFormatRuleTe
3838
/// terminator in this argument array.
3939
public static var postTerminator: ArgumentArrayParsingStrategy
4040
41+
/// The source converter for the text in the current test, which
42+
/// is related to ``thisInvalid(method.Signature:)``.
43+
var converter: SourceLocationConverter!
44+
4145
/// This docline should not succeed.
4246
/// There are two sentences without a blank line between them.
4347
1️⃣struct Test {}

0 commit comments

Comments
 (0)