Skip to content

Commit

Permalink
Don’t treat accessing properties within switch statements as calls (J…
Browse files Browse the repository at this point in the history
…ohnSundell#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.
  • Loading branch information
JohnSundell authored Apr 16, 2020
1 parent 8e96992 commit 03ea9bd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/Splash/Grammar/SwiftGrammar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ private extension SwiftGrammar {
var tokenType: TokenType { return .call }
private let keywordsToAvoid: Set<String>
private let callLikeKeywords: Set<String>
private let controlFlowTokens = ["if", "&&", "||", "for"]
private let controlFlowTokens = ["if", "&&", "||", "for", "switch"]

init() {
var keywordsToAvoid = keywords
Expand Down
23 changes: 23 additions & 0 deletions Tests/SplashTests/Tests/StatementTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,28 @@ final class StatementTests: SyntaxHighlighterTestCase {
])
}

func testSwitchStatementWithProperty() {
let components = highlighter.highlight("""
switch object.value { default: break }
""")

XCTAssertEqual(components, [
.token("switch", .keyword),
.whitespace(" "),
.plainText("object."),
.token("value", .property),
.whitespace(" "),
.plainText("{"),
.whitespace(" "),
.token("default", .keyword),
.plainText(":"),
.whitespace(" "),
.token("break", .keyword),
.whitespace(" "),
.plainText("}")
])
}

func testForStatementWithStaticProperty() {
let components = highlighter.highlight("for value in Enum.allCases { }")

Expand Down Expand Up @@ -441,6 +463,7 @@ extension StatementTests {
("testSwitchStatementWithFallthrough", testSwitchStatementWithFallthrough),
("testSwitchStatementWithTypePatternMatching", testSwitchStatementWithTypePatternMatching),
("testSwitchStatementWithOptional", testSwitchStatementWithOptional),
("testSwitchStatementWithProperty", testSwitchStatementWithProperty),
("testForStatementWithStaticProperty", testForStatementWithStaticProperty),
("testForStatementWithContinue", testForStatementWithContinue),
("testRepeatWhileStatement", testRepeatWhileStatement),
Expand Down

0 comments on commit 03ea9bd

Please sign in to comment.