Skip to content

Commit ed9d716

Browse files
authored
Merge pull request #936 from natecook1000/doc-abstract-fixes
Ignore backticks in documentation abstracts
2 parents a5b5166 + 3d14907 commit ed9d716

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

Sources/SwiftFormat/Rules/BeginDocumentationCommentWithOneLineSummary.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import Foundation
14+
import Markdown
1415
import SwiftSyntax
1516

1617
#if os(macOS)
@@ -91,14 +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
100106
// plain text; we don't need any of the styling.
101-
let trimmedText = briefSummary.plainText.trimmingCharacters(in: .whitespacesAndNewlines)
107+
let trimmedText = noInlineCodeSummary.plainText
108+
.trimmingCharacters(in: .whitespacesAndNewlines)
102109
let (commentSentences, trailingText) = sentences(in: trimmedText)
103110
if commentSentences.count == 0 {
104111
diagnose(.terminateSentenceWithPeriod(trimmedText), on: decl)
@@ -231,3 +238,9 @@ extension Finding.Message {
231238
"add a blank comment line after this sentence: \"\(text)\""
232239
}
233240
}
241+
242+
struct InlineCodeRemover: MarkupRewriter {
243+
mutating func visitInlineCode(_ inlineCode: InlineCode) -> Markup? {
244+
nil
245+
}
246+
}

Tests/SwiftFormatTests/Rules/BeginDocumentationCommentWithOneLineSummaryTests.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ final class BeginDocumentationCommentWithOneLineSummaryTests: LintOrFormatRuleTe
3131
// ...
3232
}
3333
34+
/// Open a scope enclosed by `"typewriter"` double-quotes.
35+
struct BeginTypewriterDoubleQuotes: MDocMacroProtocol {}
36+
37+
/// Before parsing arguments, capture all inputs that follow the `--`
38+
/// terminator in this argument array.
39+
public static var postTerminator: ArgumentArrayParsingStrategy
40+
41+
/// The source converter for the text in the current test, which
42+
/// is related to ``thisInvalid(method.Signature:)``.
43+
var converter: SourceLocationConverter!
44+
3445
/// This docline should not succeed.
3546
/// There are two sentences without a blank line between them.
3647
1️⃣struct Test {}

0 commit comments

Comments
 (0)