Skip to content

Commit e2ac2bf

Browse files
authored
Adds support for comments in warning about extraneous content found after a link in task group item (#706)
Adds support for comments in warning about extraneous content found after a link in task group item rdar://115321138
1 parent 4bc2659 commit e2ac2bf

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

Sources/SwiftDocC/Model/TaskGroup.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ struct ExtractLinks: MarkupRewriter {
9393
guard let paragraph = item.child(at: 0) as? Paragraph,
9494
paragraph.childCount >= 1 else { return true }
9595

96+
// Check for trailing invalid content.
97+
let containsInvalidContent = paragraph.children.dropFirst().contains { child in
98+
let isComment = child is InlineHTML
99+
var isSpace = false
100+
if let text = child as? Text {
101+
isSpace = text.string.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
102+
}
103+
return !(isComment || isSpace)
104+
}
105+
96106
switch paragraph.child(at: 0) {
97107
case let link as Link:
98108
// Topic link
@@ -114,7 +124,7 @@ struct ExtractLinks: MarkupRewriter {
114124
links.append(link)
115125

116126
// Warn if there is a trailing content after the link
117-
if paragraph.childCount > 1 {
127+
if containsInvalidContent {
118128
problems.append(problemForTrailingContent(paragraph))
119129
}
120130
return false
@@ -123,7 +133,7 @@ struct ExtractLinks: MarkupRewriter {
123133
links.append(link)
124134

125135
// Warn if there is a trailing content after the link
126-
if paragraph.childCount > 1 {
136+
if containsInvalidContent {
127137
problems.append(problemForTrailingContent(paragraph))
128138
}
129139
return false

Tests/SwiftDocCTests/Infrastructure/DocumentationCuratorTests.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,10 @@ class DocumentationCuratorTests: XCTestCase {
279279
### Extraneous list item content
280280
- <doc:MyKit/MyClass>.
281281
- <doc:MyKit/MyClass> ![](featured.png) and *more* content...
282+
- <doc:MyKit/MyClass> @Comment { We (unfortunately) expect a warning here because DocC doesn't support directives in the middle of a line. }
283+
- <doc:MyKit/MyClass> <!-- This is a valid comment -->
284+
- <doc:MyKit/MyClass> <!-- This is a valid comment --> but this is extra content :(
285+
- <doc:MyKit/MyClass> This is extra content <!-- even if this is a valid comment -->
282286
## See Also
283287
- <doc:MyKit/MyClass>
284288
- Blip blop!
@@ -324,11 +328,11 @@ class DocumentationCuratorTests: XCTestCase {
324328
.filter({ $0.diagnostic.identifier == "org.swift.docc.UnexpectedTaskGroupItem" })
325329
.compactMap({ $0.possibleSolutions.first?.replacements.first?.range })
326330
.map({ "\($0.lowerBound.line):\($0.lowerBound.column)..<\($0.upperBound.line):\($0.upperBound.column)" }),
327-
["6:1..<6:13", "9:1..<9:20", "15:1..<15:13", "5:1..<5:13", "8:1..<8:20", "11:1..<11:13"]
331+
["6:1..<6:13", "9:1..<9:20", "19:1..<19:13", "5:1..<5:13", "8:1..<8:20", "11:1..<11:13"]
328332
)
329333

330-
// Verify the crawler emitted warnings for the 2 items with trailing content.
331-
XCTAssertEqual(crawler.problems.filter({ $0.diagnostic.identifier == "org.swift.docc.ExtraneousTaskGroupItemContent" }).count, 2)
334+
// Verify the crawler emitted warnings for the 5 items with trailing content.
335+
XCTAssertEqual(crawler.problems.filter({ $0.diagnostic.identifier == "org.swift.docc.ExtraneousTaskGroupItemContent" }).count, 5)
332336
XCTAssertTrue(crawler.problems
333337
.filter({ $0.diagnostic.identifier == "org.swift.docc.ExtraneousTaskGroupItemContent" })
334338
.compactMap({ $0.diagnostic.source?.path })

0 commit comments

Comments
 (0)