mirror of
https://github.com/realm/SwiftLint.git
synced 2026-05-07 20:12:49 +00:00
Improve compile time by speeding up slow type inference expressions
This commit is contained in:
@@ -18,7 +18,7 @@ public struct JUnitReporter: Reporter {
|
||||
|
||||
public static func generateReport(_ violations: [StyleViolation]) -> String {
|
||||
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<testsuites><testsuite>" +
|
||||
violations.map({ violation in
|
||||
violations.map({ violation -> String in
|
||||
let fileName = (violation.location.file ?? "<nopath>").escapedForXML()
|
||||
let severity = violation.severity.rawValue + ":\n"
|
||||
let message = severity + "Line:" + String(violation.location.line ?? 0) + " "
|
||||
|
||||
@@ -203,7 +203,7 @@ public struct ColonRule: CorrectableRule, ConfigurationProviderRule {
|
||||
private typealias RangeWithKind = (range: NSRange, kind: ColonKind)
|
||||
|
||||
private func correctionRanges(in file: File) -> [RangeWithKind] {
|
||||
let violations = typeColonViolationRanges(in: file, matching: pattern).map {
|
||||
let violations: [RangeWithKind] = typeColonViolationRanges(in: file, matching: pattern).map {
|
||||
(range: $0, kind: ColonKind.type)
|
||||
}
|
||||
let dictionary = file.structure.dictionary
|
||||
|
||||
@@ -58,21 +58,23 @@ public struct ControlStatementRule: ConfigurationProviderRule {
|
||||
|
||||
public func validate(file: File) -> [StyleViolation] {
|
||||
let statements = ["if", "for", "guard", "switch", "while"]
|
||||
return statements.flatMap { statementKind -> [StyleViolation] in
|
||||
let pattern = statementKind == "guard"
|
||||
? "\(statementKind)\\s*\\([^,{]*\\)\\s*else\\s*\\{"
|
||||
: "\(statementKind)\\s*\\([^,{]*\\)\\s*\\{"
|
||||
return file.match(pattern: pattern).flatMap { match, syntaxKinds in
|
||||
let matchString = file.contents.substring(from: match.location, length: match.length)
|
||||
if isFalsePositive(matchString, syntaxKind: syntaxKinds.first) {
|
||||
return nil
|
||||
}
|
||||
return StyleViolation(ruleDescription: type(of: self).description,
|
||||
severity: configuration.severity,
|
||||
location: Location(file: file, characterOffset: match.location))
|
||||
}
|
||||
let statementPatterns: [String] = statements.map { statement -> String in
|
||||
let isGuard = statement == "guard"
|
||||
let elsePattern = isGuard ? "else\\s*" : ""
|
||||
return "\(statement)\\s*\\([^,{]*\\)\\s*\(elsePattern)\\{"
|
||||
}
|
||||
return statementPatterns.flatMap { pattern -> [StyleViolation] in
|
||||
return file.match(pattern: pattern)
|
||||
.filter { match, syntaxKinds -> Bool in
|
||||
let matchString = file.contents.substring(from: match.location, length: match.length)
|
||||
return !isFalsePositive(matchString, syntaxKind: syntaxKinds.first)
|
||||
}
|
||||
.map { match, _ -> StyleViolation in
|
||||
return StyleViolation(ruleDescription: type(of: self).description,
|
||||
severity: configuration.severity,
|
||||
location: Location(file: file, characterOffset: match.location))
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fileprivate func isFalsePositive(_ content: String, syntaxKind: SyntaxKind?) -> Bool {
|
||||
|
||||
@@ -47,8 +47,8 @@ public struct GenericTypeNameRule: ASTRule, ConfigurationProviderRule {
|
||||
"typealias StringDictionary<↓T_Foo> = Dictionary<String, T_Foo>\n",
|
||||
"typealias BackwardTriple<T1, ↓T2_Bar, T3> = (T3, T2_Bar, T1)\n",
|
||||
"typealias DictionaryOfStrings<↓T_Foo: Hashable> = Dictionary<T, String>\n"
|
||||
] + ["class", "struct", "enum"].flatMap { type in
|
||||
[
|
||||
] + ["class", "struct", "enum"].flatMap { type -> [String] in
|
||||
return [
|
||||
"\(type) Foo<↓T_Foo> {}\n",
|
||||
"\(type) Foo<T, ↓U_Foo> {}\n",
|
||||
"\(type) Foo<↓T_Foo, ↓U_Foo> {}\n",
|
||||
|
||||
@@ -21,15 +21,15 @@ public struct NestingRule: ASTRule, ConfigurationProviderRule {
|
||||
identifier: "nesting",
|
||||
name: "Nesting",
|
||||
description: "Types should be nested at most 1 level deep, " +
|
||||
"and statements should be nested at most 5 levels deep.",
|
||||
"and statements should be nested at most 5 levels deep.",
|
||||
kind: .metrics,
|
||||
nonTriggeringExamples: ["class", "struct", "enum"].flatMap { kind in
|
||||
nonTriggeringExamples: ["class", "struct", "enum"].flatMap { kind -> [String] in
|
||||
["\(kind) Class0 { \(kind) Class1 {} }\n",
|
||||
"func func0() {\nfunc func1() {\nfunc func2() {\nfunc func3() {\nfunc func4() { " +
|
||||
"func func5() {\n}\n}\n}\n}\n}\n}\n"]
|
||||
} + ["enum Enum0 { enum Enum1 { case Case } }"],
|
||||
triggeringExamples: ["class", "struct", "enum"].map { kind in
|
||||
"\(kind) A { \(kind) B { ↓\(kind) C {} } }\n"
|
||||
triggeringExamples: ["class", "struct", "enum"].map { kind -> String in
|
||||
return "\(kind) A { \(kind) B { ↓\(kind) C {} } }\n"
|
||||
} + [
|
||||
"func func0() {\nfunc func1() {\nfunc func2() {\nfunc func3() {\nfunc func4() { " +
|
||||
"func func5() {\n↓func func6() {\n}\n}\n}\n}\n}\n}\n}\n"
|
||||
|
||||
@@ -34,7 +34,7 @@ public struct NumberSeparatorRule: OptInRule, CorrectableRule, ConfigurationProv
|
||||
|
||||
private func violatingRanges(in file: File) -> [(NSRange, String)] {
|
||||
let numberTokens = file.syntaxMap.tokens.filter { SyntaxKind(rawValue: $0.type) == .number }
|
||||
return numberTokens.flatMap { token in
|
||||
return numberTokens.flatMap { (token: SyntaxToken) -> (NSRange, String)? in
|
||||
guard let content = contentFrom(file: file, token: token),
|
||||
isDecimal(number: content) else {
|
||||
return nil
|
||||
|
||||
@@ -101,7 +101,8 @@ public struct VerticalWhitespaceRule: CorrectableRule, ConfigurationProviderRule
|
||||
|
||||
var previousIndex = 0
|
||||
for (index, line) in lines.enumerated() {
|
||||
if lines[previousIndex].index + 1 == line.index {
|
||||
let previousLine: Line = lines[previousIndex]
|
||||
if previousLine.index + 1 == line.index {
|
||||
lineSection.append(line)
|
||||
} else if !lineSection.isEmpty {
|
||||
blankLinesSections.append(lineSection)
|
||||
|
||||
Reference in New Issue
Block a user