Skip to content

Commit 03ea9bd

Browse files
authored
Don’t treat accessing properties within switch statements as calls (JohnSundell#103)
This patch fixes syntax highlighting for when a property is being switched on, which previously would be treated as a function call with trailing closure syntax.
1 parent 8e96992 commit 03ea9bd

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

Sources/Splash/Grammar/SwiftGrammar.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ private extension SwiftGrammar {
223223
var tokenType: TokenType { return .call }
224224
private let keywordsToAvoid: Set<String>
225225
private let callLikeKeywords: Set<String>
226-
private let controlFlowTokens = ["if", "&&", "||", "for"]
226+
private let controlFlowTokens = ["if", "&&", "||", "for", "switch"]
227227

228228
init() {
229229
var keywordsToAvoid = keywords

Tests/SplashTests/Tests/StatementTests.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,28 @@ final class StatementTests: SyntaxHighlighterTestCase {
315315
])
316316
}
317317

318+
func testSwitchStatementWithProperty() {
319+
let components = highlighter.highlight("""
320+
switch object.value { default: break }
321+
""")
322+
323+
XCTAssertEqual(components, [
324+
.token("switch", .keyword),
325+
.whitespace(" "),
326+
.plainText("object."),
327+
.token("value", .property),
328+
.whitespace(" "),
329+
.plainText("{"),
330+
.whitespace(" "),
331+
.token("default", .keyword),
332+
.plainText(":"),
333+
.whitespace(" "),
334+
.token("break", .keyword),
335+
.whitespace(" "),
336+
.plainText("}")
337+
])
338+
}
339+
318340
func testForStatementWithStaticProperty() {
319341
let components = highlighter.highlight("for value in Enum.allCases { }")
320342

@@ -441,6 +463,7 @@ extension StatementTests {
441463
("testSwitchStatementWithFallthrough", testSwitchStatementWithFallthrough),
442464
("testSwitchStatementWithTypePatternMatching", testSwitchStatementWithTypePatternMatching),
443465
("testSwitchStatementWithOptional", testSwitchStatementWithOptional),
466+
("testSwitchStatementWithProperty", testSwitchStatementWithProperty),
444467
("testForStatementWithStaticProperty", testForStatementWithStaticProperty),
445468
("testForStatementWithContinue", testForStatementWithContinue),
446469
("testRepeatWhileStatement", testRepeatWhileStatement),

0 commit comments

Comments
 (0)