Files
SwiftLint/Source/SwiftLintFramework/Rules/ControlStatementRule.swift
T
2015-08-30 22:21:59 -07:00

67 lines
2.3 KiB
Swift

//
// ControlStatementRule.swift
// SwiftLint
//
// Created by Andrea Mazzini on 26/05/15.
// Copyright (c) 2015 Realm. All rights reserved.
//
import SourceKittenFramework
public struct ControlStatementRule: Rule {
public init() {}
public let identifier = "control_statement"
public func validateFile(file: File) -> [StyleViolation] {
return ["if", "for", "switch", "while"].flatMap { statementKind -> [StyleViolation] in
let pattern = "\(statementKind)\\s*\\([^,]*\\)\\s*\\{"
return file.matchPattern(pattern).flatMap { match, syntaxKinds in
if syntaxKinds.first != .Keyword {
return nil
}
return StyleViolation(type: .ControlStatement,
location: Location(file: file, offset: match.location),
severity: .Warning,
reason: "\(statementKind) statements shouldn't wrap their conditionals in " +
"parentheses.")
}
}
}
public let example = RuleExample(
ruleName: "Control Statement",
ruleDescription: "if,for,while,do statements shouldn't wrap their conditionals in " +
"parentheses.",
nonTriggeringExamples: [
"if condition {\n",
"if (a, b) == (0, 1) {\n",
"if renderGif(data) {\n",
"renderGif(data)\n",
"for item in collection {\n",
"for (key, value) in dictionary {\n",
"for (index, value) in enumerate(array) {\n",
"for var index = 0; index < 42; index++ {\n",
"while condition {\n",
"} while condition {\n",
"do { ; } while condition {\n",
"switch foo {\n",
],
triggeringExamples: [
"if (condition) {\n",
"if(condition) {\n",
"for (item in collection) {\n",
"for (var index = 0; index < 42; index++) {\n",
"for(item in collection) {\n",
"for(var index = 0; index < 42; index++) {\n",
"while (condition) {\n",
"while(condition) {\n",
"} while (condition) {\n",
"} while(condition) {\n",
"do { ; } while(condition) {\n",
"do { ; } while (condition) {\n",
"switch (foo) {\n",
]
)
}