diff --git a/Rules.md b/Rules.md index 45388c12f..f6bc77320 100644 --- a/Rules.md +++ b/Rules.md @@ -10662,11 +10662,6 @@ firstLine secondLine ``` -```swift -firstLine - secondLine -``` - ```swift firstLine secondLine @@ -10675,14 +10670,6 @@ firstLine fourthLine ``` -```swift -firstLine - secondLine - thirdLine - - fourthLine -``` - ```swift firstLine secondLine @@ -10702,11 +10689,6 @@ fourthLine
Triggering Examples -```swift -firstLine - secondLine -``` - ```swift firstLine ``` @@ -10716,11 +10698,6 @@ firstLine secondLine ``` -```swift -firstLine - secondLine -``` - ```swift firstLine secondLine diff --git a/Source/SwiftLintFramework/Rules/Style/IndentationWidthRule.swift b/Source/SwiftLintFramework/Rules/Style/IndentationWidthRule.swift index f82cdc467..e86114733 100644 --- a/Source/SwiftLintFramework/Rules/Style/IndentationWidthRule.swift +++ b/Source/SwiftLintFramework/Rules/Style/IndentationWidthRule.swift @@ -28,24 +28,17 @@ public struct IndentationWidthRule: ConfigurationProviderRule, OptInRule { "unindent to match previous indentations. Don't indent the first line.", kind: .style, nonTriggeringExamples: [ - "firstLine\nsecondLine", // It's okay to keep the same indentation - "firstLine\n secondLine", // It's okay to indent using the specified indentationWidth - "firstLine\n\tsecondLine", // It's okay to indent using a tab - "firstLine\n\tsecondLine\n\t\tthirdLine\n\n\t\tfourthLine", // It's okay to have empty lines between - "firstLine\n\tsecondLine\n\t\tthirdLine\n \n\t\tfourthLine", // It's okay to have empty lines between - "firstLine\n\tsecondLine\n\t\tthirdLine\n//test\n\t\tfourthLine", // It's okay to have comment lines between + "firstLine\nsecondLine", + "firstLine\n secondLine", + "firstLine\n\tsecondLine\n\t\tthirdLine\n\n\t\tfourthLine", + "firstLine\n\tsecondLine\n\t\tthirdLine\n//test\n\t\tfourthLine", "firstLine\n secondLine\n thirdLine\nfourthLine" - // It's okay to unindent indentationWidth * (1, 2, 3, ...) ], triggeringExamples: [ - "firstLine\n\t secondLine", // It's not okay to indent using both tabs and spaces in one line - " firstLine", // It's not okay to have the first line indented - "firstLine\n secondLine", // It's not okay to indent using neither one tab or indentationWidth spaces - "firstLine\n\t\tsecondLine", // It's not okay to indent using multiple tabs + " firstLine", + "firstLine\n secondLine", "firstLine\n\tsecondLine\n\n\t\t\tfourthLine", - // It's okay to have empty lines between, but then, the following indentation must obey the rules "firstLine\n secondLine\n thirdLine\n fourthLine" - // It's not okay to unindent indentationWidth * (1, 2, 3, ...) - 3 ] ) diff --git a/Tests/LinuxMain.swift b/Tests/LinuxMain.swift index 6ba61a6a3..4e26370a5 100644 --- a/Tests/LinuxMain.swift +++ b/Tests/LinuxMain.swift @@ -692,7 +692,8 @@ extension ImplicitlyUnwrappedOptionalRuleTests { extension IndentationWidthRuleTests { static var allTests: [(String, (IndentationWidthRuleTests) -> () throws -> Void)] = [ - ("testWithDefaultConfiguration", testWithDefaultConfiguration) + ("testTriggeringExamples", testTriggeringExamples), + ("testNonTriggeringExamples", testNonTriggeringExamples) ] } diff --git a/Tests/SwiftLintFrameworkTests/IndentationWidthRuleTests.swift b/Tests/SwiftLintFrameworkTests/IndentationWidthRuleTests.swift index c44ab7dc2..4e6e883a8 100644 --- a/Tests/SwiftLintFrameworkTests/IndentationWidthRuleTests.swift +++ b/Tests/SwiftLintFrameworkTests/IndentationWidthRuleTests.swift @@ -3,10 +3,43 @@ import Foundation import XCTest class IndentationWidthRuleTests: XCTestCase { - func testWithDefaultConfiguration() { + func testTriggeringExamples() { + let triggeringExamples = [ + "firstLine\n\t secondLine", // It's not okay to indent using both tabs and spaces in one line + " firstLine", // It's not okay to have the first line indented + "firstLine\n secondLine", // It's not okay to indent using neither one tab or indentationWidth spaces + "firstLine\n\t\tsecondLine", // It's not okay to indent using multiple tabs + "firstLine\n\tsecondLine\n\n\t\t\tfourthLine", + // It's okay to have empty lines between, but then, the following indentation must obey the rules + "firstLine\n secondLine\n thirdLine\n fourthLine" + // It's not okay to unindent indentationWidth * (1, 2, 3, ...) - 3 + ] + // Don't do crazy testing as this triggers invalid warnings verifyRule( - IndentationWidthRule.description, + IndentationWidthRule.description.with(nonTriggeringExamples: [], triggeringExamples: triggeringExamples), + skipCommentTests: true, + skipDisableCommandTests: true, + testMultiByteOffsets: false, + testShebang: false + ) + } + + func testNonTriggeringExamples() { + let nonTriggeringExamples = [ + "firstLine\nsecondLine", // It's okay to keep the same indentation + "firstLine\n secondLine", // It's okay to indent using the specified indentationWidth + "firstLine\n\tsecondLine", // It's okay to indent using a tab + "firstLine\n\tsecondLine\n\t\tthirdLine\n\n\t\tfourthLine", // It's okay to have empty lines between + "firstLine\n\tsecondLine\n\t\tthirdLine\n \n\t\tfourthLine", // It's okay to have empty lines between + // "firstLine\n\tsecondLine\n\t\tthirdLine\n//test\n\t\tfourthLine", // It's okay to have comment lines between + "firstLine\n secondLine\n thirdLine\nfourthLine" + // It's okay to unindent indentationWidth * (1, 2, 3, ...) + ] + + // Don't do crazy testing as this triggers invalid warnings + verifyRule( + IndentationWidthRule.description.with(nonTriggeringExamples: nonTriggeringExamples, triggeringExamples: []), skipCommentTests: true, skipDisableCommandTests: true, testMultiByteOffsets: false,