Skip to content

Commit c63f35b

Browse files
refactor: start is non-nullable, but defaults to 1
1 parent 6ea2532 commit c63f35b

File tree

6 files changed

+16
-17
lines changed

6 files changed

+16
-17
lines changed

Sources/Markdown/Base/RawMarkup.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ enum RawMarkupData: Equatable {
2424
case thematicBreak
2525
case htmlBlock(String)
2626
case listItem(checkbox: Checkbox?)
27-
case orderedList(start: Int?)
27+
case orderedList(start: Int = 1)
2828
case unorderedList
2929
case paragraph
3030
case blockDirective(name: String, nameLocation: SourceLocation?, arguments: DirectiveArgumentText)
@@ -217,7 +217,7 @@ final class RawMarkup: ManagedBuffer<RawMarkupHeader, RawMarkup> {
217217
return .create(data: .listItem(checkbox: checkbox), parsedRange: parsedRange, children: children)
218218
}
219219

220-
static func orderedList(parsedRange: SourceRange?, _ children: [RawMarkup], start: Int?) -> RawMarkup {
220+
static func orderedList(parsedRange: SourceRange?, _ children: [RawMarkup], start: Int = 1) -> RawMarkup {
221221
return .create(data: .orderedList(start: start), parsedRange: parsedRange, children: children)
222222
}
223223

Sources/Markdown/Block Nodes/Block Container Blocks/OrderedList.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,21 @@ public extension OrderedList {
2929
// MARK: ListItemContainer
3030

3131
init<Items: Sequence>(_ items: Items) where Items.Element == ListItem {
32-
try! self.init(.orderedList(parsedRange: nil, items.map { $0.raw.markup }, start: nil))
32+
try! self.init(.orderedList(parsedRange: nil, items.map { $0.raw.markup }))
3333
}
3434

3535
/// The starting index for the list.
3636
///
37-
/// If this is `nil`, the list will start at the default value of 1.
38-
var start: Int? {
37+
/// The default starting index in CommonMark is 1. In this case, clients may use any desired index for this list.
38+
var start: Int {
3939
get {
4040
guard case let .orderedList(start) = _data.raw.markup.data else {
4141
fatalError("\(self) markup wrapped unexpected \(_data.raw)")
4242
}
4343
return start
4444
}
4545
set {
46-
precondition(newValue ?? 1 > 0, "List start must be 1 or greater")
46+
precondition(newValue > 0, "List start must be 1 or greater")
4747
guard start != newValue else {
4848
return
4949
}

Sources/Markdown/Parser/CommonMarkConverter.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,8 @@ struct MarkupParser {
299299
case CMARK_BULLET_LIST:
300300
return MarkupConversion(state: childConversion.state.next(), result: .unorderedList(parsedRange: parsedRange, childConversion.result))
301301
case CMARK_ORDERED_LIST:
302-
let cmarkStart = cmark_node_get_list_start(state.node)
303-
let start: Int? = cmarkStart == 1 ? nil : Int(cmarkStart)
304-
return MarkupConversion(state: childConversion.state.next(), result: .orderedList(parsedRange: parsedRange, childConversion.result, start: start))
302+
let cmarkStart = Int(cmark_node_get_list_start(state.node))
303+
return MarkupConversion(state: childConversion.state.next(), result: .orderedList(parsedRange: parsedRange, childConversion.result, start: cmarkStart))
305304
default:
306305
fatalError("cmark reported a list node but said its list type is CMARK_NO_LIST?")
307306
}

Sources/Markdown/Walker/Walkers/MarkupFormatter.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -498,14 +498,14 @@ public struct MarkupFormatter: MarkupWalker {
498498
let numeral: UInt
499499
switch formattingOptions.orderedListNumerals {
500500
case let .allSame(n):
501-
if let origStart = list.start {
502-
numeral = UInt(origStart)
503-
} else {
501+
if list.start == 1 {
504502
numeral = n
503+
} else {
504+
numeral = UInt(list.start)
505505
}
506506
case let .incrementing(start):
507-
if let origStart = list.start {
508-
numeral = UInt(origStart) + UInt(listItem.indexInParent)
507+
if list.start != 1 {
508+
numeral = UInt(list.start) + UInt(listItem.indexInParent)
509509
} else {
510510
numeral = start + UInt(listItem.indexInParent)
511511
}

Sources/Markdown/Walker/Walkers/MarkupTreeDumper.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ struct MarkupTreeDumper: MarkupWalker {
190190
}
191191

192192
mutating func visitOrderedList(_ orderedList: OrderedList) {
193-
if let start = orderedList.start {
194-
dump(orderedList, customDescription: "start: \(start)")
193+
if orderedList.start != 1 {
194+
dump(orderedList, customDescription: "start: \(orderedList.start)")
195195
} else {
196196
defaultVisit(orderedList)
197197
}

Tests/MarkdownTests/Base/RawMarkupToMarkupTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ final class RawMarkupToMarkupTests: XCTestCase {
4848
}
4949

5050
func testOrderedList() {
51-
XCTAssertNoThrow(try OrderedList(.orderedList(parsedRange: nil, [], start: nil)))
51+
XCTAssertNoThrow(try OrderedList(.orderedList(parsedRange: nil, [])))
5252
XCTAssertThrowsError(try OrderedList(.softBreak(parsedRange: nil)))
5353
}
5454

0 commit comments

Comments
 (0)