diff --git a/CHANGELOG.md b/CHANGELOG.md index 76fc98b79..2c0294c57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,7 +16,10 @@ ### Bug Fixes -* None. +* Ignore function, initializer and subscript declarations alike when the + `ignores_function_declarations` option is enabled in the `line_length` rule. + [SimplyDanny](https://github.com/SimplyDanny) + [#6241](https://github.com/realm/SwiftLint/issues/6241) ## 0.61.0: Even Fresher Breeze diff --git a/Source/SwiftLintBuiltInRules/Rules/Metrics/LineLengthRule.swift b/Source/SwiftLintBuiltInRules/Rules/Metrics/LineLengthRule.swift index 862f359d2..28ae7a1df 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Metrics/LineLengthRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Metrics/LineLengthRule.swift @@ -147,6 +147,18 @@ private final class FunctionLineVisitor: SyntaxVisitor { } override func visitPost(_ node: FunctionDeclSyntax) { + collectLines(from: node) + } + + override func visitPost(_ node: InitializerDeclSyntax) { + collectLines(from: node) + } + + override func visitPost(_ node: SubscriptDeclSyntax) { + collectLines(from: node) + } + + private func collectLines(from node: any SyntaxProtocol) { let startLocation = locationConverter.location(for: node.positionAfterSkippingLeadingTrivia) let endLocation = locationConverter.location(for: node.endPositionBeforeTrailingTrivia) for line in startLocation.line...endLocation.line { diff --git a/Tests/BuiltInRulesTests/LineLengthRuleTests.swift b/Tests/BuiltInRulesTests/LineLengthRuleTests.swift index 25f4a5525..8bc678af0 100644 --- a/Tests/BuiltInRulesTests/LineLengthRuleTests.swift +++ b/Tests/BuiltInRulesTests/LineLengthRuleTests.swift @@ -7,12 +7,35 @@ final class LineLengthRuleTests: SwiftLintTestCase { "c: String, d: String, e: String, f: String, g: String, h: String, i: String, " + "j: String, k: String, l: String, m: String, n: String, o: String, p: String, " + "q: String, r: String, s: String, t: String, u: String, v: String, w: String, " + - "x: String, y: String, z: String) {\n"), + "x: String, y: String, z: String) {}\n"), Example("func superDuperLongFunctionDeclaration(a: String, b: String, " + "c: String, d: String, e: String, f: String, g: String, h: String, i: String, " + "j: String, k: String, l: String, m: String, n: String, o: String, p: String, " + "q: String, r: String, s: String, t: String, u: String, v: String, w: String, " + - "x: String, y: String, z: String) {\n"), + "x: String, y: String, z: String) {}\n"), + Example(""" + struct S { + public init(a: String, b: String, c: String, d: String, e: String, f: String, \ + g: String, h: String, i: String, j: String, k: String, l: String, \ + m: String, n: String, o: String, p: String, q: String, r: String, \ + s: String, t: String, u: String, v: String, w: String, x: String, \ + y: String, z: String) throws { + // ... + } + } + """), + Example(""" + struct S { + subscript(a: String, b: String, c: String, d: String, e: String, f: String, \ + g: String, h: String, i: String, j: String, k: String, l: String, \ + m: String, n: String, o: String, p: String, q: String, r: String, \ + s: String, t: String, u: String, v: String, w: String, x: String, \ + y: String, z: String) -> Int { + // ... + return 0 + } + } + """), ] private let longComment = Example(String(repeating: "/", count: 121) + "\n") @@ -50,8 +73,12 @@ final class LineLengthRuleTests: SwiftLintTestCase { let nonTriggeringExamples = baseDescription.nonTriggeringExamples + longFunctionDeclarations let description = baseDescription.with(nonTriggeringExamples: nonTriggeringExamples) - verifyRule(description, ruleConfiguration: ["ignores_function_declarations": true], - commentDoesntViolate: false, stringDoesntViolate: false) + verifyRule( + description, + ruleConfiguration: ["ignores_function_declarations": true], + commentDoesntViolate: false, + stringDoesntViolate: false + ) } func testLineLengthWithIgnoreCommentsEnabled() {