Skip to content

Commit e4e6eaa

Browse files
committed
Ignore sentence terminators inside quotes when applying the 'BeginDocumentationCommentWithOneLineSummary' option
1 parent 4e8ad38 commit e4e6eaa

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

Sources/SwiftFormat/Rules/BeginDocumentationCommentWithOneLineSummary.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,14 @@ public final class BeginDocumentationCommentWithOneLineSummary: SyntaxLintRule {
130130
in: text.startIndex..<text.endIndex,
131131
scheme: NSLinguisticTagScheme.lexicalClass.rawValue,
132132
tokenRanges: &tokenRanges)
133+
var isInsideQuotes = false
133134
let sentenceTerminatorIndices = tags.enumerated().filter {
134-
$0.element == "SentenceTerminator"
135+
if $0.element == "OpenQuote" {
136+
isInsideQuotes = true
137+
} else if $0.element == "CloseQuote" {
138+
isInsideQuotes = false
139+
}
140+
return !isInsideQuotes && $0.element == "SentenceTerminator"
135141
}.map {
136142
tokenRanges[$0.offset].lowerBound
137143
}

Tests/SwiftFormatTests/Rules/BeginDocumentationCommentWithOneLineSummaryTests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,24 @@ final class BeginDocumentationCommentWithOneLineSummaryTests: LintOrFormatRuleTe
139139
)
140140
#endif
141141
}
142+
143+
func testSentenceTerminationInsideQuotes() {
144+
assertLint(
145+
BeginDocumentationCommentWithOneLineSummary.self,
146+
"""
147+
/// Creates an instance with the same raw value as `x` failing iff `x.kind != Subject.kind`.
148+
struct TestBackTick {}
149+
150+
/// A set of `Diagnostic` that can answer the question 'was there an error?' in O(1).
151+
struct TestSingleQuotes {}
152+
153+
/// A set of `Diagnostic` that can answer the question “was there an error?” in O(1).
154+
struct TestDoubleQuotes {}
155+
156+
/// A set of `Diagnostic` that can answer the question “was there
157+
/// an error?” in O(1).
158+
struct TestTwoLinesDoubleQuotes {}
159+
"""
160+
)
161+
}
142162
}

0 commit comments

Comments
 (0)