From 89ea957b05a8a3fdb0ea170a106978a1fda8ebfa Mon Sep 17 00:00:00 2001 From: Chris Eidhof Date: Wed, 20 May 2015 15:25:09 +0200 Subject: [PATCH] Changed RuleExample to be a struct --- Source/SwiftLintFramework/ASTRule.swift | 12 ++--- Source/SwiftLintFramework/Linter.swift | 2 +- Source/SwiftLintFramework/Rule.swift | 3 +- .../SwiftLintFramework/Rules/ColonRule.swift | 50 +++++++++---------- .../Rules/FileLengthRule.swift | 2 + .../Rules/ForceCastRule.swift | 28 ++++------- .../Rules/FunctionBodyLengthRule.swift | 2 + .../Rules/LeadingWhitespaceRule.swift | 2 + .../Rules/LineLengthRule.swift | 2 + .../Rules/NestingRule.swift | 2 + .../SwiftLintFramework/Rules/TodoRule.swift | 32 ++++++------ .../Rules/TrailingNewlineRule.swift | 2 + .../Rules/TrailingWhitespaceRule.swift | 23 +++------ .../Rules/TypeBodyLengthRule.swift | 2 + .../Rules/TypeNameRule.swift | 2 + .../Rules/VariableNameRule.swift | 2 + .../SwiftLintFrameworkTests/LinterTests.swift | 8 +-- 17 files changed, 87 insertions(+), 89 deletions(-) diff --git a/Source/SwiftLintFramework/ASTRule.swift b/Source/SwiftLintFramework/ASTRule.swift index 0576da35e..b711f8441 100644 --- a/Source/SwiftLintFramework/ASTRule.swift +++ b/Source/SwiftLintFramework/ASTRule.swift @@ -17,10 +17,10 @@ protocol ASTRule: Rule { dictionary: XPCDictionary) -> [StyleViolation] } -public protocol RuleExample { - var ruleName: String { get } - var ruleDescription: String { get } - var correctExamples: [String] { get } - var failingExamples: [String] { get } - var showExamples: Bool { get } +public struct RuleExample { + public let ruleName: String + public let ruleDescription: String + public let correctExamples: [String] + public let failingExamples: [String] + public let showExamples: Bool = true } \ No newline at end of file diff --git a/Source/SwiftLintFramework/Linter.swift b/Source/SwiftLintFramework/Linter.swift index 379df52cd..1851a8e4d 100644 --- a/Source/SwiftLintFramework/Linter.swift +++ b/Source/SwiftLintFramework/Linter.swift @@ -43,7 +43,7 @@ public struct Linter { } public var explainableRules: [RuleExample] { - return flatten(rules.map { $0 as? RuleExample }) + return flatten(rules.map { $0.example }) } /** diff --git a/Source/SwiftLintFramework/Rule.swift b/Source/SwiftLintFramework/Rule.swift index 99e72f852..9e3729792 100644 --- a/Source/SwiftLintFramework/Rule.swift +++ b/Source/SwiftLintFramework/Rule.swift @@ -8,9 +8,10 @@ import SourceKittenFramework -protocol Validatable { +public protocol Validatable { var identifier: String { get } func validateFile(file: File) -> [StyleViolation] + var example: RuleExample? { get } } protocol ParameterizedRule: Rule { diff --git a/Source/SwiftLintFramework/Rules/ColonRule.swift b/Source/SwiftLintFramework/Rules/ColonRule.swift index 4cef50266..ca825d165 100644 --- a/Source/SwiftLintFramework/Rules/ColonRule.swift +++ b/Source/SwiftLintFramework/Rules/ColonRule.swift @@ -8,13 +8,13 @@ import SourceKittenFramework -public struct ColonRule: Rule, RuleExample { +public struct ColonRule: Rule { let identifier = "colon" public init() { } - func validateFile(file: File) -> [StyleViolation] { + public func validateFile(file: File) -> [StyleViolation] { let pattern1 = file.matchPattern("\\w+\\s+:\\s*\\S+", withSyntaxKinds: [.Identifier, .Typeidentifier]) let pattern2 = file.matchPattern("\\w+:(?:\\s{0}|\\s{2,})\\S+", @@ -27,29 +27,25 @@ public struct ColonRule: Rule, RuleExample { } } - public var ruleName = "Colon Rule" - - public var ruleDescription = "This rule checks whether you associate the colon with the identifier." - - public var correctExamples = [ - "let abc: Void\n", - "let abc: [Void: Void]\n", - "let abc: (Void, Void)\n", - "func abc(def: Void) {}\n" - ] - - public var failingExamples = [ - "let abc:Void\n", - "let abc: Void\n", - "let abc :Void\n", - "let abc : Void\n", - "let abc : [Void: Void]\n", - "func abc(def:Void) {}\n", - "func abc(def: Void) {}\n", - "func abc(def :Void) {}\n", - "func abc(def : Void) {}\n" - ] - - public var showExamples = true - + public let example: RuleExample? = RuleExample( + ruleName: "Colon Rule", + ruleDescription: "This rule checks whether you associate the colon with the identifier.", + correctExamples: [ + "let abc: Void\n", + "let abc: [Void: Void]\n", + "let abc: (Void, Void)\n", + "func abc(def: Void) {}\n" + ], + failingExamples: [ + "let abc:Void\n", + "let abc: Void\n", + "let abc :Void\n", + "let abc : Void\n", + "let abc : [Void: Void]\n", + "func abc(def:Void) {}\n", + "func abc(def: Void) {}\n", + "func abc(def :Void) {}\n", + "func abc(def : Void) {}\n" + ] + ) } diff --git a/Source/SwiftLintFramework/Rules/FileLengthRule.swift b/Source/SwiftLintFramework/Rules/FileLengthRule.swift index 9e851b432..373386910 100644 --- a/Source/SwiftLintFramework/Rules/FileLengthRule.swift +++ b/Source/SwiftLintFramework/Rules/FileLengthRule.swift @@ -31,4 +31,6 @@ struct FileLengthRule: ParameterizedRule { } return [] } + + let example: RuleExample? = nil } diff --git a/Source/SwiftLintFramework/Rules/ForceCastRule.swift b/Source/SwiftLintFramework/Rules/ForceCastRule.swift index 4dec646c0..07f093dc4 100644 --- a/Source/SwiftLintFramework/Rules/ForceCastRule.swift +++ b/Source/SwiftLintFramework/Rules/ForceCastRule.swift @@ -8,12 +8,12 @@ import SourceKittenFramework -public struct ForceCastRule: Rule, RuleExample { +public struct ForceCastRule: Rule { public init() { } let identifier = "force_cast" - func validateFile(file: File) -> [StyleViolation] { + public func validateFile(file: File) -> [StyleViolation] { return file.matchPattern("as!", withSyntaxKinds: [.Keyword]).map { range in return StyleViolation(type: .ForceCast, location: Location(file: file, offset: range.location), @@ -22,20 +22,14 @@ public struct ForceCastRule: Rule, RuleExample { } } - public var ruleName = "Force Cast Rule" - - public var ruleDescription = "This rule checks whether you don't do force casts." - - public var correctExamples = [ - "NSNumber() as? Int\n", - "// NSNumber() as! Int\n", - - ] - - public var failingExamples = [ - "NSNumber() as! Int\n" - ] - - public var showExamples = true + public let example: RuleExample? = RuleExample( + ruleName: "Force Cast Rule", + ruleDescription: "This rule checks whether you don't do force casts.", + correctExamples: [ + "NSNumber() as? Int\n", + "// NSNumber() as! Int\n", + ], + failingExamples: [ "NSNumber() as! Int\n" ] + ) } diff --git a/Source/SwiftLintFramework/Rules/FunctionBodyLengthRule.swift b/Source/SwiftLintFramework/Rules/FunctionBodyLengthRule.swift index 9ad9067cf..1e06fd43c 100644 --- a/Source/SwiftLintFramework/Rules/FunctionBodyLengthRule.swift +++ b/Source/SwiftLintFramework/Rules/FunctionBodyLengthRule.swift @@ -77,4 +77,6 @@ struct FunctionBodyLengthRule: ASTRule, ParameterizedRule { } return [] } + + let example: RuleExample? = nil } diff --git a/Source/SwiftLintFramework/Rules/LeadingWhitespaceRule.swift b/Source/SwiftLintFramework/Rules/LeadingWhitespaceRule.swift index f1df62b5a..933497911 100644 --- a/Source/SwiftLintFramework/Rules/LeadingWhitespaceRule.swift +++ b/Source/SwiftLintFramework/Rules/LeadingWhitespaceRule.swift @@ -24,4 +24,6 @@ struct LeadingWhitespaceRule: Rule { } return [] } + + let example: RuleExample? = nil } diff --git a/Source/SwiftLintFramework/Rules/LineLengthRule.swift b/Source/SwiftLintFramework/Rules/LineLengthRule.swift index 46ca0c2c1..901c5a276 100644 --- a/Source/SwiftLintFramework/Rules/LineLengthRule.swift +++ b/Source/SwiftLintFramework/Rules/LineLengthRule.swift @@ -32,4 +32,6 @@ struct LineLengthRule: ParameterizedRule { return nil }) } + + let example: RuleExample? = nil } diff --git a/Source/SwiftLintFramework/Rules/NestingRule.swift b/Source/SwiftLintFramework/Rules/NestingRule.swift index 9d703cbf0..df3560a05 100644 --- a/Source/SwiftLintFramework/Rules/NestingRule.swift +++ b/Source/SwiftLintFramework/Rules/NestingRule.swift @@ -74,4 +74,6 @@ struct NestingRule: ASTRule { }) return violations } + + let example: RuleExample? = nil } diff --git a/Source/SwiftLintFramework/Rules/TodoRule.swift b/Source/SwiftLintFramework/Rules/TodoRule.swift index dec01ca7a..eea12027f 100644 --- a/Source/SwiftLintFramework/Rules/TodoRule.swift +++ b/Source/SwiftLintFramework/Rules/TodoRule.swift @@ -8,12 +8,12 @@ import SourceKittenFramework -public struct TodoRule: Rule, RuleExample { +public struct TodoRule: Rule { let identifier = "todo" public init() { } - func validateFile(file: File) -> [StyleViolation] { + public func validateFile(file: File) -> [StyleViolation] { return file.matchPattern("// (TODO|FIXME):", withSyntaxKinds: [.Comment]).map { range in return StyleViolation(type: .TODO, location: Location(file: file, offset: range.location), @@ -22,22 +22,18 @@ public struct TodoRule: Rule, RuleExample { } } - public var ruleName = "Todo Rule" - - public var ruleDescription = "This rule checks whether you removed all TODOs and FIXMEs." - - public var correctExamples = [ - "let string = \"// TODO:\"\n", - "let string = \"// FIXME:\"\n" - ] - - public var failingExamples = [ - "// TODO:\n", - "// FIXME:\n" - ] - - public var showExamples = true - + public let example: RuleExample? = RuleExample( + ruleName: "Todo Rule", + ruleDescription: "This rule checks whether you removed all TODOs and FIXMEs.", + correctExamples: [ + "let string = \"// TODO:\"\n", + "let string = \"// FIXME:\"\n" + ], + failingExamples: [ + "// TODO:\n", + "// FIXME:\n" + ] + ) } diff --git a/Source/SwiftLintFramework/Rules/TrailingNewlineRule.swift b/Source/SwiftLintFramework/Rules/TrailingNewlineRule.swift index 1b1145544..d706da158 100644 --- a/Source/SwiftLintFramework/Rules/TrailingNewlineRule.swift +++ b/Source/SwiftLintFramework/Rules/TrailingNewlineRule.swift @@ -24,4 +24,6 @@ struct TrailingNewlineRule: Rule { } return [] } + + let example: RuleExample? = nil } diff --git a/Source/SwiftLintFramework/Rules/TrailingWhitespaceRule.swift b/Source/SwiftLintFramework/Rules/TrailingWhitespaceRule.swift index 72a0a7192..365671337 100644 --- a/Source/SwiftLintFramework/Rules/TrailingWhitespaceRule.swift +++ b/Source/SwiftLintFramework/Rules/TrailingWhitespaceRule.swift @@ -8,12 +8,12 @@ import SourceKittenFramework -public struct TrailingWhitespaceRule: Rule, RuleExample { +public struct TrailingWhitespaceRule: Rule { public init() { } let identifier = "trailing_whitespace" - func validateFile(file: File) -> [StyleViolation] { + public func validateFile(file: File) -> [StyleViolation] { return file.contents.lines().map { line in ( index: line.index, @@ -32,17 +32,10 @@ public struct TrailingWhitespaceRule: Rule, RuleExample { } } - public var ruleName = "Trailing Whitespace Rule" - - public var ruleDescription = "This rule checks whether you don't have any trailing whitespace." - - public var correctExamples = [ - "//\n" - ] - - public var failingExamples = [ - "// \n" - ] - - public var showExamples = false + public let example: RuleExample? = RuleExample( + ruleName: "Trailing Whitespace Rule", + ruleDescription: "This rule checks whether you don't have any trailing whitespace.", + correctExamples: [ "//\n" ], + failingExamples: [ "// \n" ] + ) } diff --git a/Source/SwiftLintFramework/Rules/TypeBodyLengthRule.swift b/Source/SwiftLintFramework/Rules/TypeBodyLengthRule.swift index e02f748be..4f3bfb3e0 100644 --- a/Source/SwiftLintFramework/Rules/TypeBodyLengthRule.swift +++ b/Source/SwiftLintFramework/Rules/TypeBodyLengthRule.swift @@ -66,4 +66,6 @@ struct TypeBodyLengthRule: ASTRule, ParameterizedRule { } return [] } + + let example: RuleExample? = nil } diff --git a/Source/SwiftLintFramework/Rules/TypeNameRule.swift b/Source/SwiftLintFramework/Rules/TypeNameRule.swift index 8d5559e32..a7209c5e1 100644 --- a/Source/SwiftLintFramework/Rules/TypeNameRule.swift +++ b/Source/SwiftLintFramework/Rules/TypeNameRule.swift @@ -67,4 +67,6 @@ struct TypeNameRule: ASTRule { } return violations } + + let example: RuleExample? = nil } diff --git a/Source/SwiftLintFramework/Rules/VariableNameRule.swift b/Source/SwiftLintFramework/Rules/VariableNameRule.swift index 0c9c92b72..6e8f1cf7b 100644 --- a/Source/SwiftLintFramework/Rules/VariableNameRule.swift +++ b/Source/SwiftLintFramework/Rules/VariableNameRule.swift @@ -68,4 +68,6 @@ struct VariableNameRule: ASTRule { } return violations } + + let example: RuleExample? = nil } diff --git a/Source/SwiftLintFrameworkTests/LinterTests.swift b/Source/SwiftLintFrameworkTests/LinterTests.swift index 5fd943843..5ad3f36c9 100644 --- a/Source/SwiftLintFrameworkTests/LinterTests.swift +++ b/Source/SwiftLintFrameworkTests/LinterTests.swift @@ -282,19 +282,19 @@ class LinterTests: XCTestCase { } func testLinesShouldntContainTrailingWhitespace() { - verifyRule(TrailingWhitespaceRule(), type: .TrailingWhitespace, violateInComments: true) + verifyRule(TrailingWhitespaceRule().example!, type: .TrailingWhitespace, violateInComments: true) } func testForceCasting() { - verifyRule(ForceCastRule(), type: .ForceCast) + verifyRule(ForceCastRule().example!, type: .ForceCast) } func testTodoOrFIXME() { - verifyRule(TodoRule(), type: .TODO) + verifyRule(TodoRule().example!, type: .TODO) } func testColon() { - verifyRule(ColonRule(), type: .Colon) + verifyRule(ColonRule().example!, type: .Colon) } func verifyRule(rule: RuleExample, type: StyleViolationType, violateInComments: Bool = false) {