From e2df0546d317792a0db67e99dd4728360655713f Mon Sep 17 00:00:00 2001 From: JP Simard Date: Wed, 20 May 2015 18:27:16 +0200 Subject: [PATCH] Split `Rule` into `Rule` and `ParameterizedRule` protocols (fixes #21) --- CHANGELOG.md | 4 ++++ Source/SwiftLintFramework/Rule.swift | 9 +++++---- Source/SwiftLintFramework/Rules/ColonRule.swift | 1 - Source/SwiftLintFramework/Rules/FileLengthRule.swift | 2 +- Source/SwiftLintFramework/Rules/ForceCastRule.swift | 1 - .../Rules/FunctionBodyLengthRule.swift | 2 +- .../SwiftLintFramework/Rules/LeadingWhitespaceRule.swift | 1 - Source/SwiftLintFramework/Rules/LineLengthRule.swift | 2 +- Source/SwiftLintFramework/Rules/NestingRule.swift | 1 - Source/SwiftLintFramework/Rules/TodoRule.swift | 1 - .../SwiftLintFramework/Rules/TrailingNewlineRule.swift | 1 - .../Rules/TrailingWhitespaceRule.swift | 1 - Source/SwiftLintFramework/Rules/TypeBodyLengthRule.swift | 2 +- Source/SwiftLintFramework/Rules/TypeNameRule.swift | 1 - Source/SwiftLintFramework/Rules/VariableNameRule.swift | 1 - 15 files changed, 13 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e591799964..546a1ccce0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ * The `Rule` and `ASTRule` protocol members are now non-static. [aarondaub](https://github.com/aarondaub) +* Split `Rule` into `Rule` and `ParameterizedRule` protocols. + [aarondaub](https://github.com/aarondaub) + [#21](https://github.com/realm/SwiftLint/issues/21) + ##### Enhancements * The following rules now conform to `ASTRule`: diff --git a/Source/SwiftLintFramework/Rule.swift b/Source/SwiftLintFramework/Rule.swift index d5f7e522e9..0a32cfd510 100644 --- a/Source/SwiftLintFramework/Rule.swift +++ b/Source/SwiftLintFramework/Rule.swift @@ -9,10 +9,11 @@ import SourceKittenFramework protocol Rule { - typealias ParameterType - var identifier: String { get } - var parameters: [RuleParameter] { get } - func validateFile(file: File) -> [StyleViolation] } + +protocol ParameterizedRule: Rule { + typealias ParameterType + var parameters: [RuleParameter] { get } +} diff --git a/Source/SwiftLintFramework/Rules/ColonRule.swift b/Source/SwiftLintFramework/Rules/ColonRule.swift index 0ef2926a05..fdb4fd0ada 100644 --- a/Source/SwiftLintFramework/Rules/ColonRule.swift +++ b/Source/SwiftLintFramework/Rules/ColonRule.swift @@ -10,7 +10,6 @@ import SourceKittenFramework struct ColonRule: Rule { let identifier = "colon" - let parameters = [RuleParameter]() func validateFile(file: File) -> [StyleViolation] { let pattern1 = file.matchPattern("\\w+\\s+:\\s*\\S+", diff --git a/Source/SwiftLintFramework/Rules/FileLengthRule.swift b/Source/SwiftLintFramework/Rules/FileLengthRule.swift index a1e142b575..9e851b432a 100644 --- a/Source/SwiftLintFramework/Rules/FileLengthRule.swift +++ b/Source/SwiftLintFramework/Rules/FileLengthRule.swift @@ -8,7 +8,7 @@ import SourceKittenFramework -struct FileLengthRule: Rule { +struct FileLengthRule: ParameterizedRule { let identifier = "file_length" let parameters = [ RuleParameter(severity: .VeryLow, value: 400), diff --git a/Source/SwiftLintFramework/Rules/ForceCastRule.swift b/Source/SwiftLintFramework/Rules/ForceCastRule.swift index a67dbdef34..78ab68125f 100644 --- a/Source/SwiftLintFramework/Rules/ForceCastRule.swift +++ b/Source/SwiftLintFramework/Rules/ForceCastRule.swift @@ -10,7 +10,6 @@ import SourceKittenFramework struct ForceCastRule: Rule { let identifier = "force_cast" - let parameters = [RuleParameter]() func validateFile(file: File) -> [StyleViolation] { return file.matchPattern("as!", withSyntaxKinds: [.Keyword]).map { range in diff --git a/Source/SwiftLintFramework/Rules/FunctionBodyLengthRule.swift b/Source/SwiftLintFramework/Rules/FunctionBodyLengthRule.swift index b36d9208d8..9ad9067cfe 100644 --- a/Source/SwiftLintFramework/Rules/FunctionBodyLengthRule.swift +++ b/Source/SwiftLintFramework/Rules/FunctionBodyLengthRule.swift @@ -9,7 +9,7 @@ import SourceKittenFramework import SwiftXPC -struct FunctionBodyLengthRule: ASTRule { +struct FunctionBodyLengthRule: ASTRule, ParameterizedRule { let identifier = "function_body_length" let parameters = [ RuleParameter(severity: .VeryLow, value: 40), diff --git a/Source/SwiftLintFramework/Rules/LeadingWhitespaceRule.swift b/Source/SwiftLintFramework/Rules/LeadingWhitespaceRule.swift index fdad0fdd85..f1df62b5a7 100644 --- a/Source/SwiftLintFramework/Rules/LeadingWhitespaceRule.swift +++ b/Source/SwiftLintFramework/Rules/LeadingWhitespaceRule.swift @@ -10,7 +10,6 @@ import SourceKittenFramework struct LeadingWhitespaceRule: Rule { let identifier = "leading_whitespace" - let parameters = [RuleParameter]() func validateFile(file: File) -> [StyleViolation] { let countOfLeadingWhitespace = file.contents.countOfLeadingCharactersInSet( diff --git a/Source/SwiftLintFramework/Rules/LineLengthRule.swift b/Source/SwiftLintFramework/Rules/LineLengthRule.swift index 8b8e37ef4c..46ca0c2c1b 100644 --- a/Source/SwiftLintFramework/Rules/LineLengthRule.swift +++ b/Source/SwiftLintFramework/Rules/LineLengthRule.swift @@ -8,7 +8,7 @@ import SourceKittenFramework -struct LineLengthRule: Rule { +struct LineLengthRule: ParameterizedRule { let identifier = "line_length" let parameters = [ RuleParameter(severity: .VeryLow, value: 100), diff --git a/Source/SwiftLintFramework/Rules/NestingRule.swift b/Source/SwiftLintFramework/Rules/NestingRule.swift index c7a77ad3c3..9d703cbf08 100644 --- a/Source/SwiftLintFramework/Rules/NestingRule.swift +++ b/Source/SwiftLintFramework/Rules/NestingRule.swift @@ -11,7 +11,6 @@ import SwiftXPC struct NestingRule: ASTRule { let identifier = "nesting" - let parameters = [RuleParameter]() func validateFile(file: File) -> [StyleViolation] { return self.validateFile(file, dictionary: Structure(file: file).dictionary) diff --git a/Source/SwiftLintFramework/Rules/TodoRule.swift b/Source/SwiftLintFramework/Rules/TodoRule.swift index 729d19a217..bf14d33105 100644 --- a/Source/SwiftLintFramework/Rules/TodoRule.swift +++ b/Source/SwiftLintFramework/Rules/TodoRule.swift @@ -10,7 +10,6 @@ import SourceKittenFramework struct TodoRule: Rule { let identifier = "todo" - let parameters = [RuleParameter]() func validateFile(file: File) -> [StyleViolation] { return file.matchPattern("// (TODO|FIXME):", withSyntaxKinds: [.Comment]).map { range in diff --git a/Source/SwiftLintFramework/Rules/TrailingNewlineRule.swift b/Source/SwiftLintFramework/Rules/TrailingNewlineRule.swift index 15bdcd4cfd..1b11455448 100644 --- a/Source/SwiftLintFramework/Rules/TrailingNewlineRule.swift +++ b/Source/SwiftLintFramework/Rules/TrailingNewlineRule.swift @@ -10,7 +10,6 @@ import SourceKittenFramework struct TrailingNewlineRule: Rule { let identifier = "trailing_newline" - let parameters = [RuleParameter]() func validateFile(file: File) -> [StyleViolation] { let countOfTrailingNewlines = file.contents.countOfTailingCharactersInSet( diff --git a/Source/SwiftLintFramework/Rules/TrailingWhitespaceRule.swift b/Source/SwiftLintFramework/Rules/TrailingWhitespaceRule.swift index 448ac9c579..f7b6a2b1ea 100644 --- a/Source/SwiftLintFramework/Rules/TrailingWhitespaceRule.swift +++ b/Source/SwiftLintFramework/Rules/TrailingWhitespaceRule.swift @@ -10,7 +10,6 @@ import SourceKittenFramework struct TrailingWhitespaceRule: Rule { let identifier = "trailing_whitespace" - let parameters = [RuleParameter]() func validateFile(file: File) -> [StyleViolation] { return file.contents.lines().map { line in diff --git a/Source/SwiftLintFramework/Rules/TypeBodyLengthRule.swift b/Source/SwiftLintFramework/Rules/TypeBodyLengthRule.swift index ad2f46d4de..e02f748bee 100644 --- a/Source/SwiftLintFramework/Rules/TypeBodyLengthRule.swift +++ b/Source/SwiftLintFramework/Rules/TypeBodyLengthRule.swift @@ -9,7 +9,7 @@ import SourceKittenFramework import SwiftXPC -struct TypeBodyLengthRule: ASTRule { +struct TypeBodyLengthRule: ASTRule, ParameterizedRule { let identifier = "type_body_length" let parameters = [ RuleParameter(severity: .VeryLow, value: 200), diff --git a/Source/SwiftLintFramework/Rules/TypeNameRule.swift b/Source/SwiftLintFramework/Rules/TypeNameRule.swift index da312c8d5a..8d5559e323 100644 --- a/Source/SwiftLintFramework/Rules/TypeNameRule.swift +++ b/Source/SwiftLintFramework/Rules/TypeNameRule.swift @@ -11,7 +11,6 @@ import SwiftXPC struct TypeNameRule: ASTRule { let identifier = "type_name" - let parameters = [RuleParameter]() func validateFile(file: File) -> [StyleViolation] { return self.validateFile(file, dictionary: Structure(file: file).dictionary) diff --git a/Source/SwiftLintFramework/Rules/VariableNameRule.swift b/Source/SwiftLintFramework/Rules/VariableNameRule.swift index 1ff534d2c3..0c9c92b723 100644 --- a/Source/SwiftLintFramework/Rules/VariableNameRule.swift +++ b/Source/SwiftLintFramework/Rules/VariableNameRule.swift @@ -11,7 +11,6 @@ import SwiftXPC struct VariableNameRule: ASTRule { let identifier = "variable_name" - let parameters = [RuleParameter]() func validateFile(file: File) -> [StyleViolation] { return self.validateFile(file, dictionary: Structure(file: file).dictionary)