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.
Implements realm#1779 (Limit number of QuickSpecs in file to one).
- Loading branch information
1 parent
1a50451
commit 8d478f7
Showing
7 changed files
with
137 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
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,57 @@ | ||
// | ||
// QuickSpecLimitRule.swift | ||
// SwiftLint | ||
// | ||
// Created by Ornithologist Coder on 8/15/17. | ||
// Copyright © 2017 Realm. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import SourceKittenFramework | ||
|
||
public struct QuickSingleSpecRule: Rule, OptInRule, ConfigurationProviderRule { | ||
public var configuration = SeverityConfiguration(.warning) | ||
|
||
public init() {} | ||
|
||
public static let description = RuleDescription( | ||
identifier: "quick_single_spec", | ||
name: "Quick Single Spec", | ||
description: "Test files should contain a single QuickSpec class.", | ||
kind: .style, | ||
nonTriggeringExamples: [ | ||
"class FooTests { }\n", | ||
"class FooTests: QuickSpec { }\n" | ||
], | ||
triggeringExamples: [ | ||
"↓class FooTests: QuickSpec { }\n↓class BarTests: QuickSpec { }\n", | ||
"↓class FooTests: QuickSpec { }\n↓class BarTests: QuickSpec { }\n↓class TotoTests: QuickSpec { }\n" | ||
] | ||
) | ||
|
||
public func validate(file: File) -> [StyleViolation] { | ||
let specs = quickSpecs(in: file) | ||
|
||
guard specs.count > 1 else { return [] } | ||
|
||
return specs.flatMap(toViolation(in: file, configuration: configuration, numberOfSpecs: specs.count)) | ||
} | ||
|
||
// MARK: - Private | ||
|
||
private func quickSpecs(in file: File) -> [[String: SourceKitRepresentable]] { | ||
return file.structure.dictionary.substructure.filter { $0.inheritedTypes.contains("QuickSpec") } | ||
} | ||
|
||
private func toViolation(in file: File, | ||
configuration: SeverityConfiguration, | ||
numberOfSpecs: Int) -> ([String: SourceKitRepresentable]) -> StyleViolation? { | ||
return { dictionary in | ||
guard let offset = dictionary.offset else { return nil } | ||
return StyleViolation(ruleDescription: type(of: self).description, | ||
severity: configuration.severity, | ||
location: Location(file: file, byteOffset: offset), | ||
reason: "\(numberOfSpecs) Quick Specs found in this file.") | ||
} | ||
} | ||
} |
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