Skip to content

Commit dcbdcc9

Browse files
authored
Ignore function, initializer and subscript declarations alike (#6242)
1 parent 51f112f commit dcbdcc9

File tree

3 files changed

+47
-5
lines changed

3 files changed

+47
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
### Bug Fixes
1818

19-
* None.
19+
* Ignore function, initializer and subscript declarations alike when the
20+
`ignores_function_declarations` option is enabled in the `line_length` rule.
21+
[SimplyDanny](https://github.com/SimplyDanny)
22+
[#6241](https://github.com/realm/SwiftLint/issues/6241)
2023

2124
## 0.61.0: Even Fresher Breeze
2225

Source/SwiftLintBuiltInRules/Rules/Metrics/LineLengthRule.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,18 @@ private final class FunctionLineVisitor: SyntaxVisitor {
147147
}
148148

149149
override func visitPost(_ node: FunctionDeclSyntax) {
150+
collectLines(from: node)
151+
}
152+
153+
override func visitPost(_ node: InitializerDeclSyntax) {
154+
collectLines(from: node)
155+
}
156+
157+
override func visitPost(_ node: SubscriptDeclSyntax) {
158+
collectLines(from: node)
159+
}
160+
161+
private func collectLines(from node: any SyntaxProtocol) {
150162
let startLocation = locationConverter.location(for: node.positionAfterSkippingLeadingTrivia)
151163
let endLocation = locationConverter.location(for: node.endPositionBeforeTrailingTrivia)
152164
for line in startLocation.line...endLocation.line {

Tests/BuiltInRulesTests/LineLengthRuleTests.swift

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,35 @@ final class LineLengthRuleTests: SwiftLintTestCase {
77
"c: String, d: String, e: String, f: String, g: String, h: String, i: String, " +
88
"j: String, k: String, l: String, m: String, n: String, o: String, p: String, " +
99
"q: String, r: String, s: String, t: String, u: String, v: String, w: String, " +
10-
"x: String, y: String, z: String) {\n"),
10+
"x: String, y: String, z: String) {}\n"),
1111
Example("func superDuperLongFunctionDeclaration(a: String, b: String, " +
1212
"c: String, d: String, e: String, f: String, g: String, h: String, i: String, " +
1313
"j: String, k: String, l: String, m: String, n: String, o: String, p: String, " +
1414
"q: String, r: String, s: String, t: String, u: String, v: String, w: String, " +
15-
"x: String, y: String, z: String) {\n"),
15+
"x: String, y: String, z: String) {}\n"),
16+
Example("""
17+
struct S {
18+
public init(a: String, b: String, c: String, d: String, e: String, f: String, \
19+
g: String, h: String, i: String, j: String, k: String, l: String, \
20+
m: String, n: String, o: String, p: String, q: String, r: String, \
21+
s: String, t: String, u: String, v: String, w: String, x: String, \
22+
y: String, z: String) throws {
23+
// ...
24+
}
25+
}
26+
"""),
27+
Example("""
28+
struct S {
29+
subscript(a: String, b: String, c: String, d: String, e: String, f: String, \
30+
g: String, h: String, i: String, j: String, k: String, l: String, \
31+
m: String, n: String, o: String, p: String, q: String, r: String, \
32+
s: String, t: String, u: String, v: String, w: String, x: String, \
33+
y: String, z: String) -> Int {
34+
// ...
35+
return 0
36+
}
37+
}
38+
"""),
1639
]
1740

1841
private let longComment = Example(String(repeating: "/", count: 121) + "\n")
@@ -50,8 +73,12 @@ final class LineLengthRuleTests: SwiftLintTestCase {
5073
let nonTriggeringExamples = baseDescription.nonTriggeringExamples + longFunctionDeclarations
5174
let description = baseDescription.with(nonTriggeringExamples: nonTriggeringExamples)
5275

53-
verifyRule(description, ruleConfiguration: ["ignores_function_declarations": true],
54-
commentDoesntViolate: false, stringDoesntViolate: false)
76+
verifyRule(
77+
description,
78+
ruleConfiguration: ["ignores_function_declarations": true],
79+
commentDoesntViolate: false,
80+
stringDoesntViolate: false
81+
)
5582
}
5683

5784
func testLineLengthWithIgnoreCommentsEnabled() {

0 commit comments

Comments
 (0)