forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
21e3439
commit cec0a61
Showing
5 changed files
with
89 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
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,75 @@ | ||
// | ||
// ExplicitInitRule.swift | ||
// SwiftLint | ||
// | ||
// Created by Matt Taube on 7/2/16. | ||
// Copyright (c) 2016 Realm. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import SourceKittenFramework | ||
|
||
public struct ExplicitInitRule: ConfigurationProviderRule, CorrectableRule, OptInRule { | ||
|
||
private let pattern = "\\b([A-Z][A-Za-z]*)\\.init\\(" | ||
|
||
public var configuration = SeverityConfiguration(.Warning) | ||
|
||
public init() {} | ||
|
||
public static let description = RuleDescription( | ||
identifier: "explicit_init", | ||
name: "Explicit Init", | ||
description: "Explicitly calling .init() should be avoided.", | ||
nonTriggeringExamples: [ | ||
"self.init(", | ||
"self.init", | ||
"Abc.init", | ||
"abc.init(", | ||
"$0.init(" | ||
], | ||
triggeringExamples: [ | ||
"Abc.init(", | ||
"Abc(NSURL.init(someString" | ||
], | ||
corrections: [ | ||
"Abc.init(": "Abc(", | ||
"Abc(NSURL.init(someString": "Abc(NSURL(someString" | ||
] | ||
) | ||
|
||
public func validateFile(file: File) -> [StyleViolation] { | ||
return violationRangesInFile(file).flatMap { range in | ||
return StyleViolation(ruleDescription: self.dynamicType.description, | ||
severity: configuration.severity, | ||
location: Location(file: file, characterOffset: range.location)) | ||
} | ||
} | ||
|
||
private func violationRangesInFile(file: File) -> [NSRange] { | ||
let excludingKinds = SyntaxKind.commentAndStringKinds() | ||
|
||
return file.matchPattern(pattern, excludingSyntaxKinds: excludingKinds) | ||
} | ||
|
||
public func correctFile(file: File) -> [Correction] { | ||
let matches = violationRangesInFile(file) | ||
guard !matches.isEmpty else { return [] } | ||
|
||
let regularExpression = regex(pattern) | ||
let description = self.dynamicType.description | ||
var corrections = [Correction]() | ||
var contents = file.contents | ||
for range in matches.reverse() { | ||
contents = regularExpression.stringByReplacingMatchesInString(contents, | ||
options: [], | ||
range: range, | ||
withTemplate: "$1(") | ||
let location = Location(file: file, characterOffset: range.location) | ||
corrections.append(Correction(ruleDescription: description, location: location)) | ||
} | ||
|
||
file.write(contents) | ||
return corrections | ||
} | ||
} |
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