From 03ea9bdcbb21354b66499d3d826a140a5119bc65 Mon Sep 17 00:00:00 2001 From: John Sundell Date: Thu, 16 Apr 2020 19:52:26 +0200 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20treat=20accessing=20properties?= =?UTF-8?q?=20within=20switch=20statements=20as=20calls=20(#103)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- Sources/Splash/Grammar/SwiftGrammar.swift | 2 +- Tests/SplashTests/Tests/StatementTests.swift | 23 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/Sources/Splash/Grammar/SwiftGrammar.swift b/Sources/Splash/Grammar/SwiftGrammar.swift index 262f5d0..40e6ff6 100644 --- a/Sources/Splash/Grammar/SwiftGrammar.swift +++ b/Sources/Splash/Grammar/SwiftGrammar.swift @@ -223,7 +223,7 @@ private extension SwiftGrammar { var tokenType: TokenType { return .call } private let keywordsToAvoid: Set private let callLikeKeywords: Set - private let controlFlowTokens = ["if", "&&", "||", "for"] + private let controlFlowTokens = ["if", "&&", "||", "for", "switch"] init() { var keywordsToAvoid = keywords diff --git a/Tests/SplashTests/Tests/StatementTests.swift b/Tests/SplashTests/Tests/StatementTests.swift index a43fb63..c514075 100644 --- a/Tests/SplashTests/Tests/StatementTests.swift +++ b/Tests/SplashTests/Tests/StatementTests.swift @@ -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 { }") @@ -441,6 +463,7 @@ extension StatementTests { ("testSwitchStatementWithFallthrough", testSwitchStatementWithFallthrough), ("testSwitchStatementWithTypePatternMatching", testSwitchStatementWithTypePatternMatching), ("testSwitchStatementWithOptional", testSwitchStatementWithOptional), + ("testSwitchStatementWithProperty", testSwitchStatementWithProperty), ("testForStatementWithStaticProperty", testForStatementWithStaticProperty), ("testForStatementWithContinue", testForStatementWithContinue), ("testRepeatWhileStatement", testRepeatWhileStatement),