realm / SwiftLint

A tool to enforce Swift style and conventions.
https://realm.github.io/SwiftLint
MIT License
18.45k stars 2.2k forks source link

Macro exclusion for line_length missing #5648

Open Craz1k0ek opened 2 days ago

Craz1k0ek commented 2 days ago

New Issue Checklist

Describe the bug

I have a specific setting in my .swiftlint.yml to exclude function declarations

line_length:
  warning: 120
  error: 200
  ignores_function_declarations: true

However, this ignores the macro definitions

// Line Length Violation: Line should be 120 characters or less; currently it has 126 characters (line_length)
@freestanding(expression)
public macro obfuscate(_ value: String) -> String = #externalMacro(module: "ObfuscatedStringMacros", type: "ObfuscationMacro")
Complete output when running SwiftLint, including the stack trace and command used
$ swiftlint lint

Gives me

Line Length Violation: Line should be 120 characters or less; currently it has 126 characters (line_length)

Environment

disabled_rules:
  - todo
  - trailing_comma
  - trailing_whitespace
opt_in_rules:
  - empty_count
line_length:
  warning: 120
  error: 200
  ignores_comments: true
  ignores_function_declarations: true
  ignores_interpolated_strings: true
  ignores_urls: true
// This triggers a violation:
@freestanding(expression)
public macro obfuscate(_ value: String) -> String = #externalMacro(module: "ObfuscatedStringMacros", type: "ObfuscationMacro")
Craz1k0ek commented 14 hours ago

I tried fixing this issue myself, but it appears the macro is not correctly parsed, but I can't figure out why. Parts of the code I added

if configuration.ignoresMacroDeclarations {
    print("Configuration set to ignore macros")
    if lineHasKinds(line: line, kinds: macroKinds, kindsByLine: swiftDeclarationKindsByLine.value) {
        print("Ignoring line \(line.index) due to macro declaration: \(line.content)")
        return nil
    } else {
        print("""
        Something went wrong, debug info
        ================================
        Checking line \(line.index): \(line.content)

        swiftDeclarationKindsByLine.value: \(swiftDeclarationKindsByLine.value)
        """)
    }
}

Outputs the following

Configuration set to ignore macros
Something went wrong, debug info
================================
Checking line 19: public macro obfuscate(_ value: String) -> String = #externalMacro(module: "ObfuscatedStringMacros", type: "ObfuscationMacro")

swiftDeclarationKindsByLine.value: [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [SourceKittenFramework.SwiftDeclarationKind.varParameter]]

This appears to be a SourceKitten issue. I've added an issue there.