Skip to content
Open
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
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@

### Enhancements

* Add `excluded_members` configuration to `empty_enum_arguments` to skip members
that require empty parentheses (e.g. HealthKit static functions).
[leno23](https://github.com/leno23)
[#5269](https://github.com/realm/SwiftLint/issues/5269)

* Fix `unused_enumerated` false positives when offset and element are used in
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too many entries …

separate chained trailing closures after `.enumerated()`.
[leno23](https://github.com/leno23)
[#5600](https://github.com/realm/SwiftLint/issues/5600)

* Treat macro declarations like function declarations for `line_length` when
`ignores_function_declarations` is enabled.
[leno23](https://github.com/leno23)
[#5648](https://github.com/realm/SwiftLint/issues/5648)

* Print fixed code read from stdin to stdout.
[SimplyDanny](https://github.com/SimplyDanny)
[#6501](https://github.com/realm/SwiftLint/issues/6501)
Expand Down
21 changes: 19 additions & 2 deletions Source/SwiftLintBuiltInRules/Rules/Metrics/LineLengthRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import SwiftSyntax
struct LineLengthRule: Rule {
var configuration = LineLengthConfiguration()

private static let longMacroDeclaration = """
@freestanding(expression)
public macro obfuscate(_ value: String) -> String = #externalMacro(module: "ObfuscatedStringMacros", type: "ObfuscationMacro")
"""

static let description = RuleDescription(
identifier: "line_length",
name: "Line Length",
Expand All @@ -16,11 +21,16 @@ struct LineLengthRule: Rule {
Example(String(repeating: "/", count: 120) + ""),
Example(String(repeating: "#colorLiteral(red: 0.9607843161, green: 0.7058823705, blue: 0.200000003, alpha: 1)", count: 120) + ""),
Example(String(repeating: "#imageLiteral(resourceName: \"image.jpg\")", count: 120) + ""),
Example(
longMacroDeclaration,
configuration: ["ignores_function_declarations": true, "warning": 120]
),
],
triggeringExamples: [
Example(String(repeating: "/", count: 121) + ""),
Example(String(repeating: "#colorLiteral(red: 0.9607843161, green: 0.7058823705, blue: 0.200000003, alpha: 1)", count: 121) + ""),
Example(String(repeating: "#imageLiteral(resourceName: \"image.jpg\")", count: 121) + ""),
Example(longMacroDeclaration, configuration: ["warning": 120]),
].skipWrappingInCommentTests().skipWrappingInStringTests()
)
}
Expand All @@ -35,7 +45,7 @@ private extension LineLengthRule {
private var regexLiteralLines = Set<Int>()

override func visit(_ node: SourceFileSyntax) -> SyntaxVisitorContinueKind {
// Populate functionDeclarationLines if ignores_function_declarations is true
// Populate declaration lines for functions and macros when ignoring function declarations
if configuration.ignoresFunctionDeclarations {
let funcVisitor = FunctionLineVisitor(locationConverter: locationConverter)
functionDeclarationLines = funcVisitor.walk(tree: node, handler: \.lines)
Expand Down Expand Up @@ -147,7 +157,7 @@ private extension LineLengthRule {

// MARK: - Helper Visitors for Pre-computation

// Visitor to find lines spanned by function declaration signatures
// Visitor to find lines spanned by function and macro declaration signatures
private final class FunctionLineVisitor: SyntaxVisitor {
let locationConverter: SourceLocationConverter
var lines = Set<Int>()
Expand Down Expand Up @@ -181,6 +191,13 @@ private final class FunctionLineVisitor: SyntaxVisitor {
)
}

override func visitPost(_ node: MacroDeclSyntax) {
collectLines(
from: node.positionAfterSkippingLeadingTrivia,
to: node.endPositionBeforeTrailingTrivia
)
}

private func collectLines(from startPosition: AbsolutePosition, to endPosition: AbsolutePosition) {
let startLocation = locationConverter.location(for: startPosition)
let endLocation = locationConverter.location(for: endPosition)
Expand Down