Skip to content

Commit 573e9a3

Browse files
authored
Fix BlockDirectiveParser logic (#50)
1 parent d03ad20 commit 573e9a3

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

Sources/Markdown/Parser/BlockDirectiveParser.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ struct PendingBlockDirective {
102102
// still consider subsequent lines for argument text, so we'll
103103
// indicate acceptance either way at this point.
104104
_ = parseArgumentsText(from: line)
105-
endLocation = line.location!
106105
return true
107106
} else {
108107
parseState = .contentsStart
@@ -121,6 +120,7 @@ struct PendingBlockDirective {
121120
self.argumentsText.append(argumentsText)
122121
accepted = true
123122
}
123+
endLocation = line.location!
124124

125125
if line.text.starts(with: ")") {
126126
parseState = .argumentsEnd

Tests/MarkdownTests/Parsing/BlockDirectiveParserTests.swift

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ class BlockDirectiveArgumentParserTests: XCTestCase {
292292

293293
let expectedDump = """
294294
Document @1:1-3:4
295-
├─ BlockDirective @1:1-1:8 name: "Outer"
295+
├─ BlockDirective @1:1-1:13 name: "Outer"
296296
│ ├─ Argument text segments:
297297
│ | @1:8-1:12: "x: 1"
298298
└─ Paragraph @2:3-3:2
@@ -867,4 +867,92 @@ class BlockDirectiveArgumentParserTests: XCTestCase {
867867
XCTAssertEqual(x.value, "")
868868
}
869869
}
870+
871+
872+
func testSingleLineRange() {
873+
let source = """
874+
@Image(source: 1.png, alt: "hello")
875+
"""
876+
877+
let document = Document(parsing: source, options: .parseBlockDirectives)
878+
let directive = document.child(at: 0) as! BlockDirective
879+
XCTAssertEqual(1, directive.argumentText.segments.count)
880+
881+
var parseErrors = [DirectiveArgumentText.ParseError]()
882+
let arguments = directive.argumentText.parseNameValueArguments(parseErrors: &parseErrors)
883+
XCTAssertTrue(parseErrors.isEmpty)
884+
885+
XCTAssertEqual(arguments.count,2)
886+
arguments["source"].map { x in
887+
XCTAssertEqual(x.name, "source")
888+
XCTAssertEqual(x.value, "1.png")
889+
}
890+
arguments["alt"].map { x in
891+
XCTAssertEqual(x.name, "alt")
892+
XCTAssertEqual(x.value, "hello")
893+
}
894+
895+
let expectedDump = #"""
896+
Document @1:1-1:36
897+
└─ BlockDirective @1:1-1:36 name: "Image"
898+
├─ Argument text segments:
899+
| @1:8-1:35: "source: 1.png, alt: \"hello\""
900+
"""#
901+
XCTAssertEqual(document.debugDescription(options: .printSourceLocations), expectedDump)
902+
}
903+
904+
func testSingleLineMissingCloseParenthesis() {
905+
let source = """
906+
@Image(source: 1.png,
907+
alt: "hello"
908+
@Image(source: 2.png, alt: "hello2")
909+
"""
910+
911+
let document = Document(parsing: source, options: .parseBlockDirectives)
912+
let expectedDump = #"""
913+
Document @1:1-3:37
914+
├─ BlockDirective @1:1-2:13 name: "Image"
915+
│ ├─ Argument text segments:
916+
│ | @1:8-1:22: "source: 1.png,"
917+
│ | @2:1-2:13: "alt: \"hello\""
918+
└─ BlockDirective @3:1-3:37 name: "Image"
919+
├─ Argument text segments:
920+
| @3:8-3:36: "source: 2.png, alt: \"hello2\""
921+
"""#
922+
XCTAssertEqual(document.debugDescription(options: .printSourceLocations), expectedDump)
923+
}
924+
925+
func testSingleLineOnlyOpenParenthesis() {
926+
let source = """
927+
@Image(
928+
@Image(source: 2.png, alt: "hello2")
929+
"""
930+
931+
let document = Document(parsing: source, options: .parseBlockDirectives)
932+
let expectedDump = #"""
933+
Document @1:1-2:37
934+
├─ BlockDirective @1:1-1:8 name: "Image"
935+
└─ BlockDirective @2:1-2:37 name: "Image"
936+
├─ Argument text segments:
937+
| @2:8-2:36: "source: 2.png, alt: \"hello2\""
938+
"""#
939+
XCTAssertEqual(document.debugDescription(options: .printSourceLocations), expectedDump)
940+
}
941+
942+
func testSingleLineMissingParenthesis() {
943+
let source = """
944+
@Image
945+
@Image(source: 2.png, alt: "hello2")
946+
"""
947+
948+
let document = Document(parsing: source, options: .parseBlockDirectives)
949+
let expectedDump = #"""
950+
Document @1:1-2:37
951+
├─ BlockDirective @1:1-1:7 name: "Image"
952+
└─ BlockDirective @2:1-2:37 name: "Image"
953+
├─ Argument text segments:
954+
| @2:8-2:36: "source: 2.png, alt: \"hello2\""
955+
"""#
956+
XCTAssertEqual(document.debugDescription(options: .printSourceLocations), expectedDump)
957+
}
870958
}

0 commit comments

Comments
 (0)