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.
Add quick_discouraged_call opt-in rule
Implements realm#1781 (Method calls and object initialization inside Quick 'describe' and 'context' blocks can be harmful)
- Loading branch information
1 parent
2794785
commit 04202ba
Showing
8 changed files
with
485 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
107 changes: 107 additions & 0 deletions
107
Source/SwiftLintFramework/Rules/QuickDiscouragedCallRule.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,107 @@ | ||
// | ||
// QuickInitializationRule.swift | ||
// SwiftLint | ||
// | ||
// Created by Ornithologist Coder on 8/11/17. | ||
// Copyright © 2017 Realm. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import SourceKittenFramework | ||
|
||
// swiftlint:disable identifier_name | ||
|
||
public struct QuickDiscouragedCallRule: ASTRule, OptInRule, ConfigurationProviderRule { | ||
public var configuration = SeverityConfiguration(.warning) | ||
|
||
public init() {} | ||
|
||
public static let description = RuleDescription( | ||
identifier: "quick_discouraged_call", | ||
name: "Quick Discouraged Call", | ||
description: "Discouraged call inside 'describe' and/or 'context' block.", | ||
kind: .style, | ||
nonTriggeringExamples: QuickDiscouragedCallRuleExamples.nonTriggeringExamples, | ||
triggeringExamples: QuickDiscouragedCallRuleExamples.triggeringExamples | ||
) | ||
|
||
public func validate(file: File, | ||
kind: SwiftExpressionKind, | ||
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] { | ||
guard | ||
fileContainsQuickSpec(file: file), | ||
kind == .call, | ||
let name = dictionary.name, | ||
let kindName = QuickCallKind(rawValue: name), | ||
QuickCallKind.restrictiveKinds().contains(kindName) | ||
else { return [] } | ||
|
||
return violationOffsets(in: dictionary.enclosedArguments) | ||
.map { | ||
StyleViolation(ruleDescription: type(of: self).description, | ||
severity: configuration.severity, | ||
location: Location(file: file, byteOffset: $0)) | ||
} | ||
} | ||
|
||
// MARK: - Private | ||
|
||
private func fileContainsQuickSpec(file: File) -> Bool { | ||
return !file.structure.dictionary.substructure.filter { $0.inheritedTypes.contains("QuickSpec") }.isEmpty | ||
} | ||
|
||
typealias ViolationOffset = Int | ||
|
||
private func violationOffsets(in substructure: [[String: SourceKitRepresentable]]) -> [ViolationOffset] { | ||
return substructure.flatMap { dictionary -> [ViolationOffset] in | ||
return dictionary.substructure.flatMap(toViolationOffsets) | ||
} | ||
} | ||
|
||
private func toViolationOffsets(dictionary: [String: SourceKitRepresentable]) -> [ViolationOffset] { | ||
guard | ||
let kind = dictionary.kind, | ||
let offset = dictionary.offset | ||
else { return [] } | ||
|
||
if SwiftExpressionKind(rawValue: kind) == .call, | ||
let name = dictionary.name, QuickCallKind(rawValue: name) == nil { | ||
return [offset] | ||
} | ||
|
||
guard SwiftExpressionKind(rawValue: kind) != .call else { return [] } | ||
|
||
return dictionary.substructure.flatMap(toViolationOffset) | ||
} | ||
|
||
private func toViolationOffset(dictionary: [String: SourceKitRepresentable]) -> ViolationOffset? { | ||
guard | ||
let name = dictionary.name, | ||
let offset = dictionary.offset, | ||
let kind = dictionary.kind, | ||
SwiftExpressionKind(rawValue: kind) == .call, | ||
QuickCallKind(rawValue: name) == nil | ||
else { return nil } | ||
|
||
return offset | ||
} | ||
} | ||
|
||
// MARK: - Private | ||
|
||
private enum QuickCallKind: String { | ||
case describe | ||
case context | ||
case sharedExamples | ||
case itBehavesLike | ||
case beforeEach | ||
case beforeSuite | ||
case afterEach | ||
case afterSuite | ||
case it | ||
case pending | ||
|
||
static func restrictiveKinds() -> [QuickCallKind] { | ||
return [.describe, .context, .sharedExamples] | ||
} | ||
} |
Oops, something went wrong.