-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make
no_empty_block
rule opt-in and add configurations (#5685)
- Loading branch information
Showing
5 changed files
with
186 additions
and
48 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
25 changes: 25 additions & 0 deletions
25
Source/SwiftLintBuiltInRules/Rules/RuleConfigurations/NoEmptyBlockConfiguration.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,25 @@ | ||
import SwiftLintCore | ||
|
||
@AutoApply | ||
struct NoEmptyBlockConfiguration: SeverityBasedRuleConfiguration { | ||
typealias Parent = NoEmptyBlockRule | ||
|
||
@MakeAcceptableByConfigurationElement | ||
enum CodeBlockType: String, CaseIterable { | ||
case functionBodies = "function_bodies" | ||
case initializerBodies = "initializer_bodies" | ||
case statementBlocks = "statement_blocks" | ||
|
||
static let all = Set(allCases) | ||
} | ||
|
||
@ConfigurationElement(key: "severity") | ||
private(set) var severityConfiguration = SeverityConfiguration<Parent>(.warning) | ||
|
||
@ConfigurationElement(key: "disabled_block_types") | ||
private(set) var disabledBlockTypes: [CodeBlockType] = [] | ||
|
||
var enabledBlockTypes: Set<CodeBlockType> { | ||
CodeBlockType.all.subtracting(disabledBlockTypes) | ||
} | ||
} |
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
54 changes: 54 additions & 0 deletions
54
Tests/SwiftLintFrameworkTests/NoEmptyBlockConfigurationTests.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,54 @@ | ||
@testable import SwiftLintBuiltInRules | ||
@testable import SwiftLintCore | ||
import XCTest | ||
|
||
final class NoEmptyBlockConfigurationTests: SwiftLintTestCase { | ||
func testDefaultConfiguration() { | ||
let config = NoEmptyBlockConfiguration() | ||
XCTAssertEqual(config.severityConfiguration.severity, .warning) | ||
XCTAssertEqual(config.enabledBlockTypes, NoEmptyBlockConfiguration.CodeBlockType.all) | ||
} | ||
|
||
func testApplyingCustomConfiguration() throws { | ||
var config = NoEmptyBlockConfiguration() | ||
try config.apply( | ||
configuration: [ | ||
"severity": "error", | ||
"disabled_block_types": ["function_bodies"], | ||
] as [String: any Sendable] | ||
) | ||
XCTAssertEqual(config.severityConfiguration.severity, .error) | ||
XCTAssertEqual(config.enabledBlockTypes, Set([.initializerBodies, .statementBlocks])) | ||
} | ||
|
||
func testInvalidKeyInCustomConfiguration() { | ||
var config = NoEmptyBlockConfiguration() | ||
XCTAssertEqual( | ||
try Issue.captureConsole { try config.apply(configuration: ["invalidKey": "error"]) }, | ||
"warning: Configuration for 'no_empty_block' rule contains the invalid key(s) 'invalidKey'." | ||
) | ||
} | ||
|
||
func testInvalidTypeOfCustomConfiguration() { | ||
var config = NoEmptyBlockConfiguration() | ||
checkError(Issue.invalidConfiguration(ruleID: NoEmptyBlockRule.description.identifier)) { | ||
try config.apply(configuration: "invalidKey") | ||
} | ||
} | ||
|
||
func testInvalidTypeOfValueInCustomConfiguration() { | ||
var config = NoEmptyBlockConfiguration() | ||
checkError(Issue.invalidConfiguration(ruleID: NoEmptyBlockRule.description.identifier)) { | ||
try config.apply(configuration: ["severity": "foo"]) | ||
} | ||
} | ||
|
||
func testConsoleDescription() throws { | ||
var config = NoEmptyBlockConfiguration() | ||
try config.apply(configuration: ["disabled_block_types": ["initializer_bodies", "statement_blocks"]]) | ||
XCTAssertEqual( | ||
RuleConfigurationDescription.from(configuration: config).oneLiner(), | ||
"severity: warning; disabled_block_types: [initializer_bodies, statement_blocks]" | ||
) | ||
} | ||
} |