Changed RuleExample to be a struct

This commit is contained in:
Chris Eidhof
2015-05-20 15:25:09 +02:00
committed by JP Simard
parent 1456f067c0
commit 89ea957b05
17 changed files with 87 additions and 89 deletions
+6 -6
View File
@@ -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
}
+1 -1
View File
@@ -43,7 +43,7 @@ public struct Linter {
}
public var explainableRules: [RuleExample] {
return flatten(rules.map { $0 as? RuleExample })
return flatten(rules.map { $0.example })
}
/**
+2 -1
View File
@@ -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 {
+23 -27
View File
@@ -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"
]
)
}
@@ -31,4 +31,6 @@ struct FileLengthRule: ParameterizedRule {
}
return []
}
let example: RuleExample? = nil
}
@@ -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" ]
)
}
@@ -77,4 +77,6 @@ struct FunctionBodyLengthRule: ASTRule, ParameterizedRule {
}
return []
}
let example: RuleExample? = nil
}
@@ -24,4 +24,6 @@ struct LeadingWhitespaceRule: Rule {
}
return []
}
let example: RuleExample? = nil
}
@@ -32,4 +32,6 @@ struct LineLengthRule: ParameterizedRule {
return nil
})
}
let example: RuleExample? = nil
}
@@ -74,4 +74,6 @@ struct NestingRule: ASTRule {
})
return violations
}
let example: RuleExample? = nil
}
+14 -18
View File
@@ -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"
]
)
}
@@ -24,4 +24,6 @@ struct TrailingNewlineRule: Rule {
}
return []
}
let example: RuleExample? = nil
}
@@ -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" ]
)
}
@@ -66,4 +66,6 @@ struct TypeBodyLengthRule: ASTRule, ParameterizedRule {
}
return []
}
let example: RuleExample? = nil
}
@@ -67,4 +67,6 @@ struct TypeNameRule: ASTRule {
}
return violations
}
let example: RuleExample? = nil
}
@@ -68,4 +68,6 @@ struct VariableNameRule: ASTRule {
}
return violations
}
let example: RuleExample? = nil
}
@@ -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) {