Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration for NestingRule #1340

Merged
merged 2 commits into from
Mar 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@
[Natan Rolnik](https://github.com/NatanRolnik)
[#1270](https://github.com/realm/SwiftLint/issues/1270)

* Now `nesting` rule can be configured with a type and statement level.
[Hayashi Tatsuya](https://github.com/sora0077)
[#1318](https://github.com/realm/SwiftLint/issues/1318)

##### Bug Fixes

* Fix a false positive on `large_tuple` rule when using closures.
Expand Down
23 changes: 13 additions & 10 deletions Source/SwiftLintFramework/Rules/NestingRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import SourceKittenFramework

public struct NestingRule: ASTRule, ConfigurationProviderRule {

public var configuration = SeverityConfiguration(.warning)
public var configuration = NestingConfiguration(typeLevelWarning: 1,
typeLevelError: nil,
statementLevelWarning: 5,
statementLevelError: nil)

public init() {}

Expand Down Expand Up @@ -42,16 +45,16 @@ public struct NestingRule: ASTRule, ConfigurationProviderRule {
var violations = [StyleViolation]()
let typeKinds = SwiftDeclarationKind.typeKinds()
if let offset = dictionary.offset {
if level > 1 && typeKinds.contains(kind) {
violations.append(StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
let (targetName, targetLevel) = typeKinds.contains(kind)
? ("Types", configuration.typeLevel) : ("Statements", configuration.statementLevel)
if let severity = configuration.severity(with: targetLevel, for: level) {
let threshold = configuration.threshold(with: targetLevel, for: severity)
let pluralSuffix = threshold > 1 ? "s" : ""
violations.append(StyleViolation(
ruleDescription: type(of: self).description,
severity: severity,
location: Location(file: file, byteOffset: offset),
reason: "Types should be nested at most 1 level deep"))
} else if level > 5 {
violations.append(StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
location: Location(file: file, byteOffset: offset),
reason: "Statements should be nested at most 5 levels deep"))
reason: "\(targetName) should be nested at most \(threshold) level\(pluralSuffix) deep"))
}
}
violations.append(contentsOf: dictionary.substructure.flatMap { subDict in
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// NestingConfiguration.swift
// SwiftLint
//
// Created by 林達也 on 03/03/16.
// Copyright © 2017 Realm. All rights reserved.
//

import Foundation

public struct NestingConfiguration: RuleConfiguration, Equatable {
public var consoleDescription: String {
return "(type_level) \(typeLevel.shortConsoleDescription), " +
"(statement_level) \(statementLevel.shortConsoleDescription)"
}

var typeLevel: SeverityLevelsConfiguration
var statementLevel: SeverityLevelsConfiguration

public init(typeLevelWarning: Int,
typeLevelError: Int?,
statementLevelWarning: Int,
statementLevelError: Int?) {
typeLevel = SeverityLevelsConfiguration(warning: typeLevelWarning, error: typeLevelError)
statementLevel = SeverityLevelsConfiguration(warning: statementLevelWarning, error: statementLevelError)
}

public mutating func apply(configuration: Any) throws {
guard let configurationDict = configuration as? [String: Any] else {
throw ConfigurationError.unknownConfiguration
}

if let typeLevelConfiguration = configurationDict["type_level"] {
try typeLevel.apply(configuration: typeLevelConfiguration)
}
if let statementLevelConfiguration = configurationDict["statement_level"] {
try statementLevel.apply(configuration: statementLevelConfiguration)
}
}

func severity(with config: SeverityLevelsConfiguration, for level: Int) -> ViolationSeverity? {
if let error = config.error, level > error {
return .error
} else if level > config.warning {
return .warning
}
return nil
}

func threshold(with config: SeverityLevelsConfiguration, for severity: ViolationSeverity) -> Int {
switch severity {
case .error: return config.error ?? config.warning
case .warning: return config.warning
}
}
}

public func == (lhs: NestingConfiguration, rhs: NestingConfiguration) -> Bool {
return lhs.typeLevel == rhs.typeLevel
&& lhs.statementLevel == rhs.statementLevel
}
8 changes: 6 additions & 2 deletions SwiftLint.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@
D286EC021E02DF6F0003CF72 /* SortedImportsRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = D286EC001E02DA190003CF72 /* SortedImportsRule.swift */; };
D40AD08A1E032F9700F48C30 /* UnusedClosureParameterRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = D40AD0891E032F9700F48C30 /* UnusedClosureParameterRule.swift */; };
D40F83881DE9179200524C62 /* TrailingCommaConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = D40F83871DE9179200524C62 /* TrailingCommaConfiguration.swift */; };
D4130D991E16CC1300242361 /* TypeNameRuleExamples.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4130D981E16CC1300242361 /* TypeNameRuleExamples.swift */; };
D4130D971E16183F00242361 /* IdentifierNameRuleExamples.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4130D961E16183F00242361 /* IdentifierNameRuleExamples.swift */; };
D4130D991E16CC1300242361 /* TypeNameRuleExamples.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4130D981E16CC1300242361 /* TypeNameRuleExamples.swift */; };
D41E7E0B1DF9DABB0065259A /* RedundantStringEnumValueRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = D41E7E0A1DF9DABB0065259A /* RedundantStringEnumValueRule.swift */; };
D42D2B381E09CC0D00CD7A2E /* FirstWhereRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = D42D2B371E09CC0D00CD7A2E /* FirstWhereRule.swift */; };
D4348EEA1C46122C007707FB /* FunctionBodyLengthRuleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4348EE91C46122C007707FB /* FunctionBodyLengthRuleTests.swift */; };
Expand Down Expand Up @@ -148,6 +148,7 @@
D4DAE8BC1DE14E8F00B0AE7A /* NimbleOperatorRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4DAE8BB1DE14E8F00B0AE7A /* NimbleOperatorRule.swift */; };
D4FBADD01E00DA0400669C73 /* OperatorUsageWhitespaceRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4FBADCF1E00DA0400669C73 /* OperatorUsageWhitespaceRule.swift */; };
D4FD58B21E12A0200019503C /* LinterCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = D4FD58B11E12A0200019503C /* LinterCache.swift */; };
D93DA3D11E699E6300809827 /* NestingConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = D93DA3CF1E699E4E00809827 /* NestingConfiguration.swift */; };
DAD3BE4A1D6ECD9500660239 /* PrivateOutletRuleConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAD3BE491D6ECD9500660239 /* PrivateOutletRuleConfiguration.swift */; };
E315B83C1DFA4BC500621B44 /* DynamicInlineRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = E315B83B1DFA4BC500621B44 /* DynamicInlineRule.swift */; };
E57B23C11B1D8BF000DEA512 /* ReturnArrowWhitespaceRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = E57B23C01B1D8BF000DEA512 /* ReturnArrowWhitespaceRule.swift */; };
Expand Down Expand Up @@ -368,8 +369,8 @@
D286EC001E02DA190003CF72 /* SortedImportsRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SortedImportsRule.swift; sourceTree = "<group>"; };
D40AD0891E032F9700F48C30 /* UnusedClosureParameterRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UnusedClosureParameterRule.swift; sourceTree = "<group>"; };
D40F83871DE9179200524C62 /* TrailingCommaConfiguration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TrailingCommaConfiguration.swift; sourceTree = "<group>"; };
D4130D981E16CC1300242361 /* TypeNameRuleExamples.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TypeNameRuleExamples.swift; sourceTree = "<group>"; };
D4130D961E16183F00242361 /* IdentifierNameRuleExamples.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = IdentifierNameRuleExamples.swift; sourceTree = "<group>"; };
D4130D981E16CC1300242361 /* TypeNameRuleExamples.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TypeNameRuleExamples.swift; sourceTree = "<group>"; };
D41E7E0A1DF9DABB0065259A /* RedundantStringEnumValueRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RedundantStringEnumValueRule.swift; sourceTree = "<group>"; };
D42D2B371E09CC0D00CD7A2E /* FirstWhereRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FirstWhereRule.swift; sourceTree = "<group>"; };
D4348EE91C46122C007707FB /* FunctionBodyLengthRuleTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FunctionBodyLengthRuleTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -422,6 +423,7 @@
D4DAE8BB1DE14E8F00B0AE7A /* NimbleOperatorRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NimbleOperatorRule.swift; sourceTree = "<group>"; };
D4FBADCF1E00DA0400669C73 /* OperatorUsageWhitespaceRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OperatorUsageWhitespaceRule.swift; sourceTree = "<group>"; };
D4FD58B11E12A0200019503C /* LinterCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LinterCache.swift; sourceTree = "<group>"; };
D93DA3CF1E699E4E00809827 /* NestingConfiguration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NestingConfiguration.swift; sourceTree = "<group>"; };
DAD3BE491D6ECD9500660239 /* PrivateOutletRuleConfiguration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PrivateOutletRuleConfiguration.swift; sourceTree = "<group>"; };
E315B83B1DFA4BC500621B44 /* DynamicInlineRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DynamicInlineRule.swift; sourceTree = "<group>"; };
E57B23C01B1D8BF000DEA512 /* ReturnArrowWhitespaceRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReturnArrowWhitespaceRule.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -548,6 +550,7 @@
D4C4A3511DEFBBB700E0E04C /* FileHeaderConfiguration.swift */,
3B034B6C1E0BE544005D49A9 /* LineLengthConfiguration.swift */,
3BCC04D01C4F56D3006073C3 /* NameConfiguration.swift */,
D93DA3CF1E699E4E00809827 /* NestingConfiguration.swift */,
D4DA1DFD1E1A10DB0037413D /* NumberSeparatorConfiguration.swift */,
78F032471D7D614300BE709A /* OverridenSuperCallConfiguration.swift */,
DAD3BE491D6ECD9500660239 /* PrivateOutletRuleConfiguration.swift */,
Expand Down Expand Up @@ -1251,6 +1254,7 @@
D48AE2CC1DFB58C5001C6A4A /* AttributesRulesExamples.swift in Sources */,
E88DEA6F1B09843F00A66CB0 /* Location.swift in Sources */,
D43B046B1E075905004016AF /* ClosureEndIndentationRule.swift in Sources */,
D93DA3D11E699E6300809827 /* NestingConfiguration.swift in Sources */,
93E0C3CE1D67BD7F007FA25D /* ConditionalReturnsOnNewline.swift in Sources */,
D43DB1081DC573DA00281215 /* ImplicitGetterRule.swift in Sources */,
7C0C2E7A1D2866CB0076435A /* ExplicitInitRule.swift in Sources */,
Expand Down
42 changes: 41 additions & 1 deletion Tests/SwiftLintFrameworkTests/RuleConfigurationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import SourceKittenFramework
@testable import SwiftLintFramework
import XCTest

