mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
57025565d5
The duration of `ColonRule` on linting Carthage 0.11 is reduced from 2287ms to 1581ms by Instruments.
136 lines
5.7 KiB
Swift
136 lines
5.7 KiB
Swift
//
|
|
// ColonRule.swift
|
|
// SwiftLint
|
|
//
|
|
// Created by JP Simard on 2015-05-16.
|
|
// Copyright (c) 2015 Realm. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import SourceKittenFramework
|
|
|
|
public struct ColonRule: CorrectableRule, ConfigProviderRule {
|
|
|
|
public var config = SeverityConfig(.Warning)
|
|
|
|
public init() {}
|
|
|
|
public static let description = RuleDescription(
|
|
identifier: "colon",
|
|
name: "Colon",
|
|
description: "Colons should be next to the identifier when specifying a type.",
|
|
nonTriggeringExamples: [
|
|
"let abc: Void\n",
|
|
"let abc: [Void: Void]\n",
|
|
"let abc: (Void, Void)\n",
|
|
"let abc: ([Void], String, Int)\n",
|
|
"let abc: [([Void], String, Int)]\n",
|
|
"let abc: String=\"def\"\n",
|
|
"let abc: Int=0\n",
|
|
"let abc: Enum=Enum.Value\n",
|
|
"func abc(def: Void) {}\n",
|
|
"func abc(def: Void, ghi: Void) {}\n",
|
|
"// 周斌佳年周斌佳\nlet abc: String = \"abc:\""
|
|
],
|
|
triggeringExamples: [
|
|
"let ↓abc:Void\n",
|
|
"let ↓abc: Void\n",
|
|
"let ↓abc :Void\n",
|
|
"let ↓abc : Void\n",
|
|
"let ↓abc : [Void: Void]\n",
|
|
"let ↓abc : (Void, String, Int)\n",
|
|
"let ↓abc : ([Void], String, Int)\n",
|
|
"let ↓abc : [([Void], String, Int)]\n",
|
|
"let ↓abc: (Void, String, Int)\n",
|
|
"let ↓abc: ([Void], String, Int)\n",
|
|
"let ↓abc: [([Void], String, Int)]\n",
|
|
"let ↓abc :String=\"def\"\n",
|
|
"let ↓abc :Int=0\n",
|
|
"let ↓abc :Int = 0\n",
|
|
"let ↓abc:Int=0\n",
|
|
"let ↓abc:Int = 0\n",
|
|
"let ↓abc:Enum=Enum.Value\n",
|
|
"func abc(↓def:Void) {}\n",
|
|
"func abc(↓def: Void) {}\n",
|
|
"func abc(↓def :Void) {}\n",
|
|
"func abc(↓def : Void) {}\n",
|
|
"func abc(def: Void, ↓ghi :Void) {}\n"
|
|
],
|
|
corrections: [
|
|
"let abc:Void\n": "let abc: Void\n",
|
|
"let abc: Void\n": "let abc: Void\n",
|
|
"let abc :Void\n": "let abc: Void\n",
|
|
"let abc : Void\n": "let abc: Void\n",
|
|
"let abc : [Void: Void]\n": "let abc: [Void: Void]\n",
|
|
"let abc : (Void, String, Int)\n": "let abc: (Void, String, Int)\n",
|
|
"let abc : ([Void], String, Int)\n": "let abc: ([Void], String, Int)\n",
|
|
"let abc : [([Void], String, Int)]\n": "let abc: [([Void], String, Int)]\n",
|
|
"let abc: (Void, String, Int)\n": "let abc: (Void, String, Int)\n",
|
|
"let abc: ([Void], String, Int)\n": "let abc: ([Void], String, Int)\n",
|
|
"let abc: [([Void], String, Int)]\n": "let abc: [([Void], String, Int)]\n",
|
|
"let abc :String=\"def\"\n": "let abc: String=\"def\"\n",
|
|
"let abc :Int=0\n": "let abc: Int=0\n",
|
|
"let abc :Int = 0\n": "let abc: Int = 0\n",
|
|
"let abc:Int=0\n": "let abc: Int=0\n",
|
|
"let abc:Int = 0\n": "let abc: Int = 0\n",
|
|
"let abc:Enum=Enum.Value\n": "let abc: Enum=Enum.Value\n",
|
|
"func abc(def:Void) {}\n": "func abc(def: Void) {}\n",
|
|
"func abc(def: Void) {}\n": "func abc(def: Void) {}\n",
|
|
"func abc(def :Void) {}\n": "func abc(def: Void) {}\n",
|
|
"func abc(def : Void) {}\n": "func abc(def: Void) {}\n",
|
|
"func abc(def: Void, ghi :Void) {}\n": "func abc(def: Void, ghi: Void) {}\n"
|
|
]
|
|
)
|
|
|
|
public func validateFile(file: File) -> [StyleViolation] {
|
|
return violationRangesInFile(file, withPattern: pattern).flatMap { range in
|
|
return StyleViolation(ruleDescription: self.dynamicType.description,
|
|
severity: config.severity,
|
|
location: Location(file: file, characterOffset: range.location))
|
|
}
|
|
}
|
|
|
|
public func correctFile(file: File) -> [Correction] {
|
|
let matches = violationRangesInFile(file, withPattern: pattern)
|
|
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: $2")
|
|
let location = Location(file: file, characterOffset: range.location)
|
|
corrections.append(Correction(ruleDescription: description, location: location))
|
|
}
|
|
file.write(contents)
|
|
return corrections
|
|
}
|
|
|
|
// MARK: - Private
|
|
|
|
private let pattern =
|
|
"(\\w+)" + // Capture an identifier
|
|
"(?:" + // start group
|
|
"\\s+" + // followed by whitespace
|
|
":" + // to the left of a colon
|
|
"\\s*" + // followed by any amount of whitespace.
|
|
"|" + // or
|
|
":" + // immediately followed by a colon
|
|
"(?:\\s{0}|\\s{2,})" + // followed by 0 or 2+ whitespace characters.
|
|
")" + // end group
|
|
"(" + // Capture a type identifier
|
|
"(?:\\[|\\()*" + // which may begin with a series of nested parenthesis or brackets
|
|
"\\S+?)" // lazily to the first non-whitespace character.
|
|
|
|
private func violationRangesInFile(file: File, withPattern pattern: String) -> [NSRange] {
|
|
return file.matchPattern(pattern).filter { range, syntaxKinds in
|
|
if !syntaxKinds.startsWith([.Identifier, .Typeidentifier]) {
|
|
return false
|
|
}
|
|
return Set(syntaxKinds).intersect(Set(SyntaxKind.commentAndStringKinds())).isEmpty
|
|
}.flatMap { $0.0 }
|
|
}
|
|
}
|