Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ControlStatementRule #39

Merged
merged 6 commits into from
May 26, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Source/SwiftLintFramework/Linter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public struct Linter {
VariableNameRule(),
TypeBodyLengthRule(),
FunctionBodyLengthRule(),
NestingRule()
NestingRule(),
ControlStatementRule()
]

public var styleViolations: [StyleViolation] {
Expand Down
62 changes: 62 additions & 0 deletions Source/SwiftLintFramework/Rules/ControlStatementRule.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// 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] {
let statement = file.matchPattern("\\s{0,}(if|for|while)\\s{0,}\\(",
withSyntaxKinds: [.Keyword])
return statement.map { range in
return StyleViolation(type: .ControlStatement,
location: Location(file: file, offset: range.location),
severity: .Low,
reason: "if,for,while,do statements shouldn't wrap their conditionals in parentheses.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should probably use the regex match for the specific keyword instead of listing them all

}
}

public let example = RuleExample(
ruleName: "Control Statement",
ruleDescription: "if,for,while,do statements shouldn't wrap their conditionals in parentheses.",
nonTriggeringExamples: [
"if condition {\n",
"renderGif(data)\n",
"// if (hasBetterSyntax) {\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",
"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"
],
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"
]
)
}

1 change: 1 addition & 0 deletions Source/SwiftLintFramework/StyleViolationType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public enum StyleViolationType: String, Printable {
case TODO = "TODO or FIXME"
case Colon = "Colon"
case Nesting = "Nesting"
case ControlStatement = "Control Statement"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Control Statement Parentheses?


public var description: String { return rawValue }
}
2 changes: 1 addition & 1 deletion Source/SwiftLintFrameworkTests/LinterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class LinterTests: XCTestCase {
}

func testControlStatements() {
// TODO: if,for,while,do statements shouldn't wrap their conditionals in parentheses.
verifyRule(ControlStatementRule().example, type: .ControlStatement, commentDoesntViolate: false)
}

func testNumberOfFunctionsInAType() {
Expand Down
4 changes: 4 additions & 0 deletions SwiftLint.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
65454F471B14D76500319A6C /* ControlStatementRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 65454F451B14D73800319A6C /* ControlStatementRule.swift */; };
83894F221B0C928A006214E1 /* RulesCommand.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83894F211B0C928A006214E1 /* RulesCommand.swift */; };
83D71E281B131ECE000395DE /* RuleExample.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83D71E261B131EB5000395DE /* RuleExample.swift */; };
83D71E2B1B131EED000395DE /* AnsiCode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83D71E231B131C11000395DE /* AnsiCode.swift */; };
Expand Down Expand Up @@ -104,6 +105,7 @@
/* Begin PBXFileReference section */
5499CA961A2394B700783309 /* Components.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Components.plist; sourceTree = "<group>"; };
5499CA971A2394B700783309 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
65454F451B14D73800319A6C /* ControlStatementRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ControlStatementRule.swift; sourceTree = "<group>"; };
83894F211B0C928A006214E1 /* RulesCommand.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RulesCommand.swift; sourceTree = "<group>"; };
83894F231B0CAD41006214E1 /* StructuredText.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = StructuredText.swift; path = ../swiftlint/StructuredText.swift; sourceTree = "<group>"; };
83D71E231B131C11000395DE /* AnsiCode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AnsiCode.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -382,6 +384,7 @@
isa = PBXGroup;
children = (
E88DEA831B0990F500A66CB0 /* ColonRule.swift */,
65454F451B14D73800319A6C /* ControlStatementRule.swift */,
E88DEA891B0992B300A66CB0 /* FileLengthRule.swift */,
E88DEA7F1B09903300A66CB0 /* ForceCastRule.swift */,
E88DEA8F1B099A3100A66CB0 /* FunctionBodyLengthRule.swift */,
Expand Down Expand Up @@ -569,6 +572,7 @@
E88DEA941B099C0900A66CB0 /* VariableNameRule.swift in Sources */,
E88DEA8A1B0992B300A66CB0 /* FileLengthRule.swift in Sources */,
E88DEA751B09852000A66CB0 /* File+SwiftLint.swift in Sources */,
65454F471B14D76500319A6C /* ControlStatementRule.swift in Sources */,
E88DEA8E1B0999CD00A66CB0 /* TypeBodyLengthRule.swift in Sources */,
E88DEA6F1B09843F00A66CB0 /* Location.swift in Sources */,
E88DEA881B09924C00A66CB0 /* TrailingNewlineRule.swift in Sources */,
Expand Down