class RuleConfigurationsTests: XCTestCase {
// swiftlint:disable type_body_length

class RuleConfigurationsTests: XCTestCase {
func testNameConfigurationSetsCorrectly() {
let config = [ "min_length": ["warning": 17, "error": 7],
"max_length": ["warning": 170, "error": 700],
Expand Down Expand Up @@ -68,6 +69,41 @@ class RuleConfigurationsTests: XCTestCase {
XCTAssertEqual(nameConfig.maxLengthThreshold, 17)
}

func testNestingConfigurationSetsCorrectly() {
let config = [
"type_level": [
"warning": 7, "error": 17
],
"statement_level": [
"warning": 8, "error": 18
]
] as [String: Any]
var nestingConfig = NestingConfiguration(typeLevelWarning: 0,
typeLevelError: nil,
statementLevelWarning: 0,
statementLevelError: nil)
do {
try nestingConfig.apply(configuration: config)
XCTAssertEqual(nestingConfig.typeLevel.warning, 7)
XCTAssertEqual(nestingConfig.statementLevel.warning, 8)
XCTAssertEqual(nestingConfig.typeLevel.error, 17)
XCTAssertEqual(nestingConfig.statementLevel.error, 18)
} catch {
XCTFail()
}
}

func testNestingConfigurationThrowsOnBadConfig() {
let config = 17
var nestingConfig = NestingConfiguration(typeLevelWarning: 0,
typeLevelError: nil,
statementLevelWarning: 0,
statementLevelError: nil)
checkError(ConfigurationError.unknownConfiguration) {
try nestingConfig.apply(configuration: config)
}
}

func testSeverityConfigurationFromString() {
let config = "Warning"
let comp = SeverityConfiguration(.warning)
Expand Down Expand Up @@ -287,6 +323,10 @@ extension RuleConfigurationsTests {
testNameConfigurationMinLengthThreshold),
("testNameConfigurationMaxLengthThreshold",
testNameConfigurationMaxLengthThreshold),
("testNestingConfigurationSetsCorrectly",
testNestingConfigurationSetsCorrectly),
("testNestingConfigurationThrowsOnBadConfig",
testNestingConfigurationThrowsOnBadConfig),
("testSeverityConfigurationFromString",
testSeverityConfigurationFromString),
("testSeverityConfigurationFromDictionary",
Expand Down