From 8bc46cbeb0d4a7a304574a3646088c3347ba8dc7 Mon Sep 17 00:00:00 2001 From: John Sundell Date: Mon, 11 Mar 2019 14:52:46 +0100 Subject: [PATCH] Add SwiftLint (#49) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change adds SwiftLint to the project. For now, the rules are tweaked to match the current state of the code base — but will over time be changed back to their defaults in several cases. Some smaller changes (mostly related to code style) were applied to the project to remove all warnings. --- .swiftlint.yml | 7 ++ Package.swift | 2 +- .../Output/AttributedStringOutputFormat.swift | 2 +- Sources/Splash/Theming/Theme+Defaults.swift | 108 +++++++++--------- Sources/Splash/Theming/Theme.swift | 7 +- Sources/Splash/Tokenizing/Segment.swift | 2 +- Sources/Splash/Tokenizing/Tokenizer.swift | 2 +- .../SplashTests/Tests/DeclarationTests.swift | 8 +- Tests/SplashTests/Tests/StatementTests.swift | 14 +-- Tests/SplashTests/Tests/TokenTypeTests.swift | 1 - 10 files changed, 81 insertions(+), 72 deletions(-) create mode 100644 .swiftlint.yml diff --git a/.swiftlint.yml b/.swiftlint.yml new file mode 100644 index 0000000..98897f1 --- /dev/null +++ b/.swiftlint.yml @@ -0,0 +1,7 @@ +disabled_rules: + - nesting +line_length: 200 +function_body_length: 100 +type_body_length: 600 +file_length: 800 +cyclomatic_complexity: 15 diff --git a/Package.swift b/Package.swift index 778a808..4d76248 100644 --- a/Package.swift +++ b/Package.swift @@ -11,7 +11,7 @@ import PackageDescription let package = Package( name: "Splash", products: [ - .library(name: "Splash", targets: ["Splash"]) + .library(name: "Splash", targets: ["Splash"]) ], targets: [ .target(name: "Splash"), diff --git a/Sources/Splash/Output/AttributedStringOutputFormat.swift b/Sources/Splash/Output/AttributedStringOutputFormat.swift index b418089..4a54733 100644 --- a/Sources/Splash/Output/AttributedStringOutputFormat.swift +++ b/Sources/Splash/Output/AttributedStringOutputFormat.swift @@ -59,7 +59,7 @@ private extension NSMutableAttributedString { .foregroundColor: color, .font: font ]) - + append(attributedString) } } diff --git a/Sources/Splash/Theming/Theme+Defaults.swift b/Sources/Splash/Theming/Theme+Defaults.swift index 4a921c6..90d8ccd 100644 --- a/Sources/Splash/Theming/Theme+Defaults.swift +++ b/Sources/Splash/Theming/Theme+Defaults.swift @@ -19,15 +19,15 @@ public extension Theme { blue: 0.74 ), tokenColors: [ - .keyword : Color(red: 0.91, green: 0.2, blue: 0.54), - .string : Color(red: 0.98, green: 0.39, blue: 0.12), - .type : Color(red: 0.51, green: 0.51, blue: 0.79), - .call : Color(red: 0.2, green: 0.56, blue: 0.9), - .number : Color(red: 0.86, green: 0.44, blue: 0.34), - .comment : Color(red: 0.42, green: 0.54, blue: 0.58), - .property : Color(red: 0.13, green: 0.67, blue: 0.62), - .dotAccess : Color(red: 0.57, green: 0.7, blue: 0), - .preprocessing : Color(red: 0.71, green: 0.54, blue: 0) + .keyword: Color(red: 0.91, green: 0.2, blue: 0.54), + .string: Color(red: 0.98, green: 0.39, blue: 0.12), + .type: Color(red: 0.51, green: 0.51, blue: 0.79), + .call: Color(red: 0.2, green: 0.56, blue: 0.9), + .number: Color(red: 0.86, green: 0.44, blue: 0.34), + .comment: Color(red: 0.42, green: 0.54, blue: 0.58), + .property: Color(red: 0.13, green: 0.67, blue: 0.62), + .dotAccess: Color(red: 0.57, green: 0.7, blue: 0), + .preprocessing: Color(red: 0.71, green: 0.54, blue: 0) ], backgroundColor: Color( red: 0.098, @@ -47,15 +47,15 @@ public extension Theme { blue: 1 ), tokenColors: [ - .keyword : Color(red: 0.828, green: 0.095, blue: 0.583), - .string : Color(red: 1.0, green: 0.171, blue: 0.219), - .type : Color(red: 0.137, green: 1.0, blue: 0.512), - .call : Color(red: 0.137, green: 1.0, blue: 0.512), - .number : Color(red: 0.469, green: 0.426, blue: 1.00), - .comment : Color(red: 0.255, green: 0.801, blue: 0.27), - .property : Color(red: 0.431, green: 0.714, blue: 0.533), - .dotAccess : Color(red: 0.431, green: 0.714, blue: 0.533), - .preprocessing : Color(red: 0.896, green: 0.488, blue: 0.284) + .keyword: Color(red: 0.828, green: 0.095, blue: 0.583), + .string: Color(red: 1.0, green: 0.171, blue: 0.219), + .type: Color(red: 0.137, green: 1.0, blue: 0.512), + .call: Color(red: 0.137, green: 1.0, blue: 0.512), + .number: Color(red: 0.469, green: 0.426, blue: 1.00), + .comment: Color(red: 0.255, green: 0.801, blue: 0.27), + .property: Color(red: 0.431, green: 0.714, blue: 0.533), + .dotAccess: Color(red: 0.431, green: 0.714, blue: 0.533), + .preprocessing: Color(red: 0.896, green: 0.488, blue: 0.284) ], backgroundColor: Color( red: 0, @@ -75,15 +75,15 @@ public extension Theme { blue: 0.84 ), tokenColors: [ - .keyword : Color(red: 0.992, green: 0.791, blue: 0.45), - .string : Color(red: 0.966, green: 0.517, blue: 0.29), - .type : Color(red: 0.431, green: 0.714, blue: 0.533), - .call : Color(red: 0.431, green: 0.714, blue: 0.533), - .number : Color(red: 0.559, green: 0.504, blue: 0.745), - .comment : Color(red: 0.484, green: 0.483, blue: 0.504), - .property : Color(red: 0.431, green: 0.714, blue: 0.533), - .dotAccess : Color(red: 0.431, green: 0.714, blue: 0.533), - .preprocessing : Color(red: 0.992, green: 0.791, blue: 0.45) + .keyword: Color(red: 0.992, green: 0.791, blue: 0.45), + .string: Color(red: 0.966, green: 0.517, blue: 0.29), + .type: Color(red: 0.431, green: 0.714, blue: 0.533), + .call: Color(red: 0.431, green: 0.714, blue: 0.533), + .number: Color(red: 0.559, green: 0.504, blue: 0.745), + .comment: Color(red: 0.484, green: 0.483, blue: 0.504), + .property: Color(red: 0.431, green: 0.714, blue: 0.533), + .dotAccess: Color(red: 0.431, green: 0.714, blue: 0.533), + .preprocessing: Color(red: 0.992, green: 0.791, blue: 0.45) ], backgroundColor: Color( red: 0.18, @@ -103,15 +103,15 @@ public extension Theme { blue: 1 ), tokenColors: [ - .keyword : Color(red: 0.948, green: 0.140, blue: 0.547), - .string : Color(red: 0.988, green: 0.273, blue: 0.317), - .type : Color(red: 0.584, green: 0.898, blue: 0.361), - .call : Color(red: 0.584, green: 0.898, blue: 0.361), - .number : Color(red: 0.587, green: 0.517, blue: 0.974), - .comment : Color(red: 0.424, green: 0.475, blue: 0.529), - .property : Color(red: 0.584, green: 0.898, blue: 0.361), - .dotAccess : Color(red: 0.584, green: 0.898, blue: 0.361), - .preprocessing : Color(red: 0.952, green: 0.526, blue: 0.229) + .keyword: Color(red: 0.948, green: 0.140, blue: 0.547), + .string: Color(red: 0.988, green: 0.273, blue: 0.317), + .type: Color(red: 0.584, green: 0.898, blue: 0.361), + .call: Color(red: 0.584, green: 0.898, blue: 0.361), + .number: Color(red: 0.587, green: 0.517, blue: 0.974), + .comment: Color(red: 0.424, green: 0.475, blue: 0.529), + .property: Color(red: 0.584, green: 0.898, blue: 0.361), + .dotAccess: Color(red: 0.584, green: 0.898, blue: 0.361), + .preprocessing: Color(red: 0.952, green: 0.526, blue: 0.229) ], backgroundColor: Color( red: 0.163, @@ -131,15 +131,15 @@ public extension Theme { blue: 0 ), tokenColors: [ - .keyword : Color(red: 0.161, green: 0.259, blue: 0.467), - .string : Color(red: 0.875, green: 0.027, blue: 0.0), - .type : Color(red: 0.706, green: 0.27, blue: 0.0), - .call : Color(red: 0.278, green: 0.415, blue: 0.593), - .number : Color(red: 0.161, green: 0.259, blue: 0.467), - .comment : Color(red: 0.765, green: 0.455, blue: 0.11), - .property : Color(red: 0.278, green: 0.415, blue: 0.593), - .dotAccess : Color(red: 0.278, green: 0.415, blue: 0.593), - .preprocessing : Color(red: 0.392, green: 0.391, blue: 0.52) + .keyword: Color(red: 0.161, green: 0.259, blue: 0.467), + .string: Color(red: 0.875, green: 0.027, blue: 0.0), + .type: Color(red: 0.706, green: 0.27, blue: 0.0), + .call: Color(red: 0.278, green: 0.415, blue: 0.593), + .number: Color(red: 0.161, green: 0.259, blue: 0.467), + .comment: Color(red: 0.765, green: 0.455, blue: 0.11), + .property: Color(red: 0.278, green: 0.415, blue: 0.593), + .dotAccess: Color(red: 0.278, green: 0.415, blue: 0.593), + .preprocessing: Color(red: 0.392, green: 0.391, blue: 0.52) ], backgroundColor: Color( red: 1, @@ -159,15 +159,15 @@ public extension Theme { blue: 0 ), tokenColors: [ - .keyword : Color(red: 0.706, green: 0.0, blue: 0.384), - .string : Color(red: 0.729, green: 0.0, blue: 0.067), - .type : Color(red: 0.267, green: 0.537, blue: 0.576), - .call : Color(red: 0.267, green: 0.537, blue: 0.576), - .number : Color(red: 0.0, green: 0.043, blue: 1.0), - .comment : Color(red: 0.336, green: 0.376, blue: 0.42), - .property : Color(red: 0.267, green: 0.537, blue: 0.576), - .dotAccess : Color(red: 0.267, green: 0.537, blue: 0.576), - .preprocessing : Color(red: 0.431, green: 0.125, blue: 0.051) + .keyword: Color(red: 0.706, green: 0.0, blue: 0.384), + .string: Color(red: 0.729, green: 0.0, blue: 0.067), + .type: Color(red: 0.267, green: 0.537, blue: 0.576), + .call: Color(red: 0.267, green: 0.537, blue: 0.576), + .number: Color(red: 0.0, green: 0.043, blue: 1.0), + .comment: Color(red: 0.336, green: 0.376, blue: 0.42), + .property: Color(red: 0.267, green: 0.537, blue: 0.576), + .dotAccess: Color(red: 0.267, green: 0.537, blue: 0.576), + .preprocessing: Color(red: 0.431, green: 0.125, blue: 0.051) ], backgroundColor: Color( red: 1, diff --git a/Sources/Splash/Theming/Theme.swift b/Sources/Splash/Theming/Theme.swift index 193aa5b..080014a 100644 --- a/Sources/Splash/Theming/Theme.swift +++ b/Sources/Splash/Theming/Theme.swift @@ -19,9 +19,12 @@ public struct Theme { /// What color to use for the background public var backgroundColor: Color /// What color to use for the text's highlighted tokens - public var tokenColors: [TokenType : Color] + public var tokenColors: [TokenType: Color] - public init(font: Font, plainTextColor: Color, tokenColors: [TokenType : Color], backgroundColor: Color = Color(white: 0.12, alpha: 1)) { + public init(font: Font, + plainTextColor: Color, + tokenColors: [TokenType: Color], + backgroundColor: Color = Color(white: 0.12, alpha: 1)) { self.font = font self.plainTextColor = plainTextColor self.tokenColors = tokenColors diff --git a/Sources/Splash/Tokenizing/Segment.swift b/Sources/Splash/Tokenizing/Segment.swift index 32cbe42..e3f1c84 100644 --- a/Sources/Splash/Tokenizing/Segment.swift +++ b/Sources/Splash/Tokenizing/Segment.swift @@ -27,7 +27,7 @@ public extension Segment { /// All tokens that have been found so far (excluding the current one) public var all: [String] /// The number of times a given token has been found up until this point - public var counts: [String : Int] + public var counts: [String: Int] /// The tokens that were previously found on the same line as the current one public var onSameLine: [String] /// The token that was previously found (may be on a different line) diff --git a/Sources/Splash/Tokenizing/Tokenizer.swift b/Sources/Splash/Tokenizing/Tokenizer.swift index 52ebffd..c6de106 100644 --- a/Sources/Splash/Tokenizing/Tokenizer.swift +++ b/Sources/Splash/Tokenizing/Tokenizer.swift @@ -42,7 +42,7 @@ private extension Tokenizer { private let code: String private let delimiters: CharacterSet private var index: String.Index? - private var tokenCounts = [String : Int]() + private var tokenCounts = [String: Int]() private var allTokens = [String]() private var lineTokens = [String]() private var segments: (current: Segment?, previous: Segment?) diff --git a/Tests/SplashTests/Tests/DeclarationTests.swift b/Tests/SplashTests/Tests/DeclarationTests.swift index 3e7609a..97c015c 100644 --- a/Tests/SplashTests/Tests/DeclarationTests.swift +++ b/Tests/SplashTests/Tests/DeclarationTests.swift @@ -598,10 +598,10 @@ final class DeclarationTests: SyntaxHighlighterTestCase { .plainText("}") ]) } - + func testDeferDeclaration() { let components = highlighter.highlight("func hello() { defer {} }") - + XCTAssertEqual(components, [ .token("func", .keyword), .whitespace(" "), @@ -616,10 +616,10 @@ final class DeclarationTests: SyntaxHighlighterTestCase { .plainText("}") ]) } - + func testFunctionDeclarationWithInOutParameter() { let components = highlighter.highlight("func swapValues(value1: inout Int, value2: inout Int) { }") - + XCTAssertEqual(components, [ .token("func", .keyword), .whitespace(" "), diff --git a/Tests/SplashTests/Tests/StatementTests.swift b/Tests/SplashTests/Tests/StatementTests.swift index 1ab4150..c06ddc1 100644 --- a/Tests/SplashTests/Tests/StatementTests.swift +++ b/Tests/SplashTests/Tests/StatementTests.swift @@ -134,7 +134,7 @@ final class StatementTests: SyntaxHighlighterTestCase { .plainText("}") ]) } - + func testSwitchStatementWithFallthrough() { let components = highlighter.highlight(""" switch variable { @@ -143,7 +143,7 @@ final class StatementTests: SyntaxHighlighterTestCase { callB() } """) - + XCTAssertEqual(components, [ .token("switch", .keyword), .whitespace(" "), @@ -260,10 +260,10 @@ final class StatementTests: SyntaxHighlighterTestCase { .plainText("}") ]) } - + func testForStatementWithContinue() { let components = highlighter.highlight("for value in Enum.allCases { continue }") - + XCTAssertEqual(components, [ .token("for", .keyword), .whitespace(" "), @@ -277,12 +277,12 @@ final class StatementTests: SyntaxHighlighterTestCase { .whitespace(" "), .plainText("{"), .whitespace(" "), - .token("continue",.keyword), + .token("continue", .keyword), .whitespace(" "), .plainText("}") ]) } - + func testRepeatWhileStatement() { let components = highlighter.highlight(""" var x = 5 @@ -291,7 +291,7 @@ final class StatementTests: SyntaxHighlighterTestCase { x = x - 1 } while x > 1 """) - + XCTAssertEqual(components, [ .token("var", .keyword), .whitespace(" "), diff --git a/Tests/SplashTests/Tests/TokenTypeTests.swift b/Tests/SplashTests/Tests/TokenTypeTests.swift index 7388879..f4852e0 100644 --- a/Tests/SplashTests/Tests/TokenTypeTests.swift +++ b/Tests/SplashTests/Tests/TokenTypeTests.swift @@ -29,4 +29,3 @@ extension TokenTypeTests { ] } } -