-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #338 from realm/jp-add-conditional-binding-cascade…
…-rule add ConditionalBindingCascadeRule
- Loading branch information
Showing
5 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
Source/SwiftLintFramework/Rules/ConditionalBindingCascadeRule.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// | ||
// ConditionalBindingCascadeRule.swift | ||
// SwiftLint | ||
// | ||
// Created by Aaron McTavish on 08/01/2016. | ||
// Copyright © 2016 Realm. All rights reserved. | ||
// | ||
|
||
import SourceKittenFramework | ||
|
||
public struct ConditionalBindingCascadeRule: Rule { | ||
public static let description = RuleDescription( | ||
identifier: "conditional_binding_cascade", | ||
name: "Conditional Binding Cascade", | ||
description: "Repeated `let` statements in conditional binding cascade should be avoided.", | ||
nonTriggeringExamples: [ | ||
"if let a = b, c = d {", | ||
"if let a = b, \n c = d {", | ||
"if let a = b, \n c = d \n {", | ||
"if let a = b { if let c = d {", | ||
"if let a = b { let c = d({ foo in ... })", | ||
"guard let a = b, c = d else {" | ||
], | ||
triggeringExamples: [ | ||
"if let a = b, let c = d {", | ||
"if let a = b, \n let c = d {", | ||
"if let a = b, c = d, let e = f {", | ||
"if let a = b, let c = d \n {", | ||
"if \n let a = b, let c = d {", | ||
"if let a = b, c = d.indexOf({$0 == e}), let f = g {", | ||
"guard let a = b, let c = d else {" | ||
] | ||
) | ||
|
||
public func validateFile(file: File) -> [StyleViolation] { | ||
let conditionalBindingKeywords = ["if", "guard"] | ||
let pattern = "^(" + | ||
conditionalBindingKeywords.joinWithSeparator("|") + | ||
")(\\s*?)let((.|\\s)*?),(\\s*?)let((.|\\s)*?)\\{" | ||
return file.matchPattern(pattern, | ||
excludingSyntaxKinds: SyntaxKind.commentAndStringKinds()).map { | ||
StyleViolation(ruleDescription: self.dynamicType.description, | ||
location: Location(file: file, characterOffset: $0.location)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters